大数模拟类

#include 

using namespace std;
const int maxn=10000;//最大长度

//高精度整数
struct BigInteger{
    //数组可以用vector,就不需要分配了
    //构造函数先resize(maxn),然后保留length作为精度
	int digit[maxn];	//存储数组
	int length;			//长度(精度)

	//构造
	BigInteger();
	BigInteger(int x);  //如果是0也要保留一个数字,len=1
	BigInteger(string str);
	BigInteger(const BigInteger& b);

	//赋值
	BigInteger operator=(int x);
	BigInteger operator=(string str);
	BigInteger operator=(const BigInteger& b);

	//比较
	bool operator<=(const BigInteger& b);
	bool operator==(const BigInteger& b);

	//运算
	BigInteger operator+(const BigInteger& b);
	BigInteger operator-(const BigInteger& b);
	BigInteger operator*(const BigInteger& b);
	BigInteger operator/(const BigInteger& b);
	BigInteger operator%(const BigInteger& b);

	//输入输出
	friend istream& operator>>(istream& in,BigInteger& b);
	friend ostream& operator<<(ostream& out,BigInteger& b);
};

BigInteger::BigInteger(){ //默认构造函数
	memset(digit,0,sizeof(digit));
	length=0;
}
BigInteger::BigInteger(int x){ //整型构造函数
	memset(digit,0,sizeof(digit));
	length=0;
	if(!x) digit[length++]=x;
	while(x){
		digit[length++]=x%10;
		x/=10;
	}
}
BigInteger::BigInteger(string str){	//字符串构造函数
	memset(digit,0,sizeof(digit));
	length=str.size();
	for(int i=0;i<length;i++){
		digit[i]=str[length-i-1]-'0';
	}
}
BigInteger::BigInteger(const BigInteger& b){ //高精度整型构造函数
	memset(digit,0,sizeof(digit));
	length=b.length;
	for(int i=0;i<length;i++){
		digit[i]=b.digit[i];
	}
}

BigInteger BigInteger::operator=(int x){ //整型赋值
	memset(digit,0,sizeof(digit));
	length=0;
	if(!x){
		digit[length++]=x;
	}
	while(x){
		digit[length++]=x%10;
		x/=10;
	}
	return *this;
}
BigInteger BigInteger::operator=(string str){	//字符串赋值
	memset(digit,0,sizeof(digit));
	length=str.size();
	for(int i=0;i<length;i++){
		digit[i]=str[length-i-1]-'0';
	}
	return *this;
}
BigInteger BigInteger::operator=(const BigInteger& b){ //高精度整数赋值
	memset(digit,0,sizeof(digit));
	length=b.length;
	for(int i=0;i<length;i++){
		digit[i]=b.digit[i];
	}
	return *this;
}

bool BigInteger::operator<=(const BigInteger& b){ //小于等于
	if(length<b.length) {
		return true;
	}
	else if(b.length<length) {
		return false;
	}
	else{
		for(int i=length-1;i>=0;i--){
			if(digit[i]==b.digit[i]) {
				continue;
			}
			else{
				return digit[i]<b.digit[i];
			}
		}
	}
	return true;
}
bool BigInteger::operator==(const BigInteger& b){ //等于
	if(length!=b.length)
		return false;
	for(int i=0;i<length;i++){
		if(digit[i]!=b.digit[i])
			return false;
	}
	return true;
}

BigInteger	BigInteger::operator+(const BigInteger& b){	 //加法运算
	int flag=0,temp;
	BigInteger ans;
	for(int i=0;i<length || i<b.length;i++){
		temp=digit[i]+b.digit[i]+flag;
		ans.digit[ans.length++]=temp%10;
		flag=temp/10;
	}
	if(flag){	//别忘了加最后的进位carry
		ans.digit[length++]=flag;
	}
	return ans;
}
BigInteger	BigInteger::operator-(const BigInteger& b){	 //减法运算(大数减小数)
	BigInteger ans;
	int flag=0,temp;
	for(int i=0;i<length || i<b.length;i++){
		temp=digit[i]-b.digit[i]-flag;
		if(temp<0){
			temp+=10;
			flag=1;
		}
		else{
			flag=0;
		}
		ans.digit[ans.length++]=temp;
	}
	while(ans.digit[ans.length-1]==0 && length>1){
		ans.length--;
	}
	return ans;
}
BigInteger	BigInteger::operator*(const BigInteger& b){	 //乘法运算
	BigInteger ans;
	ans.length=length+b.length;
	for(int i=0;i<length;i++){
		for(int j=0;j<b.length;j++){
			ans.digit[i+j]+=digit[i]*b.digit[j];
		}
	}
	for(int i=0;i<ans.length;i++){
		ans.digit[i+1]+=ans.digit[i]/10;
		ans.digit[i]%=10;
	}
	while(ans.digit[ans.length-1]==0 && length>1){
		ans.length--;
	}
	return ans;
}
BigInteger	BigInteger::operator/(const BigInteger& b){	 //除法运算
	BigInteger remainder=0;
	BigInteger temp=b;
	BigInteger ans;
	ans.length=length;
	for(int i=length-1;i>=0;i--){

		if(!(remainder.digit[0]==0 && remainder.length==1)){
			for(int j=remainder.length-1;j>=0;j--){
				remainder.digit[j+1]=remainder.digit[j];
			}
			remainder.length++;
		}

		remainder.digit[0]=digit[i];
		while(temp <= remainder){
			remainder=remainder-temp;
			ans.digit[i]++;

		}
	}
	while(ans.digit[ans.length-1]==0 && ans.length>1){  //全0的话要保留一个0,因此不能>=1
		ans.length--;
	}
	return ans;
}
BigInteger	BigInteger::operator%(const BigInteger& b){ //模运算
	BigInteger remainder=0;
	BigInteger temp=b;
	for(int i=length-1;i>=0;i--){
		if(!(remainder.digit[0]==0 && remainder.length==1)){
			for(int j=remainder.length-1;j>=0;j--){
				remainder.digit[j+1]=remainder.digit[j];
			}
			remainder.length++;
		}
		remainder.digit[0]=digit[i];
		while(temp <= remainder){
			remainder=remainder-temp;
		}
	}
	return remainder;
}

istream& operator>>(istream& in,BigInteger& b){	 //输入
	string str;
	in>>str;
	b=str;
	return in;
}
ostream& operator<<(ostream& out,BigInteger& b){ //输出
	for(int i=b.length-1;i>=0;i--){
		out<<b.digit[i];
	}
	return out;
}
int main(){
	BigInteger res=BigInteger("1345")/BigInteger("5");
    cout << res << endl;
}

你可能感兴趣的:(程序设计语言,c++,算法,图论)