#include<bits/stdc++.h>
using namespace std;
class long_int{
public:
char num[2005];
int l;
int sign;
long_int();
long_int read(int s)
{
sprintf(num,"%d",s);
l=strlen(num);
strrev(num);
}
friend istream& operator >> (istream& in, long_int &a);
friend ostream& operator << (ostream& out,long_int a);
friend bool operator < (long_int a,long_int b);
friend bool operator > (long_int a,long_int b);
friend bool operator <= (long_int a,long_int b);
friend bool operator >= (long_int a,long_int b);
friend bool operator == (long_int a,long_int b);
friend long_int operator + (long_int a,long_int b);
friend long_int operator - (long_int a,long_int b);
friend long_int operator * (long_int a,long_int b);
friend long_int operator / (long_int a,long_int b);
friend long_int operator % (long_int a,long_int b);
friend bool operator < (long_int a,int b);
friend bool operator > (long_int a,int b);
friend bool operator <= (long_int a,int b);
friend bool operator >= (long_int a,int b);
friend bool operator == (long_int a,int b);
friend long_int operator + (long_int a,int b);
friend long_int operator - (long_int a,int b);
friend long_int operator * (long_int a,int b);
friend long_int operator / (long_int a,int b);
friend long_int operator % (long_int a,int b);
};
int main()
{
/*
freopen("in.txt","r",stdin);
freopen("out1.txt","w",stdout);
*/
printf("plz input big-int a and b:(<10^1000)\n");
long_int a,b,c;
printf("a: ");
cin>>a;
printf("b: ");
cin>>b;
cout<<"a+b="<<a+b<<endl;
cout<<"a-b="<<a-b<<endl;
cout<<"a*b="<<a*b<<endl;
if(b==0) cout<<"zero can't be the divisor"<<endl;
else cout<<"a/b="<<a/b<<endl;
if(b==0) cout<<"zero can't be the divisor"<<endl;
else cout<<"a%b="<<a%b<<endl;
return 0;
}
long_int :: long_int()
{
memset(num,0,sizeof(num));
l=1;
sign=1;
}
istream& operator >> (istream& in, long_int &a)
{
in>>a.num;
if(a.num[0]=='-')
{
a.sign=-1;
int i;
for(i=0;i<strlen(a.num);i++) a.num[i]=a.num[i+1];
}
strrev(a.num);
a.l=strlen(a.num);
}
ostream& operator << (ostream& out,long_int a)
{
strrev(a.num);
if(a.sign==-1) out<<"-";
out<<a.num;
}
bool operator < (long_int a,long_int b)
{
if(a.l<b.l) return true;
if(a.l>b.l) return false;
int i=a.l-1;
while(i>=0)
{
if(a.num[i]>b.num[i]) return false;
if(a.num[i]<b.num[i]) return true;
i--;
}
return false;
}
bool operator > (long_int a,long_int b)
{
return b<a;
}
bool operator <= (long_int a,long_int b)
{
return !(a>b);
}
bool operator >= (long_int a,long_int b)
{
return !(a<b);
}
bool operator == (long_int a,long_int b)
{
return ((a>=b)&&(a<=b));
}
long_int operator + (long_int a,long_int b)
{
long_int c;
if(a.sign==b.sign) c.sign=a.sign;
else if(a.sign>b.sign)
{
b.sign=1;
return a-b;
}
else if(a.sign<b.sign)
{
a.sign=1;
return b-a;
}
int i,ok=0,t,L=max(a.l,b.l);
for(i=0;i<L;i++)
{
if(a.num[i]=='\0') a.num[i]='0';
if(b.num[i]=='\0') b.num[i]='0';
t=a.num[i]+b.num[i]-2*'0'+ok;
c.num[i]=t%10+'0';
if(t>=10) ok=1;
else ok=0;
}
if(ok==1)
{
c.num[L]='1';
L++;
c.num[L]='\0';
}
c.l=L;
return c;
}
long_int operator - (long_int a,long_int b)
{
long_int c;
if(a.sign>b.sign)
{
b.sign=1;
c=a+b;
return a+b;
}
else if(a.sign<b.sign)
{
a.sign=1;
c=a+b;
c.sign=-1;
return c;
}
else if(a.sign == -1 && b.sign == -1)
{
a.sign=1;
b.sign=1;
c=b-a;
return c;
}
if(a>=b) c.sign=1;
else
{
c=b-a;
c.sign=-1;
return c;
}
int i,ok=0,L=max(a.l,b.l);
int t1,t2;
for(i=0;i<L;i++)
{
if(a.num[i]=='\0') a.num[i]='0';
if(b.num[i]=='\0') b.num[i]='0';
t1=a.num[i]-'0'-ok;
t2=b.num[i]-'0';
if(t1<t2)
{
ok=1;
t1+=10;
}
else
ok=0;
c.num[i]=t1-t2+'0';
}
while((c.num[L]=='0' || c.num[L]=='\0') && L>=1) L--;
c.l=++L;
c.num[L]='\0';
return c;
}
long_int operator * (long_int a,long_int b)
{
long_int c;
int i,j,t,ok=0;
for(i=0;i<b.l;i++)
{
long_int k;
for(j=0;j<a.l;j++)
{
t=(b.num[i]-'0')*(a.num[j]-'0')+ok;
k.num[j+i]=t%10+'0';
if(t>=10) ok=t/10;
else ok=0;
}
k.l=a.l+i;
if(ok>0)
{
k.l++;
k.num[k.l-1]=ok+'0';
k.num[k.l]='\0';
ok=0;
}
c=c+k;
}
while((c.num[c.l-1]=='0'||c.num[c.l-1]=='\0')&&c.l>1) c.l--;
c.num[c.l]='\0';
c.sign=a.sign*b.sign;
return c;
}
long_int operator / (long_int a,long_int b)
{
long_int c;
c.sign=a.sign*b.sign;
a.sign=1;
b.sign=1;
while(a>b)
{
int k=0,t=0;
long_int bb=b;
while(bb*10<=a)
{
bb=bb*10;
k++;
}
while(a>=bb)
{
a=a-bb;
t++;
}
c.num[k]=t+'0';
if(c.l<k+1) c.l=k+1;
}
c.num[c.l]='\0';
int i;
for(i=0;i<c.l;i++) if(c.num[i]=='\0') c.num[i]='0';
return c;
}
long_int operator % (long_int a,long_int b)
{
long_int c=a/b;
c=b*c;
return a-c;
}
bool operator < (long_int a,int b)
{
long_int c;
c.read(b);
return a<c;
}
bool operator > (long_int a,int b)
{
long_int c;
c.read(b);
return a>c;
}
bool operator <= (long_int a,int b)
{return !(a>b);}
bool operator >= (long_int a,int b)
{return !(a<b);}
bool operator == (long_int a,int b)
{return (a<=b&&a>=b);}
long_int operator + (long_int a,int b)
{
long_int c;
c.read(b);
return a+c;
}
long_int operator - (long_int a,int b)
{
long_int c;
c.read(b);
return a-c;
}
long_int operator * (long_int a,int b)
{
long_int c;
c.read(b);
return a*c;
}
long_int operator / (long_int a,int b)
{
long_int c;
c.read(b);
return a/c;
}
long_int operator % (long_int a,int b)
{
long_int c;
c.read(b);
return a%c;
}
高精度四则运算
最新推荐文章于 2024-02-21 00:01:29 发布