跳到正文

算法·理论

用类写的高精度

MCbucket约 65 分钟
原文
文章信息与版本

文章信息

发布
2025年3月26日
收录
2026年9月7日
更新
2026年11月14日
篇幅
约 65 分钟

版本

  1. v1当前

前言、更新日记、作者の话

前言

此篇是用类写的高精度,两万七千字、一千四百四十五段落总结,不熟悉类的,请自行搜索类

更新日记

于2025-03-26发布洛谷版本,CSDN版

作者の话

麻烦点个赞好吗,作者做了好久


一、高精度基础

如果使用数组的化肯定会爆空间,并且用数组的话不方便,所以我们可以用字符指针数组字符串做,也就是string\color{#FF0000} \text string,用运算符重载更优哦,代码如下

C++
class zsgjd{//整数高精度
public:
	string value;//存储值
	const int Maxlen;//最大长度
}

疑问解答: Q1Q_1:为什么不用字符数组或字符指针? A1A_1:因为作者不会字符数组、指针占空间,也是开一个数组 再说了有string为什么不用 Q2Q_2:那个Maxlen有什么用? A2A_2:没用是后面开数组大小用的。


二、高精度初始化

作为一个班级类最重要的当然是初始化啦,不然你怎么赋值??? 直奔主题,上代码:

C++
	zsgjd():value("0"),Maxlen(100000){};//什么?你不赋值?那我帮你赋值
	zsgjd(string str):value(str),Maxlen(100000){};//这就对了嘛
	zsgjd(string str,int len):value(str),Maxlen(len){};
	zsgjd(char c[]):Maxlen(100000){
		this->value=c;//this是一重保险
	}
	zsgjd(char c[],int len):Maxlen(len){
		this->value=c;
	}

就这些破代码废了作者一( )的时间

奥对了赋值也顺便写一下:

C++
	void operator=(const zsgjd &sz){
		this->value=sz.value;
	}

三、高精度比较

1.分析

很简单的字符串比较 因为是字符串,所以只需要字符串比较…吗?

  1. 字符串的位数比较不一样
  2. TMD还有负数啊!

所以,我们应该先判断它的正负,再判断位数

2.等于の情况

也是肥肠的简单啊,判断是否一样就行了

C++
	bool operator==(const zsgjd &sz){
		if(this->value==sz.value)return true;
		return false;
	}

3.不等于の情况

也是肥肠的简单啊,判断是否不一样就行了 这句话好像说过,又好像没说过

C++
	bool operator!=(const zsgjd &sz){
		if(this->value!=sz.value)return true;
		return false;
	}

4.大于等于の情况

首先判断两个的正负,再判断位数,最后判断数值大小

C++
	bool operator>=(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){//两个都是负数的话
			if(this->value.size()<sz.value.size())return true;//位数小就返回是
			else if(this->value.size()>sz.value.size())return false;//否则返回否
			else{//位数相同的话
				if(this->value<=sz.value)return true;//负数的绝对值越小,负数越大,都有负号就不用判断绝对值了
				else return false;
			}
		}else if(this->value[0]=='-'){//左值是负数,所以不成立
			return false;
		}else if(sz.value[0]=='-'){//右值是负数,所以成立
			return true;
		}else{//两个都是正数的话
			if(this->value.size()>sz.value.size())return true;//与负数相反
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value>=sz.value)return true;
				else return false;
			}
		}
	}

5.小于等于の情况

和大于等于差不多,但是位数和数值的比较要改一下

C++
	bool operator<=(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value>=sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return true;
		}else if(sz.value[0]=='-'){
			return false;
		}else{
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value<=sz.value)return true;
				else return false;
			}
		}
	}

6.大于の情况

和大于等于差不多,把数值判断改成大于就行了

C++
	bool operator>(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value<sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return false;
		}else if(sz.value[0]=='-'){
			return true;
		}else{
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value>sz.value)return true;
				else return false;
			}
		}
	}

7.小于の情况

和小于等于差不多,把数值的判断改成小于就好了

C++
	bool operator<(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value>sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return true;
		}else if(sz.value[0]=='-'){
			return false;
		}else{
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value<sz.value)return true;
				else return false;
			}
		}
	}

超长的总代码:

C++
	bool operator==(const zsgjd &sz){
		if(this->value==sz.value)return true;
		return false;
	}
	bool operator!=(const zsgjd &sz){
		if(this->value!=sz.value)return true;
		return false;
	}
	bool operator>=(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value<=sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return false;
		}else if(sz.value[0]=='-'){
			return true;
		}else{
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value>=sz.value)return true;
				else return false;
			}
		}
	}
	bool operator<=(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value>=sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return true;
		}else if(sz.value[0]=='-'){
			return false;
		}else{
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value<=sz.value)return true;
				else return false;
			}
		}
	}
	bool operator<(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value>sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return true;
		}else if(sz.value[0]=='-'){
			return false;
		}else{
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value<sz.value)return true;
				else return false;
			}
		}
	}
	bool operator>(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value<sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return false;
		}else if(sz.value[0]=='-'){
			return true;
		}else{
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value>sz.value)return true;
				else return false;
			}
		}
	}

四、高精度基础计算

1.前期铺垫

由于有一些操作要重复执行,所以要用函数写一下,但是由于这些函数并不想让用户使用,这可是内部人员专属的东西,并且除了内部人员用,还有谁用,所以应该写在私有函数里面,所以要加private:,也就是

C++
private:

(1).字符串转数组

来,首先,用数组存下字符串,再计算,由于是经常要计算的,所以要写
我们分析一波~

  • 除了正数还有负数,所以要用一个bool存下来
  • 去除负号时的位数误差

以下是炒鸡吴迪牛鼻的代码

C++
	void Copy(int a[]/*存到这个数组里*/,string str/*字符串*/,bool &v/*是否是负数*/){
		if(str[0]=='-')v=true;//是负数
		a[0]=str.size()-v;//位数
		for(int i=1;i<=a[0];i++)a[i]=str[(int)str.size()-i/*反过来*/]-'0';//字符串转数组
	}

(2).数组转字符串

转成数组了总得转回去吧,不然又有位数限制了,并且我们™用的是字符串储存,由于也是经常要用的,所以写一个函数
分析一波

  • 负号不归函数管,不用写

没辣!!!

我代替我写的代码\color{#FFFFFF}{我}\color{#353535}{代}\color{#FFFFFF}{替我写的代}\color{#353535}{码}:

C++
	void COPY(int a[]/*计算完的数组*/,char ch[]/*存到的地方*/){
		for(int i=1;i<=a[0];i++)ch[a[0]-i/*翻回来*/]=a[i]+'0';//数组转字符串
	}

(3).数组进位

为什么有这个函数?因为作者懒 因为加法和乘法都要进位,所以写一下下下下下下下下锟斤拷烫烫烫烫烫烫烫烫烫烫烫烫烫
☹︎︎︎♏︎︎︎⧫︎︎︎🕯︎︎︎⬧︎︎︎ ⬧︎︎︎⧫︎︎︎♋︎︎︎❒︎︎︎⧫︎︎︎ ♍︎︎︎□︎︎︎♎︎︎︎♓︎︎︎■︎︎︎♑︎︎︎🖳︎

C++
	void Carry(int a[]/*要进位的数组*/){
		for(int i=1;i<=a[0];i++){//循环
			if(a[i]>9){//你怎么大于九
				a[i+1]+=a[i]/10;//多的就给下一位吧
				a[i]%=10;//你不许超过十
			}
		}
	}

W.D.Gaster附身

(4).数组退位

减法和除法要用,所以写一丅

C++
	void remove0(int a[]){
		while(a[a[0]]==0&&a[0]>0)a[0]--;//去零
		if(a[0]<=0)a[0]++;//不可能是负数位
	}

(5).数组比较

因为在判断数组夵时会用,所以写这个
Code:

C++
	bool big_(int a[],int b[]){//是否大于等于
		if(a[0]>b[0])return true;//位数大小直接判断
		if(a[0]<b[0])return false;
		for(int i=1;i<=a[0];i++){//逐位判断
			if(a[i]>b[i])return true;
			if(a[i]<b[i])return false;
		}
		return true;//等于大于的情况
	}

(6).特殊判断

因为作者太智慧了,所以有可能算出…-0!
不是,-0是正数还是负数
所以要判断-(!1)的情况

C++
	void gw(zsgjd a){
		if(a.value=="-0")a.value="0";
	}

总代码如下

C++
private:
	void Copy(int a[],string str,bool &v){
		if(str[0]=='-')v=true;
		a[0]=str.size()-v;
		for(int i=1;i<=a[0];i++)a[i]=str[(int)str.size()-i]-'0';
	}
	void COPY(int a[],char ch[]){
		for(int i=1;i<=a[0];i++)ch[a[0]-i]=a[i]+'0';
	}
	void Carry(int a[]){
		for(int i=1;i<=a[0];i++){
			if(a[i]>9){
				a[i+1]+=a[i]/10;
				a[i]%=10;
			}
		}
	}
	void remove0(int a[]){
		while(a[a[0]]==0&&a[0]>0)a[0]--;
		if(a[0]<=0)a[0]++;
	}
	bool big_(int a[],int b[]){
		if(a[0]>b[0])return true;
		if(a[0]<b[0])return false;
		for(int i=1;i<=a[0];i++){
			if(a[i]>b[i])return true;
			if(a[i]<b[i])return false;
		}
		return true;
	}
	void gw(zsgjd a){
		if(a.value=="-0")a.value="0";
	}

2.高精度加法

加法!!!
加法还不简单吗?不是!!!
因为除了负数,还有正数啊!啊不对说反了 先算一下以下算式吧

C++
213546213546546413654685798798798754651321321632161274172537923674516587321863754813271354721761296752761762373219752765137612768232732167612387237865+-89751321654213413271651432135413243341221345111451454651656563234545=?
-111111111111111111111154879879842132165465498798794965651919810546546542312321454655466544545889798985461321354649+-7985465432132132132471597521375615791676821731781375652971627382167851678167486147641545=?
-111111111111111444444444444444444444555555555555555551111111111111111111444444444444444444444444444444441111111111111199999999991111111119999999988881110
+1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111999999999999999999999999999999999999999999999999=?
1111111111111111111111111111111111111111111119999999999998465132135471432713824173518375483724875832146791378615679513751379537195-1111111111111111111524254879999999999999999999998888888888884125465432135453333333333333333333333333333333333333333333333333333333333=?

啊不对了,你不是神算不出来
人类可做版本

C++
11+45=?
-54+-98=?
84+-90=?
-54+60=?

看到以下算式呢,我是判断的方法算的:
A1A_1:两个都是正数所以用竖式就可以了

C++
   11
+  45
-------
   56

A2A_2:两个都是负数所以用竖式就可以了

C++
   54
+  98
-------
  152

然后再加上负号就可以了
A3A_3:因为是一个正数一个负数,所以用 减法
90>84所以90是被除数

C++
   .
   90
-  84
-------
    6

又因为90大于84,所以加负号
A4A_4:因为是一个正数一个负数,所以用 减法
60>54所以60是被除数

C++
   .
   60
-  54
-------
    6

又因为60大于54,所以!(加负号)


既然分析好了,那就…Code!!!
基础的模板:

C++
	zsgjd operator+(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};/*运算数组*/char chc[Maxlen];/*答案数组*/bool av=false,bv=false;//正负存储
		Copy(a,this->value,av);//转化为数组
		Copy(b,sz.value,bv);
		...
		COPY(c,chc);
		zsgjd x(chc);
		if(av==true&&bv==true)x.value='-'+x.value;//负数的情况
		gw(x);
		return x;
	}

(1).负数和正数

首先,负数和正数的绝对值也有大小之分,所以也要判断


a.负数更大的情况

因为负数更大,所以要负数减正数才可以 Code:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){//减法
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=true,bv=true;//结果是负数;

b.正数更大的情况

因为(!负数)更大,所以要 !(负数)减 !正数 才可以
ǝpoƆ:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){//也是减法
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=false,bv=false;//不是负数

总代码:

C++
			if(big_(a,b)){//判断大小
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=true,bv=true;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=false,bv=false;
			}
			remove0(c);//去零

(2).正数和负数

a.正数大的情况

因为正数大所以要用正数减负数
CodeC\hspace{-.15cm}o\hspace{-.1cm}d\hspace{-.05cm}e:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){//依然是减法
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=false,bv=false;//是正数

b.负数大的情况

因为负数大所以要用负数减正数
ƎpoɔƎ\hspace{-.1cm}p\hspace{-.07cm}o\hspace{-.05cm}ɔ:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){//依然还是减法
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=true,bv=true;//是负数

(3).都是正或都是负

因为是相同符号所以…可以快乐的加法了!!!
Happy′s  CodeHappy's\ \ Code:

C++
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];//照样...不对,是加法!!!
			c[0]=max(a[0],b[0])+1;
			Carry(c);
			remove0(c);//就当它不存在
			//因为都是true结果是负数,所以不用管

Total  codeTotal\ \ code:

C++
	zsgjd operator+(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(av&&!bv){
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=true,bv=true;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=false,bv=false;
			}
			remove0(c);
		}else if(!av&&bv){
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=false,bv=false;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=true,bv=true;
			}
			remove0(c);
		}else{
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
			c[0]=max(a[0],b[0])+1;
			Carry(c);
			remove0(c);
		}
		COPY(c,chc);
		zsgjd x(chc);
		if(av==true&&bv==true)x.value='-'+x.value;
		gw(x);
		return x;
	}

写这么久了,看着有些累了,那么我们…再来一遍!!!1


3.高精度减

因为减法和加法互为反运算,所以在减法里,异号是+,同号是-
基础墓板:

C++
	zsgjd operator-(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false,cv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		...
		c[0]=max(a[0],b[0]);
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if(cv)x.value='-'+x.value;//负数的情况
		gw(x);
		return x;
	}

(1).负数和正数的情况

因为符号不同,所以用加法,负数减正数会把负数减少,所以结果是负数;
亻弋石马:

C++
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
			cv=true;//结果是负数

(2).正数和负数的情况

因为符号不同,所以用加法,正数减负数会把正数增大,所以结果是正数;
马石弋亻:

C++
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];

(3).都是负数的情况

因为都是负数所以用减法,又有两种情况啦

a.第一个数大的情况

因为减负数等于加正数,而负数加正数的代码我们前面写过…嘿嘿嘿
抄的代码:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				cv=true;//结果是负数

b.第二个数大的情况

前面…我们…也写过^_^
也是抄的代码:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}

抄的总代码:

C++
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				cv=true;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
			}

(4).两个正数的情况

这个我们之前…啊不对,这不是一篇讲解文吗,不能抄太多,我们还是讲一下原理吧
请算出以下神算的…哦哦哦你是人,算吧

C++
514-114=?
465-4=?
300-16=?

A1A_1:基础减法,用竖式即可

C++
   514
 - 114
--------
   400

A2A_2:用竖式

C++
   465
 -   4
--------
   461

A3A_3:用竖式,但是要借位

C++
   ..
   300
 -  16
--------
   284

我们发现是低位从高位借位,我们只要模拟出借位和减法就好了
CodeCode:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){//位数不够
						a[i+1]--;//向高位借位
						a[i]+=10;
					}
					c[i]=a[i]-b[i];//减法
				}

要是高位也没有怎么办?根据算式a−b=−(b−a)a-b=-(b-a)可得出结果是负数,并用第二个减第一个
CodeǝboƆCodeǝboƆ:

C++
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				cv=true;//结果是负数

减法总代码:

C++
	zsgjd operator-(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false,cv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(av&&!bv){
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
			cv=true;
		}else if(!av&&bv){
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
		}else if(av&&bv){
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				cv=true;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
			}
		}else{
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				cv=true;
			}
		}
		c[0]=max(a[0],b[0]);
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if(cv)x.value='-'+x.value;
		gw(x);
		return x;
	}

4.高精度乘法

也是终于可以喘口气了,乘法比较简单,但是要理解其中的意思
计算以下算式

C++
14*29=?
-34*193=?
12*-47=?
-34*-23=?

符号的处理可以放到最后,因为同号为正,异号为负,来看看乘法是怎么运算的

C++
   29
*  14
-------
  116
  29
-------
  406

因为乘法要每一位相乘,所以有加法的部分,我们打算先处理,再进位
因为从第一位开始乘的起始位是一,所以是1+1-1=1、2+1-1=2…,在第二位开始开始乘的第一位,是2+1-1=2、2+2-1=3…后面以此类推

所以我们可以用数组模拟除过程
基础代码:

C++
	zsgjd operator*(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		...
		c[0]=a[0]+b[0];//最多是a[0]+b[0]-1
		Carry(c);//进位
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if((av==true&&bv==false)||(av==false&&bv==true)/*异号*/)x.value='-'+x.value;
		gw(x);
		return x;
	}

乘法代码:

C++
		for(int i=1;i<=a[0];i++){
			for(int j=1;j<=b[0];j++){
				c[i+j-1]+=a[i]*b[j];
			}
		}

总代码:

C++
	zsgjd operator*(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		for(int i=1;i<=a[0];i++){
			for(int j=1;j<=b[0];j++){
				c[i+j-1]+=a[i]*b[j];
			}
		}
		c[0]=a[0]+b[0];
		Carry(c);
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if((av==true&&bv==false)||(av==false&&bv==true))x.value='-'+x.value;
		gw(x);
		return x;
	}

没辣!!!简单吧!喘气过来了吧!那么…继续超长代码!!!


5.高精度除法

加油,再坚持一会就没气了
除法非常简单,照惯例先算算式,因为是整型的变量,所以多余的不用管

C++
5/3=?
-45/9=?
49/-7=?
-1/-4=?

因为除法反骨是从最高位开始算的,所以在写代码时应该倒着循环,又因为除法也是同号为正,异号为负,所以也可以放到最后处理
我们在计算除法的时候因为是判断前几位的大小再减的,所以要一直减除数到减不了,所以要写一下特殊判断,还有除数是零的情况也要判断
基础代码:

C++
	zsgjd operator/(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(b[0]==1&&b[1]==0){
			zsgjd x("0");
			return x;
		}
		...
		c[0]=a[0];
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if((av==true&&bv==false)||(av==false&&bv==true))x.value='-'+x.value;
		gw(x);
		return x;
	}

A1A_1:用小学( )年级就学过的除法即可

C++
   1
  _____
3/ 5
   3
  _____
   2

A2A_2根据同号为正,异号为负判断商的正负就好了

C++
    5
  _____
9/ 45
   45
  _____
    0

其他的作者懒得算了,所以不做解释
有解释的代码:

C++
		for(int i=a[0]-b[0]+1/*起点是从这个开始的*/;i>=1/*要大于个位*/;/*要不要减少看情况*/){
			bool v=false;
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){//要是已经小了
					v=true;//就标记一下
					break;
				}else if(a[j]>b[j-i+1]){//要是已经大了
					break;//直接退出
				}//等于的情况就不用判断了,因为会一直耗到结束
			}
			if(v){//不够了
				i--;//就下一位
				continue;
			}
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){//特殊处理的减法
					a[j+1]--;
					a[j]+=10;
				}
				a[j]-=b[j-i+1];
			}
			c[i]++;//这一位就加
		}

如有不懂,请把不懂了私信作者或发到讨论区
总代码:

C++
	zsgjd operator/(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(b[0]==1&&b[1]==0){
			zsgjd x("0");
			return x;
		}
		for(int i=a[0]-b[0]+1;i>=1;){
			bool v=false;
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					v=true;
					break;
				}else if(a[j]>b[j-i+1]){
					break;
				}
			}
			if(v){
				i--;
				continue;
			}
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					a[j+1]--;
					a[j]+=10;
				}
				a[j]-=b[j-i+1];
			}
			c[i]++;
		}
		c[0]=a[0];
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if((av==true&&bv==false)||(av==false&&bv==true))x.value='-'+x.value;
		gw(x);
		return x;
	}

6.高精度模法

模法我们其实在写高精度除法的时候已经写过了,因为除法的余数就是模的结果,所以我们直接改一下代码就好了
总代码:

C++
	zsgjd operator%(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0};char cha[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(b[0]==1&&b[1]==0){
			zsgjd x("0");
			return x;
		}
		for(int i=a[0]-b[0]+1;i>=1;){
			bool v=false;
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					v=true;
					break;
				}else if(a[j]>b[j-i+1]){
					break;
				}
			}
			if(v){
				i--;
				continue;
			}
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					a[j+1]--;
					a[j]+=10;
				}
				a[j]-=b[j-i+1];
			}//要的时结果就不用加了
		}
		remove0(a);
		COPY(a,cha);
		zsgjd x(cha);
		gw(x);
		return x;
	}

啊,写了这么多,也快结束了…吗?

7.高精度其他凌七杂八の运算

(1).自增自减

a.前置自增

因为类内自带前置运算符,所以后面的参数不用填,又因为前置自增是先算在返回,所以代码也要先算在返回
deCo:

C++
	zsgjd operator++(){
		zsgjd This,x("1");
		This.value=this->value;
		This=*this+x;
		this->value=This.value;
		return This;
	}
b.后置自增

类内自带前置运算符怎么办?没事,在参数框里写一个int就行了,因为创作者早就预料到了这种情况了,所以为了区分,写一个int就好了,再因为后置是返回原值再计算,所以要用一个值保存
oCed:

C++
	zsgjd operator++(int){
		zsgjd This,x("1"),THIS;
		This.value=this->value;
		THIS.value=this->value;
		This=*this+x;
		this->value=This.value;
		return THIS;
	}
c.前置自减

和前置自增一样改成减就好了
deCo:

C++
	zsgjd operator--(){
		zsgjd This,x("1");
		This.value=this->value;
		This=*this-x;
		this->value=This.value;
		return This;
	}
d.后置自减

和后置自增一样改成减就好了
oCed:

C++
	zsgjd operator--(int){
		zsgjd This,x("1"),THIS;
		This.value=this->value;
		THIS.value=this->value;
		This=*this-x;
		this->value=This.value;
		return THIS;
	}

(2).new and delete

再网上查阅资料后可以知道,new用malloc,delete用free,所以我直接贴代码了

C++
	void* operator new(size_t sz){
		zsgjd *New=(zsgjd*)malloc(sz);
		return New;
	}
	void operator delete(void* sc){
		free(sc);
	}
	void* operator new[](size_t sz){
		zsgjd *New=(zsgjd*)malloc(sz);
		return New;
	}
	void operator delete[](void* sc){
		free(sc);
	}

(3).复合运算

因为+-*/%我们前面写过所以就改一下值就好了 作者临时写的代码:

C++
	zsgjd operator+=(const zsgjd &sz){
		*this=*this+sz;
		return *this;
	}
	zsgjd operator-=(const zsgjd &sz){
		*this=*this-sz;
		return *this;
	}
	zsgjd operator*=(const zsgjd &sz){
		*this=*this*sz;
		return *this;
	}
	zsgjd operator/=(const zsgjd &sz){
		*this=*this/sz;
		return *this;
	}
	zsgjd operator%=(const zsgjd &sz){
		*this=*this%sz;
		return *this;
	}

(4).输入输出

为什么这个标题buff叠满? 因为这个很重要,难道要让我们的用户输入字符串吗,那!是!不!可!能!的!
为什么string可以直接用?其实它不是就有这个类型,而是把所有可能用到的符号都重载了,所以看起来好像是一个类型
所以我们也要把输入输出也重载了,由于类内重载默认是这个类型,所以必须类外重载,所以应该在类内写

C++
	friend istream &operator>>(istream &in,zsgjd &a);//输入要改变所以不用const
	friend ostream &operator<<(ostream &out,const zsgjd &a);

因为cin是istream类型的,而cout是ostream类型的,所以要类外重载
输入代码:

C++
istream &operator>>(istream &in,zsgjd &a){
	in>>a.value;//读入a
	return in;
}

输出代码:

C++
ostream &operator<<(ostream &out,const zsgjd &a){
	out<<a.value;
	return out;
}

后记、计划、投票

后记

本片代码未写完,还有一些运算,敬请期待

计划

  • 基础的代码
  • 更多函数(333赞更)
  • 位运算(666赞更)
  • 下一篇,小数高精度(两千赞爆肝更)

投票

写哪些函数,但不许是cos,sin等,写小数时投票更

总代码

C++
#include<bits/stdc++.h>
using namespace std;
class zsgjd{
public:
	string value;
	const int Maxlen;
	zsgjd():value("0"),Maxlen(100000){};
	zsgjd(string str):value(str),Maxlen(100000){};
	zsgjd(string str,int len):value(str),Maxlen(len){};
	zsgjd(char c[]):Maxlen(100000){
		this->value=c;
	}
	zsgjd(char c[],int len):Maxlen(len){
		this->value=c;
	}
	void operator=(const zsgjd &sz){
		this->value=sz.value;
	}
	bool operator==(const zsgjd &sz){
		if(this->value==sz.value)return true;
		return false;
	}
	bool operator!=(const zsgjd &sz){
		if(this->value!=sz.value)return true;
		return false;
	}
	bool operator>=(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value<=sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return false;
		}else if(sz.value[0]=='-'){
			return true;
		}else{
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value>=sz.value)return true;
				else return false;
			}
		}
	}
	bool operator<=(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value>=sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return true;
		}else if(sz.value[0]=='-'){
			return false;
		}else{
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value<=sz.value)return true;
				else return false;
			}
		}
	}
	bool operator<(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value>sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return true;
		}else if(sz.value[0]=='-'){
			return false;
		}else{
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value<sz.value)return true;
				else return false;
			}
		}
	}
	bool operator>(const zsgjd &sz){
		if(this->value[0]=='-'&&sz.value[0]=='-'){
			if(this->value.size()>sz.value.size())return true;
			else if(this->value.size()<sz.value.size())return false;
			else{
				if(this->value<sz.value)return true;
				else return false;
			}
		}else if(this->value[0]=='-'){
			return false;
		}else if(sz.value[0]=='-'){
			return true;
		}else{
			if(this->value.size()<sz.value.size())return true;
			else if(this->value.size()>sz.value.size())return false;
			else{
				if(this->value>sz.value)return true;
				else return false;
			}
		}
	}
	zsgjd operator+(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(av&&!bv){
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=true,bv=true;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=false,bv=false;
			}
			remove0(c);
		}else if(!av&&bv){
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				av=false,bv=false;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				av=true,bv=true;
			}
			remove0(c);
		}else{
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
			c[0]=max(a[0],b[0])+1;
			Carry(c);
			remove0(c);
		}
		COPY(c,chc);
		zsgjd x(chc);
		if(av==true&&bv==true)x.value='-'+x.value;
		gw(x);
		return x;
	}
	zsgjd operator-(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false,cv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(av&&!bv){
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
			cv=true;
		}else if(!av&&bv){
			for(int i=1;i<=max(a[0],b[0]);i++)c[i]=a[i]+b[i];
		}else if(av&&bv){
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
				cv=true;
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
			}
		}else{
			if(big_(a,b)){
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(b[i]>a[i]){
						a[i+1]--;
						a[i]+=10;
					}
					c[i]=a[i]-b[i];
				}
			}else{
				for(int i=1;i<=max(a[0],b[0]);i++){
					if(a[i]>b[i]){
						b[i+1]--;
						b[i]+=10;
					}
					c[i]=b[i]-a[i];
				}
				cv=true;
			}
		}
		c[0]=max(a[0],b[0]);
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if(cv)x.value='-'+x.value;
		gw(x);
		return x;
	}
	zsgjd operator*(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		for(int i=1;i<=a[0];i++){
			for(int j=1;j<=b[0];j++){
				c[i+j-1]+=a[i]*b[j];
			}
		}
		c[0]=a[0]+b[0];
		Carry(c);
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if((av==true&&bv==false)||(av==false&&bv==true))x.value='-'+x.value;
		gw(x);
		return x;
	}
	zsgjd operator/(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0},c[Maxlen]={0};char chc[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(b[0]==1&&b[1]==0){
			zsgjd x("0");
			return x;
		}
		for(int i=a[0]-b[0]+1;i>=1;){
			bool v=false;
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					v=true;
					break;
				}else if(a[j]>b[j-i+1]){
					break;
				}
			}
			if(v){
				i--;
				continue;
			}
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					a[j+1]--;
					a[j]+=10;
				}
				a[j]-=b[j-i+1];
			}
			c[i]++;
		}
		c[0]=a[0];
		remove0(c);
		COPY(c,chc);
		zsgjd x(chc);
		if((av==true&&bv==false)||(av==false&&bv==true))x.value='-'+x.value;
		gw(x);
		return x;
	}
	zsgjd operator%(const zsgjd &sz){
		int a[Maxlen]={0},b[Maxlen]={0};char cha[Maxlen];bool av=false,bv=false;
		Copy(a,this->value,av);
		Copy(b,sz.value,bv);
		if(b[0]==1&&b[1]==0){
			zsgjd x("0");
			return x;
		}
		for(int i=a[0]-b[0]+1;i>=1;){
			bool v=false;
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					v=true;
					break;
				}else if(a[j]>b[j-i+1]){
					break;
				}
			}
			if(v){
				i--;
				continue;
			}
			for(int j=a[0];j>=i;j--){
				if(a[j]<b[j-i+1]){
					a[j+1]--;
					a[j]+=10;
				}
				a[j]-=b[j-i+1];
			}
		}
		remove0(a);
		COPY(a,cha);
		zsgjd x(cha);
		gw(x);
		return x;
	}
	zsgjd operator++(){
		zsgjd This,x("1");
		This.value=this->value;
		This=*this+x;
		this->value=This.value;
		return This;
	}
	zsgjd operator++(int){
		zsgjd This,x("1"),THIS;
		This.value=this->value;
		THIS.value=this->value;
		This=*this+x;
		this->value=This.value;
		return THIS;
	}
	zsgjd operator--(){
		zsgjd This,x("1");
		This.value=this->value;
		This=*this-x;
		this->value=This.value;
		return This;
	}
	zsgjd operator--(int){
		zsgjd This,x("1"),THIS;
		This.value=this->value;
		THIS.value=this->value;
		This=*this-x;
		this->value=This.value;
		return THIS;
	}
	void* operator new(size_t sz){
		zsgjd *New=(zsgjd*)malloc(sz);
		return New;
	}
	void operator delete(void* sc){
		free(sc);
	}
	void* operator new[](size_t sz){
		zsgjd *New=(zsgjd*)malloc(sz);
		return New;
	}
	void operator delete[](void* sc){
		free(sc);
	}
	zsgjd operator+=(const zsgjd &sz){
		*this=*this+sz;
		return *this;
	}
	zsgjd operator-=(const zsgjd &sz){
		*this=*this-sz;
		return *this;
	}
	zsgjd operator*=(const zsgjd &sz){
		*this=*this*sz;
		return *this;
	}
	zsgjd operator/=(const zsgjd &sz){
		*this=*this/sz;
		return *this;
	}
	zsgjd operator%=(const zsgjd &sz){
		*this=*this%sz;
		return *this;
	}
	friend istream &operator>>(istream &in,zsgjd &a);
	friend ostream &operator<<(ostream &out,const zsgjd &a);
private:
	void Copy(int a[],string str,bool &v){
		if(str[0]=='-')v=true;
		a[0]=str.size()-v;
		for(int i=1;i<=a[0];i++)a[i]=str[(int)str.size()-i]-'0';
	}
	void COPY(int a[],char ch[]){
		for(int i=1;i<=a[0];i++)ch[a[0]-i]=a[i]+'0';
	}
	void Carry(int a[]){
		for(int i=1;i<=a[0];i++){
			if(a[i]>9){
				a[i+1]+=a[i]/10;
				a[i]%=10;
			}
		}
	}
	void remove0(int a[]){
		while(a[a[0]]==0&&a[0]>0)a[0]--;
		if(a[0]<=0)a[0]++;
	}
	bool big_(int a[],int b[]){
		if(a[0]>b[0])return true;
		if(a[0]<b[0])return false;
		for(int i=1;i<=a[0];i++){
			if(a[i]>b[i])return true;
			if(a[i]<b[i])return false;
		}
		return true;
	}
	void gw(zsgjd a){
		if(a.value=="-0")a.value="0";
	}
};
istream &operator>>(istream &in,zsgjd &a){
	in>>a.value;
	return in;
}
ostream &operator<<(ostream &out,const zsgjd &a){
	out<<a.value;
	return out;
}

如有错误,请大佬们在评论区@作者改正
完结撒花🎇🎇🎇
希望不要一觉醒来天塌了

脚注

  1. 作者 白 勺_白\ ^勺 解释 回到正文↩

原文