我在VS2022上尝试定义了一个大整数类,使用了int*指针和string,并且使用了动态内存
#include<iostream>
#include<string>
using namespace std;
class HugeInteger
{
public:
HugeInteger(long n);
HugeInteger(string str);
HugeInteger();
HugeInteger(int* int_p, string str_p, int lent);
~HugeInteger();
void display()
{
cout << len << endl;
}
HugeInteger operator+(HugeInteger one_huge);
HugeInteger operator+(long ln);
HugeInteger operator+(string sn);
HugeInteger operator=(HugeInteger a_huge);
friend istream& operator>>(istream& sin, HugeInteger& Huge);
friend ostream& operator<<(ostream& sout, const HugeInteger& Huge);
private:
int* int_huge; //存储大整数
string str_huge; //暂时存储字符型的大整数
int len; //大整数的位数
};
HugeInteger::HugeInteger(long n) //参数为整数型构造函数
{
int i = 0;
long temp = n;
while (temp)
{
temp /= 10;
i++;
}
temp = n;
int_huge = new int[i];
for (int j = 0; j < i; j++)
{
int_huge[j] = temp%10;
temp /= 10;
}
len = str_huge.size();
}
HugeInteger::HugeInteger(string str) //参数为字符串型构造函数
{
int_huge = new int[str.size()];
str_huge = str;
for (int k = 0; k < str.size(); k++)
{
int_huge[k] = int(str[str.size() - 1 ]-48);
cout << int_huge[k ];
k++;
}
len = str.size();
}
HugeInteger::HugeInteger() //默认构造函数
{
str_huge = "\0";
int_huge = new int[1]();
len = 1;
}
HugeInteger::HugeInteger(int* int_p, string str_p, int lent)//全参构造函数
{
len = lent;
int_huge = new int[lent];
for (int t = 0; t < lent; t++)
{
int_huge[t] = int_p[t];
}
str_huge = str_p;
}
HugeInteger::~HugeInteger() //析构函数
{
delete[]int_huge;
int_huge = 0;
str_huge = "\0";
len = 0;
}
HugeInteger HugeInteger:: operator+(HugeInteger one_huge)
{
int lenth = len > one_huge.len ? len : one_huge.len;
int* add = new int[lenth + 1];//存储相加后的结果
int x = 0;//存储上一位的进位
int j = 0;//表示运算到哪一位
for (j = 0; j < lenth; j++)
{
add[j] = int_huge[j] + one_huge.int_huge[j] + x;//两数相加,并加上上一次的进位
x = add[j] / 10;
add[j] %= 10;
}
HugeInteger huge(add, "\0", j);
delete[]add;
return huge;
}
HugeInteger HugeInteger:: operator=(HugeInteger a_huge)
{
if (this != & a_huge)
{
if (int_huge)
{
delete[] int_huge;
}
int_huge = new int[a_huge.len ];
for (int i = 0; i < a_huge.len; i++)
{
int_huge[i] = a_huge.int_huge[i];
}
len = a_huge.len;
str_huge = a_huge.str_huge;
}
return *this;
}
HugeInteger HugeInteger::operator+(long ln)
{
HugeInteger asdf(ln);
*this = *this + asdf;
return *this;
}
HugeInteger HugeInteger::operator+(string sn)
{
HugeInteger qwer(sn);
*this = *this + qwer;
return *this;
}
istream& operator>>(istream& sin, HugeInteger& Huge)
{
string s;
sin >> s;;
HugeInteger _a(s);
Huge = _a;
return sin;
}
ostream& operator<<(ostream& sout, const HugeInteger& Huge)
{
for(int jk=0;jk<Huge.len;jk++)
{
sout << Huge.int_huge[Huge.len - 1 - jk];
}
return sout;
}
int main()
{
HugeInteger n1(7654321);
HugeInteger n2(7891234);
HugeInteger n3("99999999999999999999999999999");
HugeInteger n4("1");
HugeInteger n5;
n1.display();
n2.display();
n3.display();
n4.display();
n5.display();
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;
}
运行结果:
99999999999999910
0
29
1
1
n1 is
n2 is
n3 is 9-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519
n4 is 1
n5 is 0
报了个错:
我在调试中发现,如果想打印n3的每一位数,只会打印到第14个数。
请问,这该怎么修改?