高精度加法,减法,乘法 \(\times\) 2。(可判负数)
struct st {
bool f=0;
int len=0;
int a[10086];
void clear() {
memset(a,0,sizeof(a));
f=0;
len=0;
}
void read() {
string s;
cin>>s;
len=s.size();
s=" "+s;
if(s[1]=='-') {
for(int i=2; i<=len; i++) {
a[len-i+1]=s[i]-'0';
}
f=1;
len--;
} else {
for(int i=1; i<=len; i++) {
a[len-i+1]=s[i]-'0';
}
}
}
void print() {
bool fl=0;
if(f) cout<<"-";
for(int i=len; i>=1; i--) {
if(a[i]) fl=1;
if(fl) cout<<a[i];
}
if(fl==0) cout<<0;
cout<<"\n";
}
bool operator > (st b) {
if(f==0&&b.f==0) {
if(b.len!=len) {
return len>b.len;
}
for(int i=len; i>=1; i--) {
if(a[i]!=b.a[i]) return a[i]>b.a[i];
}
return 0;
} else if(f!=b.f) {
return f<b.f;
} else {
if(b.len!=len) {
return len<b.len;
}
for(int i=len; i>=1; i--) {
if(a[i]!=b.a[i]) return a[i]<b.a[i];
}
return 0;
}
}
st operator + (st b) {
st c;
c.len=max(len,b.len);
if(b.f==f) {
for(int i=1; i<=c.len; i++) {
c.a[i]+=a[i]+b.a[i];
c.a[i+1]+=c.a[i]/10;
c.a[i]%=10;
}
while(c.a[c.len+1]) c.len++;
c.f=f;
} else if(f==1) {
st a=*this;
a.f=0;
c=a-b;
} else {
st a=b;
a.f=0;
c=b-a;
}
return c;
}
st operator - (st b) {
if(f==0&&b.f==1) {
st a=b;
a.f=0;
return *this+a;
}
else if(f==1&&b.f==0)
{
st a=*this;
a.f=0;
st c=a+b;
c.f^=1;
return c;
}
else if(f==1&&b.f==1)
{
st aa=*this,bb=b;
aa.f=0;
bb.f=0;
st c=bb-aa;
return c;
}
st c;
c.clear();
if(b>*this) {
swap(*this,b);
c.f^=1;
}
c.len=max(len,b.len);
for(int i=1; i<=c.len; i++) {
c.a[i]+=a[i]-b.a[i];
if(c.a[i]<0) {
c.a[i]+=10;
c.a[i+1]--;
}
}
return c;
}
st operator * (st b) {
st c;
if(a[1]=='0'||b.a[1]=='0')
{
c.f=0;
c.len=1;
c.a[1]=0;
return c;
}
c.f=(f!=b.f);
c.len=len+b.len;
for(int i=1; i<=len; i++) {
for(int j=1; j<=b.len; j++) {
c.a[i+j-1]+=a[i]*b.a[j];
}
}
for(int i=1; i<=c.len; i++) {
c.a[i+1]+=c.a[i]/10;
c.a[i]%=10;
}
while(c.a[c.len]==0) c.len--;
return c;
}
st operator * (int b) {
st c;
if(a[1]=='0'||b==0)
{
c.f=0;
c.len=1;
c.a[1]=0;
return c;
}
c.f=(f);
c.len=len*2;
for(int i=1; i<=len; i++) {
c.a[i]=a[i]*b;
}
for(int i=1; i<=c.len; i++) {
c.a[i+1]+=c.a[i]/10;
c.a[i]%=10;
}
while(c.a[c.len]==0) c.len--;
return c;
}
};
标签:return,高精度,int,len,st,--,模板
From: https://www.cnblogs.com/yaaaaaan/p/18650217