PDA

View Full Version : HugeInt Problem


i3rasaila
03-18-2008, 09:06 PM
this is my coding..
can someone help me for the //comment part..
how to get the result for that part..

#include<stdexcept>
#include<iostream.h>
#include<iomanip>
#include<cctype>
#include<cstring>
//using namespace std;


class HugeInt
{
friend ostream &operator<<( ostream &, const HugeInt & );
friend istream &operator>>( istream &, HugeInt & );

private:
short num[30];

public:

HugeInt( long =0 );
HugeInt( const char * );

HugeInt operator+( const HugeInt & ) const;
HugeInt operator+( int ) const;
HugeInt operator+( const char * ) const;

HugeInt operator-( const HugeInt & ) const;
HugeInt operator-( int ) const;
HugeInt operator-( const char * ) const;

HugeInt operator/( const HugeInt & ) const;
HugeInt operator/( int ) const;
HugeInt operator/( const char * ) const;

HugeInt operator*( const HugeInt & ) const;
HugeInt operator*( int ) const;
HugeInt operator*( const char * ) const;

bool operator==( const HugeInt & ) const;
bool operator<=( const HugeInt & ) const;
bool operator>=( const HugeInt & ) const;
bool operator!=( const HugeInt & ) const;
void factorial (HugeInt obb);

};

HugeInt::HugeInt( long integer )
{
for ( int i=0;i<30;i++ )
num[i]=0;

for ( int j=29; integer!=0 && j>=0; j-- )
{
num[j]=integer%10;
integer/=10;
}
}

HugeInt::HugeInt( const char *sentence )
{
for ( int i=0;i<30;i++ )
num[i]=0;

int length=strlen( sentence );

for ( int j=30-length,k=0;j<30;j++,k++ )
{
if ( isdigit(sentence[k]) )
num[j]=sentence[k]-'0';
}
}



HugeInt HugeInt::operator+( const HugeInt &obj ) const
{
HugeInt temp;
int upAdd=0;

for( int i=29;i>=0;i-- )
{
temp.num[i]= num[i] + obj.num[i] + upAdd;

if( temp.num[i]>9 )
{
temp.num[i]%=10;
upAdd=1;
}

else
upAdd=0;
}

return temp;
}

HugeInt HugeInt::operator+( int obj ) const
{
return *this + HugeInt( obj );
}

HugeInt HugeInt::operator+( const char *obj ) const
{
return *this + HugeInt( obj );
}

HugeInt HugeInt::operator-( const HugeInt &obj ) const
{
HugeInt temp;
int upMinus=0;

for( int i=29;i>=0;i-- )
{
temp.num[i]= num[i] - obj.num[i] + upMinus;

if( num[i]>0 )
{
temp.num[i]%=10;
upMinus=1;
}

else
upMinus=0;
}

return temp;
}

HugeInt HugeInt::operator-( int obj ) const
{
return *this - HugeInt( obj );
}

HugeInt HugeInt::operator-( const char *obj ) const
{
return *this - HugeInt( obj );
}

HugeInt HugeInt::operator/( const HugeInt &obj ) const
{
HugeInt temp;

for( int i=29;i>=0;i-- )
{
if( num[i]>obj.num[i] )
{
temp.num[i]= num[i] / obj.num[i];
temp.num[i]%=10;
}
else
;
}
return temp;
}

HugeInt HugeInt::operator/( int obj ) const
{
return *this / HugeInt( obj );
}

HugeInt HugeInt::operator/( const char *obj ) const
{
return *this / HugeInt( obj );
}

HugeInt HugeInt::operator*( const HugeInt &obj ) const
{
HugeInt temp,result;
int upMul=0;

for( int j=29;j>=0;j-- )
{
for(int i=29,a=j;i>=0;i--,a--)
{
temp.num[a]= ((num[i] * obj.num[j])%10) + upMul;
upMul=((num[i]*obj.num[j])/10);
}
result=result+temp;
}

return result;
}

HugeInt HugeInt::operator*( int obj ) const
{
return *this * HugeInt( obj );
}

HugeInt HugeInt::operator*( const char *obj ) const
{
return *this *HugeInt( obj );
}

bool HugeInt::operator==( const HugeInt &obj ) const
{
return false;
}

bool HugeInt::operator<=( const HugeInt &obj ) const
{
return false;
}

bool HugeInt::operator>=( const HugeInt &obj ) const
{

return true;
}

bool HugeInt::operator!=( const HugeInt &obj ) const
{
return true;
}

inline HugeInt& operator-(HugeInt& a,int b) {return (a-(HugeInt)b);}
inline HugeInt& operator+(HugeInt& a,int b) {return (a+(HugeInt)b);}
inline HugeInt& operator*(HugeInt& a,int b) {return (a*(HugeInt)b);}
/*void HugeInt::factorial(HugeInt obb)
{
HugeInt temp=HugeInt(obb);

if(obb==0)
cout<<obb<<"!="<<endl;

while(obb>1)
{
temp=temp*factorial(obb - 1);
cout<<temp<<"!="<<endl;
}
}*/
ostream &operator<<( ostream &output, const HugeInt &obj )
{
for ( int i=0;(obj.num[i]==0)&&(i<30);i++ )
;

if ( i==30 )
output<<0;
else
{
for (;i<30;i++)
output<<obj.num[i];
}
return output;
}

istream &operator>> ( istream & input, HugeInt & obj )
{

char temp[30];

input>>temp;
obj=temp;

return input;

}
int main()
{

HugeInt a; //HugeInt object initialized to zero
HugeInt b(12345);
HugeInt c("100200101002005550");
HugeInt result;

cin >> a;
result = a+b;
cout << a << "+" << b << " = " << result << endl;

result = c - b;
cout << result << endl;

result = c / b;
cout << result << endl;

result = c * b;
cout << result << endl;

if (a == b)
cout << "Equal" << endl;
else
cout << "Not Equal" << endl;

if (a >= b)
cout << "Greater" << endl;
else
cout << "Less" << endl;

if (a <= b)
cout << "Less" << endl;
else
cout << "Greater" << endl;

if (a != b)
cout << "Not Equal" << endl;
else
cout << "Equal" << endl;

//factorial(b); //will output the result of b factorial

return 0;

}