#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;
}
uj5u.com熱心網友回復:
devc++調式產生的中斷。 網上查了下 說的意思是對str空間回收了 所以訪問了未分配空間 不知道是不是這個原因 但vs沒問題啊 。如果是這個原因有什么方法解決嗎 求指點
uj5u.com熱心網友回復:
把這個去掉char str[210];for回圈上面那個uj5u.com熱心網友回復:
這個去掉char str[210];for回圈上面那個 沒啥用 還是不行uj5u.com熱心網友回復:
char *str=new char[210];char str[210];
定義兩個 str 變數, 能編譯通過?
uj5u.com熱心網友回復:
重定義了。用下面的就會回收,用上面的就暫時沒有問題,但要注意泄露。
char *str=new char[210];
char str[210];
uj5u.com熱心網友回復:
和重定義沒關系吧 我已經注釋了一個 還是不行 在網上查說是str 記憶體被回收 訪問了未分配空間 有什么辦法解決嗎uj5u.com熱心網友回復:
用第一個就不會被回收
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/50717.html
標籤:C++ 語言
下一篇:C語言指標
