高精度算法其实就是把两个超长位数的数加减乘除;下面是一个演示代码:
#include<bits/stdc++.h>
using namespace std;
struct bign{
int l,x[20000];
bign(){
l=1;
memset(x,0,sizeof(x));
}
}a,b,c;
string sa,sb;
bign read(string s){
int n=s.size();
bign a;
a.l=n;
for(int i=1;i<=n;i++){
a.x[i]=s[n-i]-'0';
}
return a;
}
void print(bign a){
for(int i=a.l;i>=1;i--){
printf("%d",a.x[i]);
}
}
bign fix(bign a){//处理进位
for(int i=1;i<=a.l;i++){
while(a.x[i]<0){
a.x[i+1]--;
a.x[i]+=10;
}
a.x[i+1]+=a.x[i]/10;
a.x[i]%=10;
}
if(a.x[a.l+1]) a.l++;
while(a.x[a.l]>=10){
a.x[a.l+1]+=a.x[a.l]/10;
a.x[a.l]%=10;
a.l++;
}
while(a.x[a.l]==0&&a.l>1){
a.l--;
}
return a;
}
bign operator + (bign a, int b){//opertor 是重载的意思,能定义c++中没有的运算
a.x[1] += b;
return fix(a);
}
bign operator + (bign a,bign b){
bign c;
c.l=max(a.l, b.l);
for(int i=1;i<=c.l;i++){
c.x[i]=a.x[i]+b.x[i];
}
return fix(c);
}
bign operator - (bign a,bign b){
bign c;
c.l=a.l;
for(int i=1;i<=a.l;i++){
c.x[i]+=a.x[i]-b.x[i];
}
return fix(c);
}
bign operator * (bign a,int b){
for(int i=1;i<=a.l;i++){
a.x[i]*=b;
}
return fix(a);
}
bign operator * (bign a,bign b){
bign c;
c.l=a.l+b.l;
for(int i=1;i<=a.l;i++){
for(int j=1;j<=b.l;j++){
c.x[i+j-1]+=a.x[i]*b.x[j];
}
}
return fix(c);
}
bool operator < (bign a,bign b){
if(a.l<b.l){
return true;
}
if(a.l==b.l){
for(int i=a.l;i>=1;i--){
if(a.x[i]>b.x[i]) return false;
if(a.x[i]<b.x[i]) return true;
}
}
return false;
}
void jian(bign a,bign b){
if(a<b){
cout<<"-";
swap(a,b);
}
c=a-b;
}
void jia(bign a,bign b){
c=a+b;
}
void cheng(bign a,bign b){
c=a*b;
}
void chu(bign a,bign b){
c=a/b;
}
int main(){
cin>>sa>>sb;
a=read(sa);
b=read(sb);
jia(a,b);
jian(a,b);
cheng(a,b);
chu(a,b);
print(c);
return 0;
}