#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
bool c=false;
class high_pricision{
public:
int a[100001],lenth;
void read()
{
memset(a,0,sizeof(a));
char s[100001];
int k=0;
cin>>s;
lenth=strlen(s);
for(int i=0;i<lenth;i++)
a[i]=s[lenth-i-1]-'0';
}
void write()
{
if(c==true&&(lenth>-1||a[0]!=0)) cout<<'-';
for(int i=lenth+1;i>=0;i--)
cout<<a[i];
}
high_pricision operator +(const high_pricision &num)const//num代表第二个操作数
{
high_pricision ans;
ans.lenth=max(lenth,num.lenth);
for(int i=0;i<ans.lenth;i++)
{
ans.a[i]+=num.a[i]+a[i];
if(ans.a[i]>=10)
{
ans.a[i]-=10;
ans.a[i+1]+=1;
}
}
int k=ans.lenth+1;
while(ans.a[k--]==0&&k>=0);
ans.lenth=k;
return ans;
}
high_pricision operator -(const high_pricision &num)const
{
high_pricision ans;
ans.lenth=max(lenth,num.lenth);
for(int i=0;i<ans.lenth;i++)
{
if(c==true) ans.a[i]=num.a[i]-a[i];
else ans.a[i]=a[i]-num.a[i];
}
for(int i=0;i<ans.lenth;i++)
{
if(ans.a[i]<0&&ans.lenth!=1)
{
ans.a[i]+=10;
ans.a[i+1]-=1;
}
}
int k=ans.lenth;
while(ans.a[k--]==0&&k>=0);
ans.lenth=k;
return ans;
}
high_pricision operator *(const high_pricision &num)const
{
high_pricision ans;
for(int i=0;i<lenth;i++)
{
int mo=0;
for(int j=0;j<num.lenth;j++)
{
ans.a[i+j]=mo+a[i]*num.a[j]+ans.a[i+j];
mo=ans.a[i+j]/10;
ans.a[i+j]%=10;
}
ans.a[i+num.lenth]=mo;
}
int k=lenth+num.lenth;
while(ans.a[k--]==0&&k>=0);
ans.lenth=k;
return ans;
}
high_pricision operator /(const long long &num)const//高精除单精,取整
{
high_pricision ans;
if(c==true)
{
ans.a[0]=0;
ans.lenth=-1;
return ans;
}
int mo=0;
for(int i=0;i<lenth;i++)
{
ans.a[i]=ans.a[i]+((mo*10+a[i])/num);
mo=(mo*10+a[i])%num;
}
int k=lenth;
while(ans.a[k--]==0&&k>=0);
ans.lenth=k;
return ans;
}
};
void change(int *f,int *s,int fl,int sl);
int main()
{
high_pricision fir,sec,ans;
char mod;
cout<<"Enter one number,the mode and the other number:\n";
fir.read();
cin>>mod;
sec.read();
if(mod=='-')
{
change(fir.a,sec.a,fir.lenth,sec.lenth);
ans=fir-sec;
}
if(mod=='+') ans=fir+sec;
if(mod=='*') ans=fir*sec;
if(mod=='/')
{
change(fir.a,sec.a,fir.lenth,sec.lenth);
long long float_sec=0;
for(int i=sec.lenth-1;i>=0;i--)
float_sec=(float_sec*10)+sec.a[i];
ans=fir/float_sec;
}
ans.write();
return 0;
}
void change(int *f,int *s,int fl,int sl)
{
if(fl>sl) return;
else if(fl<sl) c=true;
else if(fl==sl)
{
for(int i=fl-1;i>=0;i--)
{
if(f[i]==s[i]) continue;
else if(f[i]<s[i])
{
c=true;
break;
}
else if(f[i]>s[i]) break;
}
}
}
其中高精度加减已经在洛谷和oj测试通过,代码部分参考了《c++一本通》中的模板