#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int MR=1e3+2;
struct Big{
int l;
int num[MR];
void set(string s){
//用s设置l与num[]的值
l = s.size();
for (int i = 1;i <= l;i++) num[i] = s[l - i] - '0';
}
void set(int x){
set(to_string(x));
}
void print(){
for(int i = l;i >= 1;i--){
cout <<num[i];
}
}
};
bool operator<(Big x,Big y){
if(x.l!=y.l) return x.l<y.l;
for(int i = x.l;i >= 1;i--){
if(x.num[i]!=y.num[i]) return x.num[i]<y.num[i];
}
return 0;
}
Big operator+(Big a,Big b){
Big c;
int al=a.l,bl=b.l;
int cl=max(al,bl);
int u=0;
for (int i = 1;i <= cl;i++) {
if(i<=al) u+=a.num[i];
if(i<=bl) u+=b.num[i];
c.num[i]=u%10;
u/=10;
}
if(u>0){
cl++;
c.num[cl]=u;
}
c.l=cl;
return c;
}
Big operator-(Big a,Big b){
Big c;
c.l=a.l;
int u=0;
for (int i = 1;i <= c.l;i++) {
u+=a.num[i];
if(i<=b.l) u-=b.num[i];
if(u<0){
c.num[i]=u+10;
u=-1;
}
else{
c.num[i]=u;
u=0;
}
}
while(c.l>1&&c.num[c.l]==0) c.l--;
return c;
}
Big operator*(Big a,int b){
Big c;
int al=a.l;
c.l=al;
long long u=0;
for(int i = 1;i <= c.l;i++){
u+=1ll*a.num[i]*b;
c.num[i]=u%10;
u/=10;
}
while(u>0){
c.l++;
c.num[c.l]=u%10;
u/=10;
}
return c;
}
Big operator/(Big a,int b){
Big c;
c.l=a.l;
long long r=0;
for(int i = c.l;i >= 1;i--){
r=r*10+a.num[i];
c.num[i]=r/b;
r%=b;
}
while(c.num[c.l]==0&&c.l>1) c.l--;
return c;
}
int main(){
char x;
string a;
Big A,B;
cin >> x >> a;
A.set(a);
if(x=='+'){
string b;
cin >> b;
B.set(b);
A=A+B;
A.print();
}
else if(x=='-'){
string b;
cin >> b;
Big B,C;
B.set(b);
if(A<B) C=B-A;
else C=A-B;
C.print();
}
else if(x=='*'){
int b;
cin >> b;
A=A*b;
A.print();
}
else if(x=='/'){
int b;
cin >> b;
A=A/b;
A.print();
}
return 0;
}
/*
高精乘高精:
for(int i =1 ;i<=al;i++)
for(int j = 1;j <= bl;j++)
c.num[i+j-1]+=a[i]*b[j];
*/
标签:set,return,高精度,int,Big,--,算法,num,模板
From: https://www.cnblogs.com/andy561/p/gaojingdu.html