Home > Mobile >  A Hugeint object Hugeint object = garbage value. Huh?
A Hugeint object Hugeint object = garbage value. Huh?

Time:04-15

I'm making 2 Hugeint objects with this constructor

HugeInt.h

public: 
    static const int digits = 30; // maximum digits in a Hugelnt 
    HugeInt( long = 0 ); // conversion/default constructor 
    HugeInt( const string & ); // conversion constructor 
    //addition operator; Hugelnt   Hugelnt 
    HugeInt operator ( const HugeInt & ) const; 
private: 
short integer[ digits ]; 

first I convert my int or string into an integer array

HugeInt.cpp

HugeInt::HugeInt( long value)
{ 
    // initialize array to zero 
    for ( int i = 0; i < digits; i   ) 
        integer[i] = 0; 
        
    // place digits of argument into array 
    for ( int j = digits - 1; value != 0 && j>= 0; j-- ) 
    { 
        integer[j] = value % 10; 
        value /= 10; 
    } // end for 
} // end Hugelnt default/conversion constructor 
// conversion constructor that converts a character string 
// representing a large integer into a HugeInt object 
HugeInt::HugeInt( const string &number) 
{
    // initialize array to zero 
    for ( int i = 0; i < digits; i   ) 
        integer[i] = 0; 
    
    // place digits of argument into array 
    int length = number.size(); 
    
    for ( int j = digits - length, k = 0; j < digits; j  , k   ) 
        if ( isdigit( number[ k])) // ensure that character is a digit 
            integer[j] = number[ k ] - '0'; 
} // end Hugelnt conversion constructor

operator for HugeInt HugeInt

HugeInt HugeInt::operator ( const HugeInt &op2 ) const
{ 
    HugeInt temp; // temporary result 
    int carry = 0; 
    
    for ( int i = digits - 1; i >= 0; i-- ) 
    { 
        temp.integer[i] = integer[i]   op2.integer[i]   carry; 
        
        // determine whether to carry a 1 
        if ( temp.integer[i] > 9) 
        { 
            temp.integer[i] %= 10; // reduce to 0-9 
            carry = 1; 
        } // end if
         else // no carry 
            carry = 0; 
    } // end for 
    
}

operator<< to output the HugeInt

ostream& operator<<( ostream &output, const HugeInt &num ) 
{ 
    int i; 
    
    for (i = 0; ( num.integer[i] == 0 ) && ( i <= HugeInt::digits ); i   )
        ;// skip leading zeros
        
    if ( i == HugeInt::digits ) 
        output << 0 ; 
    else 
        for (; i < HugeInt::digits; i   ) 
            output << num.integer[i]; 
        
    return output; 
} // end function operator<<

in main.cpp it looks like this.

HugeInt n3( "9999999999999999999999999999999999" ); 
HugeInt n4( "1" ); 
cout <<"n6 = n3   n4 = " << n3 << "   "<< n4 << " = " << n3 n4 << "\n\n";

But the output become random numbers

n6 = n3   n4 = 9999999999999999999999999999999999   1 = 23919128293276100-2095744600-20957446000006832-2095744606848-2095744607519200006000-20957

I add return temp at the end of operator , the result becomes = 0 if I add return *this, the results become = 9999999999999999999999999999999999

the result should be 10000000000000000000000000000000000

Please help explain this to me, my English is bad so sorry for bad question.

CodePudding user response:

Your major problem is that you hard code maximum number of digits you can deal with to 30

But

 9999999999999999999999999999999999
 1234567890123456789012345678901234

this string is 34 digits

Plus you build the result of the add into 'temp' but do nothing with it

added

 return temp;

increased digits to 40

 n6 = n3   n4 = 9999999999999999999999999999999999   1 = 10000000000000000000000000000000000
  •  Tags:  
  • c
  • Related