#include <iostream>
#include<string.h>
using namespace std;
#define MAXSIZE 104
void Add(char *str1, char *str2, char *str3)
{
int i,j,i1,i2,tmp,carry;
int len1 = strlen(str1), len2 = strlen(str2);
char ch;
i1 = len1-1; i2 = len2-1;
j = carry = 0;
for(; i1>=0&& i2>=0; j++, i1--, i2--)
{
tmp = str1[i1] -'0' + str2[i2] -'0' + carry;
carry = tmp/10;
str3[j] = tmp%10 + '0';
}
while(i1>=0)
{
tmp = str1[i1--] - '0' + carry;
carry = tmp/10; ///
str3[j++] = tmp%10 +'0';
}
while(i2>=0)
{
tmp = str2[i2--] - '0' + carry;
carry = tmp/10;
str3[j++] = tmp%10 +'0';
}
if(carry)
str3[j++] = carry + '0';
str3[j] = '\0';
for(i = 0, j--;i<j; i++, j--)
{
ch = str3[i];
str3[i] = str3[j];
str3[j] = ch;
}
}
int main()
{
int c;
cin>>c;
char str1[MAXSIZE], str2[MAXSIZE],str3[MAXSIZE];
while(c >0)
{
str1[0] = '0'; str1[1] = '\0';
while(cin>>str2 && strcmp("0",str2) != 0)
{
memset(str3,'0',sizeof(str3));
Add(str1, str2,str3);
strcpy(str1,str3);
}
cout<<str1<<endl;
c--;
if(c>0)
cout<<endl;
}
return 0;
}
多个大数相加问题
最新推荐文章于 2021-05-19 00:08:07 发布