#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using std::ostream;
using std::cout;
using std::cin;
using std::endl;
const int MAX = 110;
class CHugeInt
{
public:
CHugeInt()
{
length = MAX;
memset(num, '0', sizeof(num));
}
CHugeInt(const char* origin)
{
memset(num, '0', sizeof(num));
int m = 0;
for(int i = strlen(origin) - 1; i >= 0; i--)
{
num[m++] = origin[i];
}
num[m] = 0;
length = m;
}
CHugeInt(int origin)
{
char buf[MAX] = {};
int m = 0;
while(origin != 0)
{
buf[m++] = origin % 10 + '0';
origin /= 10;
}
buf[m] = 0;
length = m;
strcpy(num, buf);
}
CHugeInt(CHugeInt& b)
{
length = b.length;
strcpy(num, b.num);
}
friend ostream& operator << (ostream& output, CHugeInt& b);
friend CHugeInt operator + (CHugeInt& b1, CHugeInt& b2);
friend CHugeInt operator + (CHugeInt& a, int b);
friend CHugeInt operator + (int a, CHugeInt& b);
CHugeInt& operator ++()
{
*this = *this + 1;
return *this;
}
CHugeInt operator ++(int)
{
CHugeInt temp = *this;
*this = *this + 1;
return temp;
}
CHugeInt operator += (int a)
{
*this = *this + 5;
return *this;
}
int length;
char num[MAX];
};
CHugeInt operator + (CHugeInt& a, int b)
{
return (a + CHugeInt(b));
}
CHugeInt operator + (int a, CHugeInt& b)
{
return (CHugeInt(a) + b);
}
CHugeInt operator + (CHugeInt& b1, CHugeInt& b2)
{
CHugeInt t;
int temp[MAX] = {0};
int length = b1.length > b2.length ? b1.length : b2.length;
for(int i = 0; i < length; i++)
{
temp[i] = ((i < b1.length) ? b1.num[i] - '0' : 0) + ((i < b2.length) ? b2.num[i] - '0' : 0);
}
for(int i = 0; i < length; i++)
{
if(temp[i] >= 10)
{
int ten = temp[i] / 10;
temp[i] %= 10;
temp[i + 1] += ten;
}
}
int i;
for(i = MAX - 1; i >= 0; i--)
{
if(temp[i] != 0)
{
break;
}
}
t.length = i + 1;
for(i = 0; i < t.length; i++)
{
t.num[i] = temp[i] + '0';
}
t.num[i] = 0;
return t;
}
ostream& operator << (ostream& output, CHugeInt& b)
{
for(int i = b.length - 1; i >= 0; i--)
{
cout << b.num[i];
}
return output;
}
int main()
{
CHugeInt a("1234545436342424354354365289899834234235");
CHugeInt d(9999);
CHugeInt temp = CHugeInt("100000088888888") + 111112;
CHugeInt temp2 = 111112 + CHugeInt("100000088888888");
cout << "1)" << temp << endl;
cout << "2)" << temp2 << endl;
cout << "3)" << ++d << endl;
cout << "4)" << d++ << endl;
cout << "5)" << d << endl;
d += 5;
cout << "6)" << d << endl;
cout << "7)" << d + temp << endl;
return 0;
}
HugeInt
最新推荐文章于 2020-09-20 21:26:49 发布