get rid of leading zeros

Hey folks, Im a newebie in c++ and I was working on operator overloading to figure out how they work but Im kinda stuck in not a cool problem (leading zeros).

I have bunch of arithmetic and IO overloaded operators, what I want to do is to suppress leading zero in my insert operator so the final calculated result is without any leading zeros.
Last edited on
Assuming that there is no + or - char in front:
1
2
3
4
5
6
7
std::ostream& operator << ( std::ostream& output, const Bigint& number )
{
    std::string str( number.numerals_, DIGITS ) ;
    auto pos = str.find_first_not_of( '0' ) ;
    if( pos != std::string::npos ) return output << str.substr(pos) ;
    else return output << '0' ;
}


Consider:
1
2
// #define DIGITS 50
constexpr int DIGITS = 50 ;
you can also define an END_DIGIT value, such as const int END_DIGIT = -1 or #define END_DIGIT -1

Example:
Bigint() contains [0 0 0 ... 0 -1 0]
Bigint(123456789) contains [0 0 0 ... 0 -1 9 8 7 6 5 4 3 2 1]

iter through every digit of Bigint number:
1
2
3
4
5
for (int i = DIGITS - 1; i >= 0; i--)
{
    if (number[i] == END_DIGIT) break;
    //Your code
}
Thanks folks.

I'm trying to edit the extract part to set the unused leading digits to zero since the array is filled via user input so for instance if some body put a digit string that is less than my DEFINE DIGITS 50 (like 5 + 6) the answer will be 11 (11 is two characters so far) plus 48 other funny non-sense characters.

The strategy is to set the null (unused) array elements to 0 (my loop counts backward in array so null elements are always leading elements in the array) so as we did earlier in extract operator those null elements which are now sat to be zero are suppressed in insert operator.

here is the extract operator (I come up with something but its not working!) :
1
2
3
4
5
6
7
8
9
10
11

std::istream& operator >>(std::istream& input, Bigint& number) {
    string test ;
    if (in >> test ) {
        for (int i = DIGITS - 1; i >= 0; i--) {

        }
    }
    return in;
}


Last edited on
> The strategy is to set the null (unused) array elements to 0

Fill the entire array with zeroes and then iterate forwards from the first user entered digit, perhaps.

For instance,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <algorithm>
#include <iostream>
#include <cctype>

struct bigint
{
    enum { NDIGITS = 100 } ;
    int numerals_[NDIGITS] ;
};

std::istream& operator >> ( std::istream& stm, bigint& b )
{
    std::fill_n( b.numerals_, bigint::NDIGITS, 0 ) ; // fill with zeroes
    std::string str ;
    if( stm >> str && str.size() <= bigint::NDIGITS )
    {
        std::size_t first = bigint::NDIGITS - str.size() ; // start position
        for( std::size_t i = 0 ; i < str.size() ; ++i )
             b.numerals_[first+i] = std::isdigit( str[i] ) ? str[i] - '0' : 0 ;
             // is it a good idea to treat invalid characters as zero 
    }
    else stm.clear( std::ios::failbit ) ;
    return stm ;
}

std::ostream& operator<< ( std::ostream& stm, const bigint& b )
{
    std::string str( b.numerals_, b.numerals_ + bigint::NDIGITS ) ;
    for( char& c : str ) c += '0' ; // for every char, add '0' for literal digit
    auto pos = str.find_first_not_of( '0' ) ;
    if( pos != std::string::npos ) return stm << str.substr(pos) ;
    else return stm << '0' ;
}
Last edited on
Topic archived. No new replies allowed.