018:别叫,这个大整数已经很简化了!
程序员文章站
2022-03-14 09:58:33
...
题目:
程序填空,输出指定结果
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
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
来源
Guo Wei
分析:
CHugeInt a(s); //构造函数1
CHugeInt b(n); //重载构造函数2
cout << a + b << endl; //重载"+",类 + 类
cout << n + a << endl; //数字 + 类
cout << a + n << endl; //类 + 数字
b += n; //重载 "+="
cout << ++ b << endl; //重载前置++
cout << b++ << endl; //重载后置++
cout << b << endl; //重载"<<"
完整代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
// 在此处补充你的代码
private:
//char * HugeInt;//题目明确数据最大为(200+1)位,此处用数组更好
char HugeInt[210];
public:
void reverse(char * _HugeInt){ //将数据头尾反转,便于进位处理
int len = strlen(_HugeInt);
int i = 0, j = len - 1;
while(i <= j){
swap(_HugeInt[i],_HugeInt[j] );
++i;
--j;
}
}
//构造函数1
CHugeInt(const char * _HugeInt){
memset(HugeInt,'\0',sizeof(HugeInt));
strcpy(HugeInt,_HugeInt);
reverse(HugeInt);
}
//构造函数2
CHugeInt(const int _n){
memset(HugeInt,'\0',sizeof(HugeInt));
sprintf(HugeInt,"%d",_n);
reverse(HugeInt);
}
//满足计算:类 + num (cout << a + n << endl;)
CHugeInt operator+(int _n) {
return *this + CHugeInt(_n); //将(类 + num) 转变成 (类 + 类)。
}
//满足计算:num +类 (cout << n + a << endl;)
friend CHugeInt operator+(int _n, CHugeInt & _HugeInt){
//return _HugeInt + _n;
return _HugeInt + CHugeInt(_n);
}
//满足计算:类 + 类 (cout << a + b << endl;)
CHugeInt operator+(const CHugeInt & _HugeInt){
CHugeInt temp(0);
int carry = 0;
for(int i = 0; i < 210 ;i++){
char c1 = HugeInt[i];
char c2 = _HugeInt.HugeInt[i];
if(c1 == 0 && c2 == 0 && carry == 0) break;
//c1 == 0 而不是 c1 == '0' 是判断结束符'\0' !
if( c1 == 0)
c1 = '0';
if( c2 == 0)
c2 = '0';
int k = c1 -'0' + c2 - '0' + carry;
if(k > 9){
carry = 1;
k = k % 10;
}
else carry = 0;
temp.HugeInt[i] = k + '0';
}
return temp;
}
//重载"+="b += n;
CHugeInt operator+=(int n){
*this = *this + CHugeInt(n); //将 b += n 变成 b = b + n;
return *this;
}
//重载前置++,为成员函数即可。
CHugeInt operator++(){
*this = *this + CHugeInt(1);
return *this;
}
//重载后置++,多一个无用int参数,
CHugeInt operator++(int){
CHugeInt tmp(*this); //注意!!!后置++是要返回进行加法之前的变量的!!!
* this = tmp + CHugeInt(1);
return tmp;
}
//重载"<<"
friend ostream & operator<<(ostream & os, const CHugeInt & _HugeInt){
int len = strlen(_HugeInt.HugeInt);
for(int i = len - 1; i >= 0 ; i--){
os << _HugeInt.HugeInt[i];
}
return os;
}
};
int main()
{
char s[210];
int n;
while (cin >> s >> n) {
CHugeInt a(s); //构造函数1
CHugeInt b(n); //重载构造函数2
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;
}
此题主要借鉴:https://blog.csdn.net/qq_44116998/article/details/104371709 这个博主写得真好。
下一篇: 015:看上去好坑的运算符重载
推荐阅读