#include<iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char s1[200],s2[200],sum[210];
void add()
{
int lena=strlen(s1);
int lenb=strlen(s2);
int maxlen=(lena<lenb)?lenb:lena;
int carry=0,k=0;
while(lena-->0 && lenb-->0)
{
int left=s1[lena]-'0';
int right=s2[lenb]-'0';
int s=left+right+carry;
carry=s/10;
s%=10;
sum[k++]=s+'0';
}
if(lena<=0)
{
while(lenb-->0)
{
int left=0;
int right=s2[lenb]-'0';
int s=left+right+carry;
carry=s/10;
s%=10;
sum[k++]=s+'0';
}
}
else if(lenb<=0)
{
while(lena-->0)
{
int left=s1[lena]-'0';
int right=0;
int s=left+right+carry;
carry=s/10;
s%=10;
sum[k++]=s+'0';
}
}
if(carry!=0)
sum[k++]=carry+'0';
sum[k]='\0';
strrev(sum);
}
int main()
{
cin>>s1>>s2;
add();
cout<<"result is "<<sum<<endl;
return 0;
}
大数加法的字符串实现
最新推荐文章于 2022-12-06 13:41:47 发布