#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;
}
大家讲道理2017-04-17 13:34:52
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
PHP中文网2017-04-17 13:34:52
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:
INT temp;
// initialize temp INT object
temp.len = x1.len + 1; // you don't need to add x1 and x2 size, actually just the large one's length + 1 is enough
temp.p_num = new char[temp.len];
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:
INT operator+(const INT& x1, const INT& x2)
{
if(x1.len < x2.len) {
return x2 + x1;
}
INT temp;
// initialize temp INT object
temp.len = x1.len + 1; // save for extra increment
temp.p_num = new char[temp.len];
/* replace this loop with a memset call to be more efficient and readable
for(int i = temp.len - 1; i >= 0; i--) {
temp.p_num[i] = '0';
}*/
memset(temp.p_num, '0', temp.len);
temp.p_num[temp.len - 1] = 'void display() const
{
for(int i = len - 2; i >= 0; --i)
if (p_num[i] != '123456801
')
cout << p_num[i];
cout << endl;
}
';
for(int i = 0; i < x1.len - 1; ++i) {
temp.p_num[i] += (x1.p_num[i] - '0') +
((i < x2.len - 1) ? (x2.p_num[i] - '0') : 0);
if(temp.p_num[i] - '0' >= 10) {
temp.p_num[i] -= 10;
temp.p_num[i + 1] += 1;
}
}
// truncate extra byte of 0
if (temp.p_num[temp.len - 2] == '0')
temp.p_num[temp.len - 2] = 'rrreee';
return temp;
}
Of course, your display function implementation must also be modified accordingly:
rrreeeThe results of running the program are as follows:
rrreeeringa_lee2017-04-17 13:34:52
//赋值函数
INT & operator=(const INT &s)
{
delete[] p_num;
//...
The situation of p_num == NULL
is not considered here