跳到正文

个人记录

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

caizehao约 10 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

想必高精度算法大家都是按普通的方法打的~~(别说你也是用重载运算符的)~~

但我是一般是用重载运算符的~~(你看标题就知道了)~~

俺滴代码:

C++
#include <bits/stdc++.h>
using namespace std;

struct number{
	int num[10100];
	number & operator = (const char*);
	number & operator = (int);
	number();
	number(int);
	bool operator > (const number &) const;
	bool operator < (const number &) const;
	bool operator <= (const number &) const;
	bool operator >= (const number &) const;
	bool operator != (const number &) const;
	bool operator == (const number &) const;
	number operator + (const number &) const;
	number operator - (const number &) const;
	number operator * (const number &) const;
	number operator / (const number &) const;
	number operator % (const number &) const;
	number & operator += (const number &);
	number & operator -= (const number &);
	number & operator *= (const number &);
	number & operator /= (const number &);
	number & operator %= (const number &);
};
number & 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 & number::operator = (int a){
	char s[10100];
	sprintf(s,"%d",a);
	return *this=s;
}
number::number(){
	memset(num,0,sizeof(num));
	num[0]=1;
}
number::number(int n){
	*this=n;
}
bool number::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 number::operator < (const number &b) const {
	return b>*this;
}
bool number::operator <= (const number &b) const {
	return !(*this>b);
}
bool number::operator >= (const number &b) const {
	return !(b>*this);
}
bool number::operator != (const number &b) const {
	return (b>*this) || (*this>b);
}
bool number::operator == (const number &b) const {
	return !(b>*this) && !(*this>b);
}
number 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 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 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 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 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 & number::operator += (const number &b){
	return *this=*this+b;
}
number & number::operator -= (const number &b){
	return *this=*this-b;
}
number & number::operator *= (const number &b){
	return *this=*this*b;
}
number & number::operator /= (const number &b){
	return *this=*this/b;
}
number & number::operator %= (const number &b){
	return *this=*this%b;
}
ostream & operator << (ostream &o,number &n){
	o << n.num[n.num[0]];
	for(int i=n.num[0]-1;i>=1;i--){
		o.width(4);
		o.fill('0');
		o << n.num[i];
	}
	return o;
}
istream & operator >> (istream &in,number &n){
	char s[10100];
	in >> s;
	n=s;
	return in;
}
#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;
}

别乱用!!!

原文
存档评论1 条抓取于