#ifndef HUGEINT_H
#define HUGEINT_H
#include<iostream>
using std::ostream;
class HugeInt
{
friend ostream &operator<<( ostream &output, const HugeInt &A) {
for (int i=A.getLength();i>=0;i--) output<<A.integer[i];
return output;
}
public:
HugeInt(int =0);
HugeInt(const char *);
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;
bool operator>(const HugeInt &) const;
bool operator>=(const HugeInt &) const;
HugeInt operator-(const HugeInt &) const;
HugeInt operator*(const HugeInt &) const;
HugeInt operator/(const HugeInt &) const;
int getLength() const;
private:
int integer[30];
};
#endif
#include<iostream>
#include"HugeInt.h"
using namespace std;
HugeInt::HugeInt(int number) {
int cnt=0;
while (number!=0) {
integer[cnt++]=number%10;
number/=10;
}
while (cnt<30) integer[cnt++]=0;
}
HugeInt::HugeInt(const char *s) {
int len=strlen(s);
int cnt=0;
while (cnt<len) integer[cnt++]=s[len-cnt-1]-'0';
while (cnt<30) integer[cnt++]=0;
}
HugeInt HugeInt::operator+(const HugeInt &A) const {
int len=max(this->getLength(),A.getLength());
HugeInt ans;
for (int i=0;i<=len;i++) {
ans.integer[i]+=integer[i]+A.integer[i];
ans.integer[i+1]+=ans.integer[i]/10;
ans.integer[i]%=10;
}
return ans;
}
HugeInt HugeInt::operator+(int number) const {
return (*this)+HugeInt(number);
}
HugeInt HugeInt::operator+(const char *s) const {
return (*this)+HugeInt(s);
}
bool HugeInt::operator==(const HugeInt &A) const {
for (int i=0;i<30;i++)
if (integer[i]!=A.integer[i]) return 0;
return 1;
}
bool HugeInt::operator!=(const HugeInt &A) const {
return !((*this)==A);
}
bool HugeInt::operator<(const HugeInt &A) const {
for (int i=30;i>=0;i--)
if (integer[i]<A.integer[i]) return 1;
else if (integer[i]>A.integer[i]) return 0;
return 0;
}
bool HugeInt::operator<=(const HugeInt &A) const {
return ((*this)<A)||((*this)==A);
}
bool HugeInt::operator>(const HugeInt &A) const {
for (int i=30;i>=0;i--)
if (integer[i]>A.integer[i]) return 1;
else if (integer[i]<A.integer[i]) return 0;
return 0;
}
bool HugeInt::operator>=(const HugeInt &A) const {
return ((*this)>A)||((*this)==A);
}
HugeInt HugeInt::operator-(const HugeInt &A) const {
HugeInt ans;
HugeInt tmp=(*this);
for (int i=0;i<30;i++) {
while (tmp.integer[i]<A.integer[i]) {tmp.integer[i+1]--;tmp.integer[i]+=10;}
ans.integer[i]=tmp.integer[i]-A.integer[i];
}
return ans;
}
HugeInt HugeInt::operator*(const HugeInt &A) const {
if ((*this)==HugeInt(0)||A==HugeInt(0)) return HugeInt(0);
HugeInt ans;
int len=A.getLength();
for (int i=len;i>=0;i--) {
for (int j=ans.getLength();j>=0;j--) ans.integer[j+1]=ans.integer[j]; ans.integer[0]=0;
HugeInt tmp=(*this);
int t=A.integer[i],o=0;
for (int j=0;j<=getLength();j++) {
tmp.integer[j]*=t;
tmp.integer[j]+=o;
o=tmp.integer[j]/10;
tmp.integer[j]%=10;
}
t=tmp.getLength();
while (tmp.integer[t]>=10) {
tmp.integer[t+1]=tmp.integer[t]/10;
tmp.integer[t]%=10;
t++;
}
ans=ans+tmp;
}
return ans;
}
HugeInt HugeInt::operator/(const HugeInt &A) const {
if ((*this)<A) return HugeInt(0);
HugeInt ans;
HugeInt tmp;
for (int i=getLength();i>=0;i--) {
for (int j=tmp.getLength();j>=0;j--) tmp.integer[j+1]=tmp.integer[j]; tmp.integer[0]=0;
for (int j=ans.getLength();j>=0;j--) ans.integer[j+1]=ans.integer[j]; ans.integer[0]=0;
tmp=tmp+integer[i];
while (tmp>=A) {
tmp=tmp-A;
ans=ans+1;
}
}
return ans;
}
int HugeInt::getLength() const {
int cnt=29;
while (!integer[cnt]&&cnt>0) cnt--;
return cnt;
}
#include<iostream>
#include"HugeInt.h"
using namespace std;
int main()
{
HugeInt n1(7654321),n2(7891234);
HugeInt n3("9999999999999999999999999");
HugeInt n4(1),n5(12341234),n6(7888);
cout<<"n1 is "<<n1<<endl;
cout<<"n2 is "<<n2<<endl;
cout<<"n3 is "<<n3<<endl;
cout<<"n4 is "<<n4<<endl;
cout<<"n5 is "<<n5<<endl;
cout<<"n6 is "<<n6<<endl;
cout<<endl;
cout<<"n1 is"<<(n1==n2 ? " ":" not ")<<"equal to n2\n";
cout<<"n1 is"<<(n1<n2 ? " ":" not ")<<"less than n2\n";
cout<<"n1 is"<<(n1<=n2 ? " ":" not ")<<"less than or equal to n2\n\n";
cout<<n1<<" + "<<n1<<" = "<<n1+n2<<endl;
cout<<endl;
cout<<n3<<" + "<<n4<<" = "<<n3+n4<<endl;
cout<<endl;
cout<<n1<<" + 9 = "<<n1+9<<endl;
cout<<n2<<" + 10000 = "<<n2+10000<<endl;
cout<<n5<<" * "<<n6<<" = "<<n5*n6<<endl;
cout<<n5<<" - "<<n6<<" = "<<n5-n6<<endl;
cout<<n5<<" / "<<n6<<" = "<<n5/n6<<endl;
system("pause");
return 0;
}