跳到正文

个人记录

高精度算法(重载运算符)(P党勿入)(新版)

caizehao约 9 分钟
原文

这是 2025年10月4日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2023年5月3日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

上回发了个高精度算法(运算符)(P党勿入)

估计很多人多说可以把重载运算符(不包括重载输入输出)那段放结构体里

今天

他来了

代码:

C++
#include <bits/stdc++.h>
using namespace std;
struct number{
	int num[10100];
	number & operator = (const char *c){
		memset(num,0,sizeof(num));
		int n=strlen(c),j=1,k=1;
		for(int i=1;i<=n;i++){
			if(k==10000){
				j++,k=1;
			}
			num[j]+=k*(c[n-i]-'0');
			k*=10;
		}
		num[0]=j;
		return *this;
	}
	number & operator = (int a){
		char s[10100];
		sprintf(s,"%d",a);
		return *this=s;
	}
	number(){
		memset(num,0,sizeof(num));
		num[0]=1;
	}
	number(int n){
		*this=n;
	}
	bool operator > (const number &b) const{
		if(num[0]!=b.num[0]){
			return num[0]>b.num[0];
		}
		for(int i=num[0];i>=1;i--){
			if(num[i]!=b.num[i]){
				return (num[i]>b.num[i]);
			}
		}
		return false;
	}
	bool operator < (const number &b) const {
		return b>*this;
	}
	bool operator <= (const number &b) const {
		return !(*this>b);
	}
	bool operator >= (const number &b) const {
		return !(b>*this);
	}
	bool operator != (const number &b) const {
		return (b>*this) || (*this>b);
	}
	bool operator == (const number &b) const {
		return !(b>*this) && !(*this>b);
	}
	number operator + (const number &b) const {
		number c;
		c.num[0]=max(num[0],b.num[0]);
		for (int i=1;i<=c.num[0];i++){
			c.num[i]+=num[i]+b.num[i];
			if(c.num[i]>=10000){
				c.num[i]-=10000;
				c.num[i+1]++;
			}
		}
		if (c.num[c.num[0]+1]>0){
			c.num[0]++;
		}
		return c;
	}
	number operator - (const number &b) const {
		number c;
		c.num[0]=num[0];
		if(*this<b){
			cout << '-'; 
			c=b-*this;
			return c;
		}
		for(int i=1;i<=c.num[0];i++){
			c.num[i]+=num[i]-b.num[i];
			if(c.num[i]<0){
				c.num[i]+=10000;
				c.num[i+1]--;
			}
		}
		while(c.num[c.num[0]]==0 && c.num[0]>1){
			c.num[0]--;
		}
		return c;
	}
	number operator * (const number &b) const{
		number c;
		c.num[0]=num[0]+b.num[0]+1;
		for (int i=1;i<=num[0];i++){
			for(int j=1;j<=b.num[0];j++){
				c.num[i+j-1]+=num[i]*b.num[j];
				c.num[i+j]+=c.num[i+j-1]/10000;
				c.num[i+j-1]%=10000;
			}
		}
		while(c.num[c.num[0]]==0 && c.num[0]>1){
			c.num[0]--;
		}
		return c;
	}
	number operator / (const number &b) const{
		number c,d;
		c.num[0]=num[0]+b.num[0]+1;
		d.num[0]=0;
		for (int i=num[0];i>=1;i--){
			memmove(d.num+2,d.num+1,sizeof(d.num)-sizeof(int)*2);
			d.num[0]++;
			d.num[1]=num[i];
			int left=0,right=9999,mid;
			while(left<right){
				mid=(left+right)/2;
				if(b*number(mid)<=d){
					left=mid+1;
				}else{
					right=mid;
				}
			}
			c.num[i]=right-1;
			d=d-b*number(right-1);
		}
		while(c.num[c.num[0]]==0 && c.num[0]>1){
			c.num[0]--;
		}
		return c;
	}
	number operator % (const number &b) const{
		number c,d;
		c.num[0]=num[0]+b.num[0]+1;
		d.num[0]=0;
		for (int i=num[0];i>=1;i--){
			memmove(d.num+2,d.num+1,sizeof(d.num)-sizeof(int)*2);
			d.num[0]++;
			d.num[1]=num[i];
			int left=0,right=9999,mid;
			while(left<right){
				mid=(left+right)/2;
				if(b*number(mid)<=d){
					left=mid+1;
				}else{
					right=mid;
				}
			}
			c.num[i]=right-1;
			d=d-b*number(right-1);
		}
		while(c.num[c.num[0]]==0 && c.num[0]>1){
			c.num[0]--;
		}
		return d;
	}
	number & operator += (const number &b){
		return *this=*this+b;
	}
	number & operator -= (const number &b){
		return *this=*this-b;
	}
	number & operator *= (const number &b){
		return *this=*this*b;
	}
	number & operator /= (const number &b){
		return *this=*this/b;
	}
	number & operator %= (const number &b){
		return *this=*this%b;
	}
};
istream & operator >> (istream &in,number &n){
	char s[10100];
	in >> s;
	n=s;
	return in;
}
ostream & operator << (ostream &out,number &n){
	out << n.num[n.num[0]];
	for(int i=n.num[0]-1;i>=1;i--){
		out.width(4);
		out.fill('0');
		out << n.num[i];
	}
	return out;
}
#define long number
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long a,b,c;
    cin >> a >> b;
    cout << a << '+' << b << '=';
    c=a+b;
    cout << c << "\n";//不能直接cout << a+b;!!!
    cout << a << '-' << b << '=';
    c=a-b;
    cout << c << "\n";
    c=a*b;
    cout << a << '*' << b << '=';
    cout << c << "\n";
    c=a/b;
    cout << a << '/' << b << '=';
    cout << c << "\n";
    c=a%b;
    cout << a << '%' << b << '='; 
    cout << c;
    return 0;
}