#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const short MAX=200;//要想数据范围更大就修改这个
struct BigInt{
//可以自己加一个符号位
short len;
char numbers[MAX+1];
};
BigInt add(BigInt a,BigInt b){//竖式计算,小学内容
BigInt s;
memset(s.numbers,'0',sizeof(s.numbers));
s.len=max(a.len,b.len);
for(int i=MAX;i>MAX-s.len;i--){
int x=s.numbers[i]-'0'+a.numbers[i]-'0'+b.numbers[i]-'0';
if(x>=10){
s.numbers[i-1]++;
x-=10;
}
s.numbers[i]=x+'0';
}
if(s.numbers[MAX-s.len]!='0') s.len++;//判断是否进位
return s;
}
BigInt subtraction(BigInt a,BigInt b){
BigInt s;
memset(s.numbers,'0',sizeof(s.numbers));
s.len=max(a.len,b.len);
for(int i=MAX;i>MAX-s.len;i--){
int x=s.numbers[i]-'0'+a.numbers[i]-b.numbers[i];
if(x<0){
s.numbers[i-1]--;
x+=10;
}
s.numbers[i]=x+'0';
}
while(s.numbers[MAX-s.len+1]=='0') s.len--;
return s;
}
int main(){
BigInt a,b,c;
memset(a.numbers,'0',sizeof(a.numbers));//初始化非常重要!!!
memset(b.numbers,'0',sizeof(b.numbers));
cin>>a.len>>b.len;
for(int i=MAX-a.len+1;i<=MAX;i++) cin>>a.numbers[i];
for(int i=MAX-b.len+1;i<=MAX;i++) cin>>b.numbers[i];
c=subtraction(a,b);
//c=add(a,b);
for(int i=MAX-c.len+1;i<=MAX;i++) cout<<c.numbers[i];
return 0;
}
这个大整数是用char储存的,非常简单,就没写乘除了,原理就是竖式计算
其实有更高效的储存方式,使用unsighed int(2^32)的大数数组就比较好,大家可以自己试试