#include <bits/stdc++.h>
using namespace std;
//两个用字符串存储的整数进行相加操作,
//对非法的输入则返回error
#define INT_MAX 2147483647
int main()
{
string A,B;
while(cin>>A>>B)
{
int jinwei =0;
int cnt =0;
int sumstr[1000]={0};
int str;
int i=A.size()-1,j =B.size()-1;
int error =0;
while( i>=0&&j>=0)
{
if((A[i]<'0'||A[i]>'9')||(B[j]<'0'||B[j]>'9'))
{
error=1;
break;
}
str = A[i]-'0'+B[j]-'0'+jinwei;
if(str>=10)
{
str -=10;
jinwei =1;
}
else
jinwei =0;
sumstr[cnt] =str;
cnt ++;
i--,j--;
}
//字符串B长
if(i==-1)
{
for(int k=j;k>=0;k--)
{
if(B[k]<'0'||B[k]>'9')
{
error=1;
break;
}
str =B[k]-'0'+jinwei;
if(str>=10)
{
str -=10;
jinwei =1;
}
else
jinwei =0;
sumstr[cnt] =str;
cnt ++;
}
}
//字符串A长
if(j==-1)
{
for(int k = i;k>=0;k--)
{
if(A[k]<'0'||A[k]>'9')
{
error=1;
break;
}
str = A[k]-'0'+jinwei;
if(str>=10)
{
str -=10;
jinwei =1;
}
else
jinwei =0;
sumstr[cnt] =str;
cnt ++;
}
}
if(error)cout<<"error"<<endl;
else
{
if(jinwei)cout<<'1';
for(int i =cnt-1;i>=0;i--)
cout<<sumstr[i];
}
}
return 0;
}
模拟整数加法
最新推荐文章于 2022-11-27 18:46:54 发布