设计实现"HugeInteger"大整数类,实现大整数的加法,输入和输出功能。能够满足以下程序的运行
#include<iostream>
#include<array>
#include<string>
using namespace std;
class HugeInteger
{
private:
int n[101];
public:
HugeInteger();
HugeInteger(int n1);
HugeInteger(string n1);
void printa();
friend ostream& operator<<(ostream& os, const HugeInteger& n1);
HugeInteger operator+ (const HugeInteger& n2)
{
HugeInteger temp;
int temp1 = 0;
int temp2 = 0;
int judge = 0;
for (int i = 100; i >= 0; i--)
{
temp1 = this->n[i] + n2.n[i] +temp2;
judge = temp1 / 10;
if (judge == 0)
{
temp2 = 0;
}
else
{
temp2 = 1;
}
temp.n[i] = temp1 % 10;
}
return temp;
}
HugeInteger operator+ (const int& n2)
{
HugeInteger temp;
int temp1 = 0;
int temp2 = 0;
int temp3 = 1;
int judge = 0;
for (int i = 100; i >= 0; i--)
{
if ((n2 / temp3 % 10) != 0)
{
temp1 = this->n[i] + (n2 / temp3 % 10) +temp2;
temp3 *= 10;
judge = temp1 / 10;
if (judge == 0)
{
temp2 = 0;
}
else
{
temp2 = 1;
}
temp.n[i] = temp1 % 10;
}
else
{
if (temp2 == 1)
{
temp.n[i] = this->n[i] + 1;
temp2 = 0;
}
else
{
temp.n[i] = this->n[i];
}
}
}
return temp;
}
HugeInteger operator+ (const string& n3)
{
HugeInteger temp;
int temp1 = 0;
int temp2 = 0;
int judge = 0;
int n2[101] = { 0 };
int len = n3.length() - 1, len1 = n3.length() - 1;
for (int i = 100; i >= 100 - len1; i--)
{
n2[i] = int(n3[len]) - 48;
len = len - 1;
}
for (int i = 100; i >= 0; i--)
{
temp1 = this->n[i] + n2[i] + temp2;
judge = temp1 / 10;
if (judge == 0)
{
temp2 = 0;
}
else
{
temp2 = 1;
}
temp.n[i] = temp1 % 10;
}
return temp;
}
};
HugeInteger::HugeInteger()
{
for (int i = 0; i <=100; i++)
{
this->n[i] = 0;
}
}
HugeInteger::HugeInteger(int n1)
{
for (int i = 0; i <= 100; i++)
{
this->n[i] = 0;
}
int temp = 1;
for (int i = 100;(n1/temp)!=0; i--)
{
this->n[i] = n1/temp % 10;
temp *= 10;
}
}
ostream& operator<<(ostream& os,const HugeInteger& n1)
{
int judge = 0;
for (int i = 0; i <= 100; i++)
{
if (n1.n[i] == 0 && judge == 0)
{
continue;
}
else
judge = 1;
if (judge == 1)
{
os << n1.n[i];
}
}
if (judge == 0)
{
os << 0;
}
return os;
}
HugeInteger::HugeInteger(string n1)
{
for (int i = 0; i <= 100; i++)
{
this->n[i] = 0;
}
int temp = 1;
int len=n1.length() - 1 , len1 = n1.length() - 1 ;
for (int i = 100; i >= 100 - len1; i--)
{
this->n[i] = int(n1[len]) - 48;
len = len - 1;
}
}
void HugeInteger::printa()
{
int judge = 0;
for (int i = 0; i <= 100; i++)
{
if(this->n[i] == 0 && judge == 0)
{
continue;
}
else
judge = 1;
if (judge == 1)
{
cout << n[i];
}
}
if (judge == 0)
{
cout << 0;
}
cout << endl;
}
int main()
{
HugeInteger n1(7654321);
HugeInteger n2(7891234);
HugeInteger n3("99999999999999999999999999999");
HugeInteger n4("1");
HugeInteger n5;
HugeInteger n6("1234567");
cout << "n1 is " << n1 << "\nn2 is " << n2
<< "\nn3 is " << n3 << "\nn4 is " << n4
<< "\nn5 is " << n5 << "\n\n";
n5 = n1 + n2;
cout << n1 << " + " << n2 << " = " << n5 << "\n\n";
cout << n3 << " + " << n4 << "\n= " << (n3 + n4) << "\n\n";
n5 = n1 + 9;
cout << n1 << " + " << 9 << " = " << n5 << "\n\n";
n5 = n2 + "10000";
cout << n2 << " + " << "10000" << " = " << n5 << endl;
return 0;
}
|