C++ 运算符重载 005:别叫,这个大整数已经很简化了!

本文介绍了一种实现大整数运算的方法,通过自定义类CHugeInt并重载运算符,包括加法、前置和后置递增,以及与整型变量的运算。示例代码展示了如何处理大整数与普通整数之间的加法运算,以及如何正确输出大整数的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

总时间限制: 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

while (cin >> s >> n) {         //s为大整数char ,n为小整数int 
	CHugeInt a(s);              // 复制构造函数——char*类型 
	CHugeInt b(n);              // 复制构造函数——int类型 

	cout << a + b << endl;        //重构加号 CHugeInt+CHugeInt
	cout << n + a << endl;        //重构加号 int +ChugeInt    
	cout << a + n << endl;        //重构加号 CHugeInt+int 
	b += n;                       // 重构+= 
	cout  << ++ b << endl;        //重构 ++b 
	cout << b++ << endl;          //重构 b++ 
	cout << b << endl;            //重构cout<< 

需要编写+重载, CHugeInt+CHugeInt
因为上面有int类型的复制构造函数,所以 CHugeInt+int 可以把int转换成CHugeInt再加

char类型的列表大整数相加——先反转,让个位在前,高位在后,
然后从前往后,将char类型转换为int相加,并记录进位,再转会char放入结果列表

重载Int+CHugeInt时要用友元,当全局函数处理。
重载cout<<时也如此

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
using namespace std;
const int MAX = 110; 
class CHugeInt {
//your code starts here	
	private:
		char buf[220];
	public:
		void reverse(char * p) {       //反转函数,成员为char* 
			int len = strlen(p);       //strlen,位数 
			int i = 0,j = len -1;      //底标 
			while(i <= j ) {          //前后反转 
				swap(p[i],p[j]);        
				++i;
				--j;
			}
		}
		CHugeInt(char * p) {         //构造函数(char *) 
			memset(buf,0,sizeof(buf));       //初始化buf 
			strcpy(buf,p);                   //copybuf 
			reverse(buf);                      //反转buf 
		}
		CHugeInt(int n) {             //构造函数(int) 
			memset(buf,0,sizeof(buf));       //初始化buf 
			sprintf(buf,"%d",n);             //sprintf:发送格式化输出到buf所指向的字符串 
			reverse(buf);                   //反转buf 
		}
		CHugeInt operator+(int n) {        //重构+ int 
			return * this + CHugeInt(n);     //返回当前函数+强制类型转换成CHugeInt的int 
		}
		CHugeInt operator +(const CHugeInt & n) const  {    //重构+CHugeInt 
			CHugeInt tmp(0);        //创建temp,构造int 0 
			int carry = 0;               //进位——一开始为0 
			for(int i = 0;i < 210 ; ++i) {   //每一位都进行运算    ,个位在前,多位在后 
				char c1 = buf[i];           
				char c2 = n.buf[i];      
				if( c1 == 0 && c2 == 0 && carry == 0)     //都为0则后续无数据输入——直接break 
					break;
				if( c1 == 0)         //初始化为0,将int转化为char 
					c1 = '0';                    
				if( c2 == 0)
					c2 = '0';                      
					
				int k = c1 - '0' + c2 - '0' + carry;     //c1-'0'为char转换int             
				if( k >= 10) {           
					carry = 1;     //进位+1 
					tmp.buf[i] = k - 10 + '0';        //两个char相加 
				}
				else {               
					carry = 0;
					tmp.buf[i] = k + '0';       //无进位,直接相加 
				}
			}
			return tmp;             //temp为CHugeInt对象 
		}
		friend CHugeInt operator +(int n,const CHugeInt & h)     //友元——int + CHugeInt 
		{
			return h+n;       //返回h+n 
		}
		friend ostream &  operator <<(ostream & o,const CHugeInt & h) {   //友元——cout<<重载 
			int len = strlen(h.buf);           //h的位数 
			for(int i = len -1 ; i >= 0; -- i) // 从后往前输出 
				cout << h.buf[i];
			return o;                //返回cout<< 
		}
		CHugeInt & operator += (int n) {       //重载+= 
			* this = * this  + n;            //利用上面的CHugeInt + int 完成 
			return * this;                  //+=对象+n,返回对象 
		}
		CHugeInt & operator ++() {          //++自己 
			* this = * this + 1;
			return * this;
		}
		CHugeInt operator ++(int ) {        //++0 
			CHugeInt tmp(*this);             //创建temp保存当前数字 
			* this = tmp + 1;                //对象+1 
			return tmp;                    //返回tmep 
		}
//your code ends here			
};
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、付费专栏及课程。

余额充值