大整数 — 运算符重载

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

总时间限制: 1000ms 内存限制: 65536kB

描述

程序填空,输出指定结果

#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

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
using namespace std;
const int MAX = 110; 
class CHugeInt {
	char s[210];
	int len;
	public:
	CHugeInt(const char *a){
		memset(s,0,sizeof(s));
		len=strlen(a);
		for(int i=len-1;~i;--i)
			s[i]=a[len-i-1]-'0';
	}
	CHugeInt(int a){
		memset(s,0,sizeof(s));
		len=0;
		while(a){
			s[len++]=a%10;
			a/=10;
		}
	}
	friend ostream & operator <<(ostream &o,const CHugeInt &a){
		for(int i=a.len-1;~i;--i)
			putchar(a.s[i]+'0');
		return o;
	}
	CHugeInt operator +(const CHugeInt &a){
		CHugeInt tp(0);
		tp.len=max(len,a.len);
		for(int i=0;i<tp.len;++i){
			tp.s[i]+=s[i]+a.s[i];
			if(tp.s[i]>9) tp.s[i]-=10,++tp.s[i+1];
		}
		if(tp.s[tp.len]) ++tp.len;
		return tp;
	}
	friend CHugeInt operator +(const int &z,const CHugeInt &a){
		CHugeInt tp(0),b(z);
		tp.len=max(b.len,a.len);
		for(int i=0;i<tp.len;++i){
			tp.s[i]+=b.s[i]+a.s[i];
			if(tp.s[i]>9) tp.s[i]-=10,++tp.s[i+1];
		}
		if(tp.s[tp.len]) ++tp.len;
		return tp;
	}
	CHugeInt & operator +=(const int &z){
		CHugeInt b(z);
		len=max(len,b.len);
		for(int i=0;i<len;++i){
			s[i]+=b.s[i];
			if(s[i]>9) s[i]-=10,++s[i+1];
		}
		if(s[len]) ++len;
		return *this;
	}
	CHugeInt & operator ++(){
		int i=0;++s[0];
		while(s[i]>9) s[i]-=10,++s[++i];
		len=max(len,i+1);
		return *this;
	}
	CHugeInt operator ++(int){
		CHugeInt tp(*this);
		int i=0;++s[0];
		while(s[i]>9) s[i]-=10,++s[++i];
		len=max(len,i+1);
		return tp;
	}
// 在此处补充你的代码
};
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;
}

评论

还没有任何评论,你来说两句吧

发表评论

衫小寨 出品