| obay (1) | |
|
how to make (/)operator? // Fig. 11.23: Hugeint.cpp // HugeInt member-function and friend-function definitions. #include <cctype> // isdigit function prototype #include <cstring> // strlen function prototype //#include <math.h> i don't need pow function #include "Hint.h" // HugeInt class definition #include<cstdlib> // default constructor; conversion constructor that converts // a long integer into a HugeInt object HugeInt::HugeInt( long value ) { // initialize array to zero for ( int i = 0; i <= 29; i++ ) integer[ i ] = 0; // place digits of argument into array for ( int j = 29; value != 0 && j >= 0; j-- ) { integer[ j ] = value % 10; value /= 10; } // end for } // end HugeInt default/conversion constructor // conversion constructor that converts a character string // representing a large integer into a HugeInt object HugeInt::HugeInt( const char *string ) { // initialize array to zero for ( int i = 0; i <= 29; i++ ) integer[ i ] = 0; // place digits of argument into array int length = strlen( string ); for ( int j = 30 - length, k = 0; j <= 29; j++, k++ ) if ( isdigit( string[ k ] ) ) integer[ j ] = string[ k ] - '0'; } // end HugeInt conversion constructor // addition operator; HugeInt + HugeInt HugeInt HugeInt::operator+( const HugeInt &op2 ) const { HugeInt temp; // temporary result int carry = 0; for ( int i = 29; 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 return temp; // return copy of temporary object } // end function operator+ // addition operator; HugeInt + int HugeInt HugeInt::operator+( int op2 ) const { // convert op2 to a HugeInt, then invoke // operator+ for two HugeInt objects return *this + HugeInt( op2 ); } // end function operator+ // addition operator; // HugeInt + string that represents large integer value HugeInt HugeInt::operator+( const char *op2 ) const { // convert op2 to a HugeInt, then invoke // operator+ for two HugeInt objects return *this + HugeInt( op2 ); } // end operator+ // overloaded output operator ostream& operator<<( ostream &output, const HugeInt &num ) { int i; for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ ) ; // skip leading zeros if ( i == 30 ) output << 0; else for ( ; i <= 29; i++ ) output << num.integer[ i ]; return output; } int HugeInt::countd() { int i; for ( i = 0; (integer[ i ] == 0 ) && ( i <= 29 ); i++ ); i=30-i; return i; } HugeInt HugeInt::operator*(HugeInt o) { HugeInt hold("0"); for(int i=29,dn=29-o.countd();i>dn;i--) { hold=hold + *this*o.integer[i]; return hold; } } HugeInt HugeInt::operator*(const int o) { HugeInt temp(1); // temporary result int carry = 0; int dn=29-countd(); for ( int i = 29; i >= dn; i-- ) { //std::cout<<"d = "<<carry<<" "; temp.integer[ i ] = integer[ i ] * o + carry; std::cout<<"d = "<<carry<<" __ ";//zero! // determine whether to carry a 1 if ( temp.integer[ i ] > 9 ) { //std::cout<<"d = "<<temp.integer[ i ]<<" "; carry = temp.integer[ i ]/10; temp.integer[ i ] %= 10; // reduce to 0-9 } // end if else // no carry carry = 0; } // end for return temp; // return copy of temporary object } bool HugeInt::operator>(HugeInt o) { if(countd()>o.countd()) return 1; else if(countd()<o.countd()) return 0; else for(int i=30-countd();i<30;i++) { if(integer[i]>o.integer[i]) return 1; else if(integer[i]<o.integer[i]) return 0; } return 0; } | |
|
|
|
| Aceix (455) | |||
Do you mean overloading, and pls use code tags | |||
|
|
|||
| doug4 (224) | |
|
I agree about using code tags. You can use the "<>" button on the Format options. It will make your code MUCH easier for us to read. And what's the deal with declaring functions to return bool and then returning an int? Use the values true and false. That's what they are there for, and it is easier for a reader to know what's going on.
| |
|
|
|