都是用很直觀且暴力的方法去解的
main裡面先判斷長度
其實有些根本不用判斷
然後我的除法...寫得太複雜了
建議去看其他大神的寫法
以下程式碼
#include <bits/stdc++.h>
using namespace std;
void add(int * input1,int * input2,int len1,int len2)
{
int temp=0,k=len1-len2,i,temp1;
for(i=len1-1;i>=k;--i)
{
temp1=(input1[i]+input2[i-k]+temp)%10;
temp=(input1[i]+input2[i-k]+temp)/10;
input1[i]=temp1;
}
while(1)
{
if(temp!=0&&i>=0)
{
input1[i]=(input1[i]+temp)%10;
temp=(input1[i]+temp)/10;
i--;
}
else
break;
}
if(temp>0)
cout<<temp;
for(i=0;i<len1;++i)
cout<<input1[i];
cout<<endl;
}
void subtract(int * input1,int * input2,int len1,int len2,int check)
{
int k=len1-len2,i,output_check=1;
for(i=len1-1;i>=k;--i)
{
if(input1[i]-input2[i-k]<0)
{
int j=i;
while(1)
{
--j;
if(input1[j]>0&&j>=0)
{
--input1[j];
break;
}
else
input1[j]=9;
}
input1[i]=input1[i]-input2[i-k]+10;
}
else
input1[i]=input1[i]-input2[i-k];
}
if(check==1)
cout<<'-';
for(i=0;i<len1;++i)
{
if(input1[i]!=0&&output_check==1)
output_check=0;
if(output_check==0)
cout<<input1[i];
if(i==len1-1&&output_check==1)
cout<<0;
}
cout<<endl;
}
void multiply(int * input1,int * input2,int len1,int len2)
{
int output[len1+len2]={0};
int temp,output_check=1,k;
for(int i=len2-1;i>=0;--i)
{
temp=0;
for(int j=len1+i,k=len1-1;j>=i,k>=-1;--j,--k)
{
if(k==-1&&temp!=0)
{
int count=j;
while(1)
{
if(temp!=0&&j>=0)
{
output[count]+=temp;
temp=output[count]/10;
output[count]%=10;
++count;
}
else
break;
}
}
else if(k>-1)
{
output[j]+=(input2[i]*input1[k])%10+temp;
temp=(input2[i])*(input1[k])/10+output[j]/10;
output[j]%=10;
}
}
}
for(int i=0;i<len1+len2;++i)
{
if(output[i]!=0&&output_check==1)
output_check=0;
if(output_check==0)
cout<<output[i];
}
cout<<endl;
}
void divide(int * input1,int * input2,int len1,int len2)
{
int output[len1-len2+1]={0},output_count=0,temp=0,check=0,output_check=1;
for(int i=0;i<len1-len2+1;++i)
{
while(1)
{
if(input1[i]<input2[0]||input1[i]==0)
{
output[output_count]=temp;
temp=0;
++output_count;
break;
}
else if(input1[i]>input2[0]||check==1)
{
int k=len2;
for(int j=i+len2-1;j>=i;--j)
{
if(input1[j]<input2[k-1])
{
int l=j;
while(1)
{
--l;
if(input1[l]>0&&l>=i)
{
--input1[l];
break;
}
else
input1[l]='9';
}
input1[j]=input1[j]-input2[k-1]+10;
}
else
input1[j]=input1[j]-input2[k-1];
--k;
}
++temp;
check=0;
}
else if(input1[i]==input2[0])
{
int k=0;
for(int j=i;j<i+len2;++j)
{
if(input1[j]>input2[k])
{
check=1;
break;
}
else if(input1[j]<input2[k])
{
input1[i+1]+=input1[i]*10;
input1[i]=0;
output[output_count]=temp;
temp=0;
++output_count;
break;
}
else if(j==i+len2-1)
{
for(int l=i;l<i+len2;++l)
input1[l]=0;
output[output_count]=temp+1;
temp=0;
++output_count;
}
++k;
}
if(check!=1)
break;
}
}
}
for(int i=0;i<output_count;++i)
{
if(output[i]!=0&&output_check==1)
output_check=0;
if(output_check==0)
cout<<output[i];
}
cout<<endl;
}
int main()
{
string ptr1,ptr2;
char a;
while(cin>>ptr1>>a>>ptr2)
{
int *input1=new int [ptr1.length()];
int *input2=new int [ptr2.length()];
for(int i=0;i<ptr1.length();++i)
input1[i]=(int)ptr1[i]-48;
for(int i=0;i<ptr2.length();++i)
input2[i]=(int)ptr2[i]-48;
if(a=='+')
{
int temp;
if(ptr1.length()>ptr2.length())
add(input1,input2,ptr1.length(),ptr2.length());
else
add(input2,input1,ptr2.length(),ptr1.length());
}
else if(a=='-')
{
if(ptr1.length()>ptr2.length())
subtract(input1,input2,ptr1.length(),ptr2.length(),0);
else if(ptr1.length()<ptr2.length())
subtract(input2,input1,ptr2.length(),ptr1.length(),1);
else
{
for(int i=0;i<ptr1.length();++i)
if(input1[i]>input2[i])
{
subtract(input1,input2,ptr1.length(),ptr2.length(),0);
break;
}
else if(input1[i]<input2[i])
{
subtract(input2,input1,ptr2.length(),ptr1.length(),1);
break;
}
else if(i==ptr1.length()-1)
subtract(input1,input2,ptr1.length(),ptr2.length(),0);
}
}
else if(a=='*')
{
if(input1[0]==0||input2[0]==0)
cout<<0<<endl;
else if(ptr1.length()>ptr2.length())
multiply(input1,input2,ptr1.length(),ptr2.length());
else
multiply(input2,input1,ptr2.length(),ptr1.length());
}
else if(a=='/')
{
if(ptr1.length()>ptr2.length())
divide(input1,input2,ptr1.length(),ptr2.length());
else if(ptr1.length()<ptr2.length())
cout<<0<<endl;
else
{
for(int i=0;i<ptr1.length();++i)
if(input1[i]>input2[i])
{
divide(input1,input2,ptr1.length(),ptr2.length());
break;
}
else if(input1[i]<input2[i])
{
cout<<0<<endl;
break;
}
else if(i==ptr1.length()-1)
cout<<1<<endl;
}
}
delete [] input1;
delete [] input2;
}
return 0;
}