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

描述

程序填空,输出指定结果

#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

代码实现

  1. CHguInt a(s);CHugeInt b(n);判断两个构造函数需要重载
  2. b += n;判断+=运算符需要重载
  3. b++;++b;判断前置和后置++运算符均需要重载
  4. <<输出运算符也需要重载为友元函数,前面的运算符重载后均返回CHugeInt对象,所以<<输出运算符只需要重载一个方法就可以了
  5. 为了方便大数组做运算,将数组进行倒置,尽快处理返回
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值