A+B Problem Time Limit:1Sec Input File:aplusb.in Calculate a+bInputthe input will consist of a series of pairs of integers a and b,separated by one line.Each integar may consist of less than 79 digits.Output For each pair of input integers a and byou
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/29 01:24:42
A+B Problem Time Limit:1Sec Input File:aplusb.in Calculate a+bInputthe input will consist of a series of pairs of integers a and b,separated by one line.Each integar may consist of less than 79 digits.Output For each pair of input integers a and byou
A+B Problem Time Limit:1Sec Input File:aplusb.in Calculate a+b
Input
the input will consist of a series of pairs of integers a and b,separated by one line.Each integar may consist of less than 79 digits.
Output
For each pair of input integers a and byou should output the sum of a and b in one line.
这是钱能C++程序设计第二版第十一章的课后题,意思是输出两个最多79位的整数的和,希望各位大神能提供一些想法和源代码.
附上原题图片,
A+B Problem Time Limit:1Sec Input File:aplusb.in Calculate a+bInputthe input will consist of a series of pairs of integers a and b,separated by one line.Each integar may consist of less than 79 digits.Output For each pair of input integers a and byou
//DEV CPP编译通过~
#include
#include
#include
#define MAX_SIZE 100 // 最大位数
char n1[MAX_SIZE] = ""; // 第一个整数
char n2[MAX_SIZE] = ""; // 第二个整数
char n3[MAX_SIZE+1] = ""; // 两个整数之和
void do_sum();
int main(void)
{
//读取两个整数
printf("第一个整数为:");
scanf("%s", n1);
printf("第二个整数为:");
scanf("%s", n2);
do_sum();//求和
FILE *fp;
if((fp=fopen("aplusb.in","w"))==NULL)//保存结果
{
fprintf(stderr,"不能打开文件.\n");
exit(1);
}
int size=strlen(n3);
for(int n=0;n= 0) && (Len2 >= 0); Len1--, Len2--)
{
n3[n] = n1[Len1] - '0' + n2[Len2] - '0' + Carry;
Carry = n3[n] / 10;
n3[n] %= 10;
n++;
}
for(; Len1 >= 0; Len1--)
{
n3[n] = n1[Len1] - '0' + Carry;
Carry = n3[n] / 10;
n3[n] %= 10;
n++;
}
for(;Len2 >= 0; Len2--)
{
n3[n] = n2[Len2] - '0' + Carry;
Carry = n3[n] / 10;
n3[n] %= 10;
n++;
}
printf("两数之和为");
for(n--; n >= 0; n--)
{
printf("%d", n3[n]);
}//输出和
printf("\n");
}