Bài số 2 mình giải bằng quá tải toán tử được không ?
PHP Code:
#include <iostream>
using std::endl;
using std::cout;
using std::endl;
#include <fstream>
using std::ostream;
#include <iomanip>
using std::setw;
#include <cctype> /* isdigit function prototype*/
#include <string> /*strlen function prototype*/
class HugeInt
{
/*Redefine operator << */
friend ostream &operator << ( ostream &, const HugeInt &);
public :
HugeInt ( long = 0 ); /*Conversion or Default constructor*/
HugeInt ( const char* ); /*Conversion constructor*/
/*Addition operator HugeInt + HugeInt*/
HugeInt operator+ ( const HugeInt & );
/*Addition operator : HugeInt + int */
HugeInt operator+ ( int );
/*HugeInt + string that represents large integer value*/
HugeInt operator+ ( const char*);
private :
short INT[30]; /*Integer array*/
};
/*Default/Conversion constructor that converts
a long integer into Hugeint Object*/
HugeInt::HugeInt ( long value )
{
/*Initialize all elements to zero*/
for ( int i = 0; i <= 29; i++ ){
INT[i] = 0;
}
for ( int j = 29; value != 0 && j >= 0; j-- ){
INT[j] = value % 10;
value = value / 10;
}
}
/*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++ ){
INT[i] = 0;
}
/*place digits of arguement into array*/
int length = strlen ( string );
for ( int j = 30 - length, k = 0; j <= 29; j++, k++ ){
if ( isdigit ( string[k] ))
INT[j] = string[k] - '0';
}
}
/*HugeInt + HugeInt*/
HugeInt HugeInt::operator + ( const HugeInt &Obj )
{
/*Temporary result*/
HugeInt tmpVal;
int carry = 0;
for ( int i = 29; i >= 0; i-- ){
tmpVal.INT[i] = INT[i] + Obj.INT[i] + carry;
/*Examine whether to carry a "1"*/
if ( tmpVal.INT[i] > 9 )
{
tmpVal.INT[i] = tmpVal.INT[i] % 10;
carry = 1
}
else
{
carry = 0;
}
}
/*return copy of temporary object*/
return tmpVal;
}
/*HugeInt + Int*/
HugeInt HugeInt::operator + ( int Obj )
{
/*convert Ob to a HugeInt then call
operator + for 2 HugeInt object*/
return *this + HugeInt ( Obj );
}
/*HugeInt + string*/
HugeInt HugeInt::operator + ( const char *Obj )
{
return *this + HugeInt ( Obj );
}
/*Redefine operator << for convinient purpose ^^*/
ostream& operator << ( ostream &output, const HugeInt &num )
{
int i;
for ( i = 0; (num.INT[i] == 0 ) && ( i <= 29 ); i++ )
; /*Keep leading stupid zeros^^*/
if ( i == 30 )
output << 0;
else
for (; i <= 29; i++ )
output << num.INT[i];
return output;
}
int main (void)
{
HugeInt FIRST ( 83424914 );
HugeInt SECOND ( 248141840 );
HugeInt THIRD ( "192313493225290502");
HugeInt FOURTH ( "1" );
HugeInt FIFTH;
cout << "\nFIRST is : " << FIRST
<< "\nSECOND is : " << SECOND
<< "\nTHIRD is : " << THIRD
<< "\nFOURTH is : " << FOURTH
<< "\nFIFTH is : " << FIFTH << "\n\n";
FIFTH = FIRST + SECOND;
cout << FIRST << " + "
<< SECOND << " = "
<< FIFTH << "\n\n";
cout << THIRD << " + "
<< FOURTH << " = "
<< ( THIRD + FOURTH ) << "\n\n";
FIFTH = FIRST + 7;
cout << FIRST << " + "
<< 9 << " = "
<< FIFTH << "\n\n";
FIFTH = SECOND + "999999";
cout << SECOND << " + "
<< "999999" << " = "
<< FIFTH << "\n\n";
system("pause")
}