Create BigInteger Class and Overload multiplication, division and all relational and equality operators. - C++ Class

C++ examples for Class:Operator Overload

Description

Create BigInteger Class and Overload multiplication, division and all relational and equality operators.

Demo Code

                                                                                                                                                               
#include <iostream> 
#include <string> 
using namespace std; 
                                                                                                                                                               
class BigInteger 
 { //from   w w  w  .j a  v a 2  s .c om
    friend ostream &operator<<( ostream &, const BigInteger & ) ; 
public: 
    static const int digits = 30; // maximum digits in a BigInteger 
                                                                                                                                                               
    BigInteger( long = 0 ); // conversion/default constructor 
    BigInteger( const string & ); // conversion constructor 
                                                                                                                                                               
    // addition operator; BigInteger + BigInteger 
    BigInteger operator+( const BigInteger & ) const; 
                                                                                                                                                               
    // addition operator; BigInteger + int 
    BigInteger operator+( int ) const; 
                                                                                                                                                               
    // addition operator; 
    // BigInteger + string that represents large integer value 
    BigInteger operator+( const string & ) const; 
private: 
    short integer[ digits ]; 
 }; // end class HugetInt 
                                                                                                                                                               
#include <cctype> // isdigit function prototype 
using namespace std; 
                                                                                                                                                               
// default constructor; conversion constructor that converts a long integer into a BigInteger object 
 BigInteger::BigInteger( 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; 
    }
}
                                                                                                                                                               
// conversion constructor that converts a character string representing a large integer into a BigInteger object 
BigInteger::BigInteger( 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'; 
}
                                                                                                                                                               
// addition operator; BigInteger + BigInteger 
BigInteger BigInteger::operator+( const BigInteger &op2 ) const 
{ 
    BigInteger 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; 
       }
       else // no carry 
          carry = 0; 
    }
                                                                                                                                                               
    return temp; // return copy of temporary object 
}
                                                                                                                                                               
// addition operator; BigInteger + int 
BigInteger BigInteger::operator+( int op2 ) const 
{ 
    // convert op2 to a BigInteger, then invoke operator+ for two BigInteger objects 
    return *this + BigInteger( op2 ); 
}
                                                                                                                                                               
// addition operator; 
// BigInteger + string that represents large integer value 
BigInteger BigInteger::operator+( const string &op2 ) const 
{ 
   // convert op2 to a BigInteger, then invoke 
   // operator+ for two BigInteger objects 
    return *this + BigInteger( op2 ); 
}
                                                                                                                                                               
// overloaded output operator 
ostream& operator<<( ostream &output, const BigInteger &num ) 
{ 
    int i; 
                                                                                                                                                               
    for ( i = 0 ; ( num.integer[ i ] == 0 ) && ( i <= BigInteger::digits ); ++i ) 
       ; // skip leading zeros 
                                                                                                                                                               
    if ( i == BigInteger::digits ) 
       output << 0; 
    else 
       for ( ; i < BigInteger::digits; ++i ) 
          output << num.integer[ i ]; 
                                                                                                                                                               
    return output; 
}
                                                                                                                                                               
#include <iostream> 
using namespace std; 
                                                                                                                                                               
int main() 
{ 
    BigInteger n1( 7654321 ); 
    BigInteger n2( 7891234 ); 
    BigInteger n3( "12123129123123123123123999999" ); 
    BigInteger n4( "1" ); 
    BigInteger n5; 
                                                                                                                                                               
    cout << "n1 is " << n1 << "\nn2 is " << n2 
       << "\nn3 is " << n3 << "\nn4 is " << n4 
       << "\nn5 is " << n5 << "\n\n"; 
                                                                                                                                                               
    n5 = n1 + n2; 
    cout << n1 << " + " << n2 << " = " << n5 << "\n\n"; 
                                                                                                                                                               
    cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 ) << "\n\n"; 
                                                                                                                                                               
    n5 = n1 + 9; 
    cout << n1 << " + " << 9 << " = " << n5 << "\n\n"; 
                                                                                                                                                               
    n5 = n2 + "10000"; 
    cout << n2 << " + " << "10000" << " = " << n5 << endl; 
}

Result


Related Tutorials