C++ 实现大数加法类

这里运算符的重载比较难

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

  • 查看
  • 提交
  • 统计
  • 提问

总时间限制: 

1000ms

 

内存限制: 

65536kB

// 在此处补充你的代码

描述

程序填空,输出指定结果

#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

来源

Guo Wei

 

#include  
#include  
#include  
#include  
using namespace std;
const int MAX = 110; 
class CHugeInt {
	// 在此处补充你的代码
private:
	char c[200];

public:
	// 将字符串反转
	void reverse(char *a)
	{
		int i = 0, j = strlen(a) - 1;
		while (i < j)
		{
			swap(a[i], a[j]);
			i++;
			j--;
		}
	}
	CHugeInt(const char* a)
	{
		int len;
		memset(c, 0, 200); // 把c全部置为0
		strcpy(c, a);
		len = strlen(c);
		reverse(c);
	}
	CHugeInt(int n)
	{
		int len;
		memset(c, 0, 200); // 把c全部置为0
		sprintf(c, "%d", n);//把n转换为char存在c
		len = strlen(c);
		reverse(c);
	}
	CHugeInt operator +(const CHugeInt &b)const
	{
		CHugeInt a(0);
		int lenb = strlen(b.c);
		int len = strlen(c);
		int lena;
		if (b.c[0] == '0' && lenb == 1)
		{
			// b为0的情况
			return *this;
		}
		if (c[0] == '0' && len == 1)
		{
			// a为0的情况
			strcpy(a.c, b.c);
			return a;
		}
		int j = 0;
		// 正常大数加法
		for (int i = 0; i < len || i < lenb; i++)
		{
			int x = 0;
			char c1 = c[i], c2 = b.c[i];
			if (c[i] == 0)
				c1 = '0';                 // 注意前面处理中有的字符串位是用数字0填充的
			if (b.c[i] == 0)
				c2 = '0';
			x = c1 - '0' + c2 - '0' + j;  // j是进位
			a.c[i] = (x % 10) + '0';      // 处理进位
			j = x / 10;                   // j是进位
		}
		// 将字符串长度较大值,给新的字符串
		lena = len > lenb ? len : lenb;
		// 如果还有进位
		if (j != 0)
		{
			int a0 = len > lenb ? len : lenb;
			a.c[a0] = j + '0';
			lena = a0 + 1;
		}
		return a;
	}

	CHugeInt operator +(int n)
	{
		return *this + CHugeInt(n);
	}

	friend CHugeInt operator +(int n, const CHugeInt &h) {
		// 这里用上面函数,传的引用
		return h + n;
	}

	CHugeInt& operator +=(int n) {
		*this = *this + CHugeInt(n);//直接调用默认复制构造函数,因为char c[200]是数组不是指针
		return *this;
	}

	friend CHugeInt& operator ++(CHugeInt &a) {
		a = a + 1;
		return a;
	}
	
	friend ostream& operator<<(ostream& o, const CHugeInt& a) 
	{	

		int len = strlen(a.c);
		for (int i = len-1; i >= 0; --i) 
		{			
			cout << a.c[i];		
		}		
		return o;	
	}

	friend CHugeInt operator ++(CHugeInt &a, int) {
		CHugeInt b = a;
		a = (b + 1);
		return b;
	}
};
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;
}

 

你可能感兴趣的:(算法)