C++ 大整数类 存档 BUG逐渐改善 优化

本文介绍了C++中自定义大整数类的设计,包括构造函数、去0函数、数值比较、加减乘除等操作。讨论了如何优化万进制的除法,并提供了输入输出流重载的实现。作者欢迎读者提出改进BUG和优化建议。
摘要由CSDN通过智能技术生成

struct big 成员列表一览:

int ten[4]={1,10,100,1000};//构造万进制时用的系数
struct big{
	bool tag;//符号
	vector<int> d;//存储数值
	int base;//进制
	big();
	big(const big&);//复制构造函数
	big(const string&,const int&);//字符串构造函数,转换为相应的进制 。以POJ 1220 为标准
	big(int,const int&);//把int转换为相应的进制
	big(const string&);//默认的万进制
	void change_base(const int&);//转换为相应的进制
	void tail();//去0
	void operator=(const big&);//赋值
	void operator=(const string&);
	void operator=(const int&);
	void Dec();//数值加1
	void Inc();//数值减1
	big add(const big&)const;	//数值相加
	big minus(const big&)const;//数值上 大数减小数
	big mul(const big&)const;	//数值相乘,顺便处理符号
	void operator+=(const int&);//与int型的 加和减 运算都转换为big大整数类再运算,因为符号难处理。。
	void operator-=(const int&);
	big operator+(const int&)const;
	big operator-(const int&)const;
	void operator*=(const int&);//与int的乘法也转换为big大整数类再运算,差不多
	big operator*(const int&)const;
	void operator+=(const big&);//基于 add() 和 minus() 的加,减运算
	big operator+(const big&)const;
	void operator-=(const big&);
	big operator-(const big&)const;
	big operator*(const big&)const;//朴素乘法
	void operator*=(const big&);
	//friend smaller(const big&,const big&,const int&);//除法用到		//outside of the struct
	//friend Minus(big&,const big&,const int&);	//除法用到				//outside of the struct
	void dou(const big&);//本质为*this=parameter*2; //不用转换为big再乘,除法用到
	big operator/(const int&)const;//除法    !!!限于万进制的big
	big operator/(const big&)const;
	big operator%(const big&)const;//取余   基于 除法,减法,乘法 ,懒---
	big operator%(const int&)const;
	friend istream&operator>>(istream&,big&);
	friend ostream&operator<<(ostream&,const big&);//输出万进制的话需要#include<iomanip>
	void operator++();//自增   有点多余
	void operator--();//自减
	int compare(const big&)const;//数值比较函数  左边大于右边 返回 -1   ,等于放回 0,小于返回 -1
	bool operator<(const big&)const;//基于 compare() 的比较运算符重载
	bool operator>(const big&)const;
	bool operator==(const big&)const;
	bool operator>=(const big&)const;
	bool operator<=(const big&)const;
	bool operator!=(const big&)const;
}mid[14];//除法用到,存储除数的(2次幂)倍数,做减法,模拟竖式


构造函数:

big::big(){tag=0;base=1e4;}
big::big(const big&a){
	base=a.base;
	d=a.d;
	tag=a.tag;
}
big::big(const string&s,const int&b){
	if(s[0]=='-')tag=1;else tag=0;
	base=b;
	int e;
	if(tag) e=1;else e=0;
	int len=s.size();
	for(int i=len-1;i>=e;i--){
		if(s[i]<='9') d.push_back(s[i]-48);
		else if(s[i]<=90) d.push_back(s[i]-55);
		else
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值