#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;
}
If no error message is reported, basically no one will help you debug.
If no error message is reported, basically no one will help you debug.
If no error message is reported, basically no one will help you debug. .
Give you a piece of wisdom on asking questions
It is recommended that the questioner first describe the purpose of your program and what kind of errors you encounter when running it. For example, the INT class should be an implementation of BigInterger that uses string representation internally. During runtime, it is actually found that calling z.display() does not produce any output (the expectation is that the integer content should be printed out). This can save a lot of time for people who are really willing to help you, and it is also an exercise for yourself.
First you need to modify the expression of the string. In the memory, you should use the following method to store integers, so that it can be relatively simple to perform addition alignment:
The problem with the program lies in the implementation of the overloaded addition function.
First, when initializing the temp variable, the value of temp.len is not set correctly:
Secondly, you also have problems with the operation of each bit. It is not convenient for me to explain them one by one. Here is a sample implementation I wrote (note that in this implementation, string storage is in reverse order), please note Compare the differences between the two:
Of course, your display function implementation must also be modified accordingly:
rrreeeThe results of running the program are as follows:
rrreeeThe situation of
p_num == NULL
is not considered here