#include<iostream>
#include<cstring>
using namespace std;
class INT
{
char *p_num;
int len;
public:
//构造函数
INT()
{
p_num = NULL; len = 0;
}
INT(const char *p)
{
len = strlen(p)+1;
p_num=new char[strlen(p)+1];
strcpy(p_num,p);
}//拷贝函数
INT(const INT &s)
{
len = s.len;
p_num=new char[strlen(s.p_num)+1];
strcpy(p_num,s.p_num);
}
//赋值函数
INT & operator=(const INT &s)
{
delete[]p_num;
len = s.len;
p_num = new char[strlen(s.p_num) + 1];
strcpy(p_num, s.p_num);
return *this;
}
//析构函数
~INT()
{
delete []p_num;
p_num=NULL;
len = 0;
}
//下面三个重载函数实现INT型与int型混合运算
friend INT operator+(const INT &x1, const INT &x2);
friend INT operator+(const INT &x, int y);
friend INT operator+(int y, const INT &x);
//显示数据
void display()const
{
for (int i = 0; i <len; i++)
cout << p_num[i];
cout << endl;
}
};
INT operator+(const INT &x1, const INT &x2)
{
INT temp;
temp.p_num = new char[x1.len+x2.len+2];
if (x1.len>=x2.len)
{
for (int i = temp.len-1; i >= 0; i--) { temp.p_num[i] = '0'; }
for (int i = x2.len-1; i >= 0; i--)
{
temp.p_num[i] = temp.p_num[i] + x1.p_num[i] + x2.p_num[i] - '0';
if (temp.p_num[i] - '0' > 10)
{
temp.p_num[i] -= 10; temp.p_num[i - 1] += 1;
}
}
for (int i = temp.len-x2.len-1; i >= 0; i--)
{
temp.p_num[i] +=x1.p_num[i];
}
}
else
{
for (int i = temp.len-1; i >= 0; i--) { temp.p_num[i] = '0'; }
for (int i = x1.len-1; i >= 0; i--)
{
temp.p_num[i] = temp.p_num[i] + x1.p_num[i] + x2.p_num[i] - '0';
if (temp.p_num[i] - '0' > 10)
{
temp.p_num[i] -= 10; temp.p_num[i - 1] += 1;
}
}
for (int i = temp.len-1; i >= 0; i--)
{
temp.p_num[i] += x2.p_num[i];
}
}
return temp;
}
int main()
{
INT x,y,z;
x = "123456789";
y = "12";
z=x+y;
z.display();
system("pause");
return 0;
}
如果没有报错信息, 基本上没人会帮你debug.
如果没有报错信息, 基本上没人会帮你debug.
如果没有报错信息, 基本上没人会帮你debug.
送你一篇提问的智慧