重载高精减不知道哪出问题了
查看原帖
重载高精减不知道哪出问题了
397250
CD_Sun_doer楼主2023/10/19 17:34
#include<bits/stdc++.h>
using namespace std;
int base;
char str[20005];
struct node{
	int len,s[20005];
	node() { len=0; memset(s,0,sizeof(s)); }
};
//重载>=符号 
bool operator >=(const node &a,const node &b){
	if(a.len<b.len) 
		return false;
	else if(a.len==b.len){
		for(int i=1;i<=b.len;i++){
			if(a.s[i]>b.s[i])
				return true;
			else if(a.s[i]<b.s[i]) 
				return false;
		}
	}
	return true;
}
//加法 
//node operator + (const node&a,const node&b){
//	node c;
//	c.len=max(a.len,b.len);
//	for(int i=1;i<=c.len;i++){
//		c.s[i]+=a.s[i]+b.s[i];
//		c.s[i+1]+=c.s[i]/10;
//		c.s[i]%=10;
//	}
//	if(c.s[c.len+1])
//		c.len++;
//	return c;
//}
//减法 
node operator - (const node &a,const node &b){
	node c;
	c.len=max(a.len,b.len);
	for(int i=1;i<=c.len;i++){
		c.s[i]=a.s[i]-b.s[i];
		if(c.s[i] < 0){
			c.s[i]+=10;
			a.s[i+1]--;
		}
	}
	while(c.len>0 && !c.s[c.len]) 
		c.len--;
	if(c.len==0) 
		c.len=1,c.s[1]=0;
	return c;
}
//读入 
node read(){
	scanf("%s",str);
	int len = strlen(str);
	node a;
	a.len=len;
	for(int i=0;i<len;i++) 
		a.s[len-i]=str[i]-'0';
	return a;
}
//输出 
void print(node a){
	for(int i=a.len;i>=1;i--)
		printf("%d",a.s[i]);
}
int main(){
	//scanf("%d",&base);
	node a,b,c;
	a=read(),b=read();
	if(a>=b) 
		c=a-b;
	else {
		c=b-a;
		putchar('-');
	}
	print(c);
	return 0;
} 
2023/10/19 17:34
加载中...