大数相加 vs里正常运行产生正确结果 但在devc++就崩了求解
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
public:
char *cs;
int cn;
CHugeInt(char _s[])
{
if (cs != NULL) delete[] cs;
cs = new char[strlen(_s) + 1];
strcpy(cs, _s);
}
CHugeInt(int _n) :cn(_n) {}
friend char* operator+(CHugeInt &a1, CHugeInt &b1)
{
int a2[210];
int b2[210];
char b2_str[20];
int sum[210];
int lena = strlen(a1.cs);
for (int i = 0; i<lena; i++)
{
a2[i] = a1.cs[i] - '0';
}
_itoa(b1.cn, b2_str, 10);
int lenb = strlen(b2_str);
for (int i = lena - 1; i >= 0; i--)
{
if (lenb <= 0)
{
lenb = 0;
b2[i] = 0;
}
else
b2[i] = b2_str[--lenb] - '0';
}
int jinwei = 0;
for (int i = lena - 1; i >= 0; i--)
{
sum[i] = (a2[i] + b2[i] + jinwei) % 10;
jinwei = (a2[i] + b2[i] + jinwei) / 10;
}
if (jinwei)
{
for (int i = lena; i>0; i--)
sum[i] = sum[i - 1];
sum[0] = 1;
};
char *str=new char[210];
char str[210];
for (int i = 0; i<lena + 1; i++)
{
str[i] = sum[i] + '0';
}
str[lena + 1] = '\0';
return str;
}
~CHugeInt()
{
delete[]cs;
}
// 在此处补充你的代码
};
int main()
{
char s[210];
int n;
while (cin >> s >> n) {
CHugeInt a(s);
CHugeInt b(n);
cout << a + b << endl;
// cout << n + a << endl;
// cout << a + n << endl;
// b += n;
// cout << ++ b << endl;
// cout << b++ << endl;
// cout << b << endl;
}
return 0;
}