c++编程练习 018:别叫,这个大整数已经很简化了!

北大程序设计与算法(三)测验题汇总(2020春季)


描述

程序填空,输出指定结果

#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


分析

  1. 构造函数必要的
CHugeInt a(s);
CHugeInt b(n);
  1. +重载
cout << a + b << endl;
cout << n + a << endl;
cout << a + n << endl;
  1. +=重载
b += n;
  1. 前置与后置++重载
cout  << ++ b << endl;
cout << b++ << endl;
  1. <<重载
cout << b << endl;

解决方案

class CHugeInt {
private:
	char name[210];
public:
	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); 
		memset(name,'\0',sizeof(name));
		strcpy(name,_name);
		reverse(name);
	}
	CHugeInt(int _n){//CHugeInt b(n); 
		memset(name,'\0',sizeof(name));
		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 & cur){
		CHugeInt temp(0);
		int carry = 0;
		for(int i = 0;i < 210;i++)
		{
			char c1 = name[i];
			char c2 = cur.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;
	}
};

注意:

  1. 在实现两个数相加时,因为此处是大整数,那么需要用大整数相加的思想,将前后倒置,便于进位处理;
  2. 因为每一个输出都是对象,那么我们可以将每一个运算符的重载后的返回对象设置为对象,那么最后只需要<<重载即可输出数据;
  3. 当声明在类的外部时,则参数列表为2个参数,所以需要声明为友元,便于访问类的对象的数据。
发布了196 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44116998/article/details/104371709
今日推荐