018:别叫,这个大整数已经很简化了!

018:别叫,这个大整数已经很简化了!
程序填空,输出指定结果

#include
#include
#include
#include
using namespace std;
const int MAX = 110;
class CHugeInt {
// 在此处补充你的代码
};
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;

}
输入
多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示
输出
对每组数据,输出6行,内容分别是:
样例输入
99999999999999999999999999888888888888888812345678901234567789 12
6 6
样例输出
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14

#include
#include
#include
#include
using namespace std;
const int MAX = 110;
class CHugeInt
{
private:
char st[300];
public:
void rever(char *s)
{
int i, j;
i = 0, j = strlen(s)-1;
//char c;
while(i < j)
{
swap(s[i], s[j]);
i++;
j–;
}
}
CHugeInt(char *s)
{
if(s)
{
memset(st, 0, sizeof(st));
strcpy(st, s);
rever(st);
}
}
CHugeInt(int n)
{
memset(st, 0, sizeof(st));
sprintf(st, “%d”, n);
rever(st);
}
CHugeInt operator + (const CHugeInt& a) const //有个疑问, 为啥一定要加const, 不加就报错?????
{
CHugeInt t(0);
int t1 = 0;
int i = 0, jinwei = 0;
while(st[i] || a.st[i] || jinwei) //大数相加的方法get
{
char x = st[i], y = a.st[i];
if(x == 0) x = ‘0’;
if(y == 0) y = ‘0’;
int k = x + y - ‘0’ - ‘0’ + jinwei;
if(k >= 10)
{
t.st[t1++] = k - 10 + ‘0’;
jinwei = 1;
}
else
{
t.st[t1++] = k + ‘0’;
jinwei = 0;
}
i++;
}
return t;
}
CHugeInt operator + (int x)
{
CHugeInt t(x);
return *this + t;
}
friend CHugeInt operator + (int x, const CHugeInt& a)
{
return a + x;
}
CHugeInt& operator ++() //前置
{
*this = *this + 1;
return *this;
}
CHugeInt operator ++ (int) //后置
{
CHugeInt t(*this);
*this = *this + 1;
return t;
}
CHugeInt& operator += (int n)
{
*this = *this + n;
return *this;
}
friend ostream& operator << (ostream& o, const CHugeInt& a)
{
int i;
for(i = strlen(a.st)-1; i >= 0; i–)
{
cout << a.st[i];
}
return o;
}
};
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;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值