描述
程序填空,输出指定结果
#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行,内容对应程序中6个输出语句
样例输入
99999999999999999999999999888888888888888812345678901234567789 12 6 6
样例输出
99999999999999999999999999888888888888888812345678901234567801 99999999999999999999999999888888888888888812345678901234567801 99999999999999999999999999888888888888888812345678901234567801 25 25 26 12 12 12 13 13 14
代码实现
- CHguInt a(s);CHugeInt b(n);判断两个构造函数需要重载
- b += n;判断+=运算符需要重载
- b++;++b;判断前置和后置++运算符均需要重载
- <<输出运算符也需要重载为友元函数,前面的运算符重载后均返回CHugeInt对象,所以<<输出运算符只需要重载一个方法就可以了
- 为了方便大数组做运算,将数组进行倒置,尽快处理返回
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
private:
char name[210];
public:
//将数据头尾反转,便于进位处理a{123456}-->a{654321}
void reverse(char *_name){
int len = strlen(_name);
int i = 0,j = len - 1;
while(i <= j){
swap(_name[i],_name[j]);
i += 1;
j -= 1;
}
}
/*构造函数的重载*/
CHugeInt(char *_name){//CHugeInt a(s);
//sizeof()后跟数组名时,计算整个数组大小
memset(name,'\0',sizeof(name));
strcpy(name,_name);
reverse(name);
}
CHugeInt(int _n){//CHugeInt b(n);
memset(name,'\0',sizeof(name));
//格式化输出stdio.h头文件中,向字符串中写入数据
sprintf(name,"%d",_n);
reverse(name);
}
/*满足计算:类 + num*/ //cout << a + n << endl;
CHugeInt operator+ (int n){
return *this + CHugeInt(n);
}
/*满足计算:类 + 类*/ //cout << a + b << endl;
CHugeInt operator+ (const CHugeInt & next){
CHugeInt temp(0);//返回临时构造对象
int carry = 0;
for(int i = 0;i < 210;i++)
{
char c1 = name[i];
char c2 = next.name[i];
if(c1 == 0 && c2 == 0 && carry == 0)
break;
if( c1 == 0)
c1 = '0';
if( c2 == 0)
c2 = '0';
int k = c1 - '0' + c2 - '0' + carry;
if(k >= 10){//判断有进位
k = k % 10;
temp.name[i] = k + '0';
carry = 1;
}
else{//判断没有进位
carry = 0;
temp.name[i] = k + '0';
}
}
return temp;//返回对象
}
//cout << n + a << endl;
//当声明在类的外部时,则参数列表为2个参数,所以需要声明为友元,便于访问数据h
friend CHugeInt operator+ (int n,CHugeInt & h){
return h + n;
}
//cout << b << endl;
friend ostream & operator<< (ostream & o,const CHugeInt & h) {
int len = strlen(h.name);
//因为字符串时倒置的,需要从后往前输出
for(int i = len -1 ; i >= 0; -- i)
cout << h.name[i];
return o;
}
/*重载+=运算符*/ //b += n;
CHugeInt & operator+= (int n) {
* this = * this + n;//n创建临时对象
return * this;
}
/*重载前置运算符*/ //cout << ++ b << endl;
CHugeInt & operator ++() {
* this = * this + 1;//1创建临时对象
return * this;
}
/*重载后置运算符*/ //cout << b++ << endl;
CHugeInt operator ++(int) {
CHugeInt tmp(*this);
* this = tmp + 1;//1创建临时对象
return tmp;
}
};
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;
}