#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f
using namespace std;
int main()
{
int vis,test;
char a[1000],b[1000],h;
int c[1000];
while( scanf("%d",&test)!= EOF)
{
scanf("%c",&h);
for(int i = 1 ; i <= test ;i++)
{
scanf("%s",a);
scanf("%c",&h);
scanf("%s",b);
printf("Case %d:\n",i);
if(strlen(a) >= strlen(b))
{
for(int j = 0 ;j < strlen(a) ;j++)
c[j] = a[j] - '0';
for(int j = 1 ;j <= strlen(b) ;j++)
c[strlen(a)-j] = a[strlen(a) - j]-'0' + b[strlen(b) - j]-'0';
for(int j = strlen(a)-1 ;j > 0 ;j--)
{
if(c[j] >= 10)
{
c[j-1] = c[j-1] + c[j]/10;
c[j] = c[j]%10;
}
}
printf("%s + %s = ",a,b);
for(int j = 0 ;j < strlen(a) ;j++)
printf("%d",c[j]);
}
else
{
for(int j= 0 ;j < strlen(b) ;j++)
c[j] = b[j] - '0';
for(int j = 1 ;j <= strlen(a) ;j++)
c[strlen(b)-j] = a[strlen(a) - j]-'0' + b[strlen(b) - j]-'0';
for(int j = strlen(b)-1 ;j > 0 ;j--)
{
if(c[j] >= 10)
{
c[j-1] = c[j-1]+c[j]/10;
c[j] = c[j]%10;
}
}
printf("%s + %s = ",a,b);
for(int j = 0 ;j < strlen(b) ;j++)
printf("%d",c[j]);
}
printf("\n");
if(i != test)
printf("\n");
}
}
}
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f
using namespace std;
int main()
{
int vis,test;
char a[1000],b[1000],h;
int c[1000];
while( scanf("%d",&test)!= EOF)
{
scanf("%c",&h);
for(int i = 1 ; i <= test ;i++)
{
scanf("%s",a);
scanf("%c",&h);
scanf("%s",b);
printf("Case %d:\n",i);
if(strlen(a) >= strlen(b))
{
for(int j = 0 ;j < strlen(a) ;j++)
c[j] = a[j] - '0';
for(int j = 1 ;j <= strlen(b) ;j++)
c[strlen(a)-j] = a[strlen(a) - j]-'0' + b[strlen(b) - j]-'0';
for(int j = strlen(a)-1 ;j > 0 ;j--)
{
if(c[j] >= 10)
{
c[j-1] = c[j-1] + c[j]/10;
c[j] = c[j]%10;
}
}
printf("%s + %s = ",a,b);
for(int j = 0 ;j < strlen(a) ;j++)
printf("%d",c[j]);
}
else
{
for(int j= 0 ;j < strlen(b) ;j++)
c[j] = b[j] - '0';
for(int j = 1 ;j <= strlen(a) ;j++)
c[strlen(b)-j] = a[strlen(a) - j]-'0' + b[strlen(b) - j]-'0';
for(int j = strlen(b)-1 ;j > 0 ;j--)
{
if(c[j] >= 10)
{
c[j-1] = c[j-1]+c[j]/10;
c[j] = c[j]%10;
}
}
printf("%s + %s = ",a,b);
for(int j = 0 ;j < strlen(b) ;j++)
printf("%d",c[j]);
}
printf("\n");
if(i != test)
printf("\n");
}
}
}
本文介绍了一个通过读取输入的数字和运算符,进行加法运算并输出结果的程序。程序采用C语言实现,通过循环和条件判断,对输入的字符串进行解析,并将字符转换为整数进行加法计算。
235

被折叠的 条评论
为什么被折叠?



