WA line 8 錯處不明求教
#include <iostream>
#include <string>
using namespace std;
int main()
{
string num1,num2,s;
int l,n1,n2,k,i;
char c;
while (cin >> num1 >> c >> num2 )
{
l=0;
if (num1.size()<num2.size())l=1,s=num1,num1=num2,num2=s;
if (num1.size()==num2.size()){
k=num1.size();
for (int i=0;i<k;i++)
{
if (num1[i]>num2[i]) break;
if (num1[i]<num2[i])
{
l=1;
s=num1,num1=num2,num2=s;
break;
}
}
}
n1=num1.size();
n2=num2.size();
int m1[n1],m2[n2];
for (k=0;k<n1;k++)m1[k]=num1[k]-48;
for (k=0;k<n2;k++)m2[k]=num2[k]-48;
switch (c){
case '+':
{
int ans[n1],d;
d=n1-n2;
for (k=n1-1;k>=d;k--) ans[k]=m1[k]+m2[k-d];
for (;k>=0;k--) ans[k]=m1[k];
for (k=n1-1;k>0;k--) ans[k-1]=ans[k-1]+ans[k]/10,ans[k]=ans[k]%10;
for (k=0;k<n1;k++) cout << ans[k];
cout << endl;
break;
}
case '-':
{
int ans[n1],d,p;
d=n1-n2;
for (k=n1-1;k>=d;k--) ans[k]=m1[k]-m2[k-d];
for (;k>=0;k--) ans[k]=m1[k];
for (k=n1-1;k>0;k--) ans[k-1]=ans[k-1]-1+(ans[k]+10)/10,ans[k]=(ans[k]+10)%10;
cout << (l==1?"-":"");
for (k=p=0;k<n1;k++)
{
if (p==0 && ans[k]!=0)p=1;
if (p) cout << ans[k];
}
cout << endl;
break;
}
case '*':
{
int ans[n1+n2-1];
for (k=0;k<n1+n2-1;k++) ans[k]=0;
for (k=0;k<n1;k++)
{
for (i=0;i<n2;i++) ans[i+k]=ans[i+k]+m1[k]*m2[i];
}
for (k=n1+n2-2;k>0;k--) ans[k-1]=ans[k-1]+ans[k]/10,ans[k]=ans[k]%10;
for (k=0;k<n1+n2-1;k++)cout << ans[k];
cout << endl;
break;
}
case '/':
{
int ans[n1-n2+1],d,p,d1,d2;
if (l==1)
{
cout << "0" << endl;
break;
}
else{
for (k=0;k<=n1-n2;k++)
{
d1=(n1<2?m1[k]:m1[k]*10+m1[k+1]);
d2=(n2<=1?m2[0]:m2[0]*10+m2[1]);
ans[k]=d1/d2;
for (i=0;i<n2;i++) m1[i]=m1[i]-m2[i]*ans[k];
for (i=n1-1;i>k;i--)
{
p=(m1[i]<0?(m1[i]-10)/10:m1[i]/10);
m1[i-1]=m1[i-1]+p;
m1[i]=m1[i]-10*p;
}
if (k!=0) m1[k]=m1[k]+m1[k-1]*10,m1[k-1]=0;
}
for (k=0;k<n1-n2+1;k++) cout << ans[k];
cout << endl;
}
break;
}
}
}
return 0;
}