题目描述:
读入两个小于100的正整数A和B,计算A+B。需要注意的是:A和B的每一位数字由对应的英文单词给出。
输入:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。
输出:
对每个测试用例输出1行,即A+B的值。
样例输入:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
样例输出
3
90
96
相关代码及解析:
运用string.h中strcmp函数进行输入问题的解决,通过返回值确定相应的数值,再定义(a~g)7个空间为7的字符型数组,首先判断第b个数组是否为+号,如果是输入c和d数组,再判断d数组是否为=号,如果是则判断a和c是否为“zero”如果是则跳出循环,否则输出判断函数的返回值,如果首次判断真值为0,则判断5是否为等号 是的话返回判断函数(a)*10+函数(b)+函数(d),否则返回函数(a)*10+函数(b)+函数(d)*10+函数(e);
#include<stdio.h>
#include<string.h>
int A(char a[])
{
if(strcmp(a,"zero")==0)
return 0;
else if(strcmp(a,"one")==0)
return 1;
else if(strcmp(a,"two")==0)
return 2;
else if(strcmp(a,"three")==0)
return 3;
else if(strcmp(a,"four")==0)
return 4;
else if(strcmp(a,"five")==0)
return 5;
else if(strcmp(a,"six")==0)
return 6;
else if(strcmp(a,"seven")==0)
return 7;
else if(strcmp(a,"eight")==0)
return 8;
else if(strcmp(a,"nine")==0)
return 9;
}
int main()
{
char a[7]/*1*/,b[7]/*2*/,c[7]/*3*/,d[7]/*4*/,e[7]/*5*/,f[7]/*6*/,g[7]/*7*/;
while(~scanf("%s%s",a,b))
{
if(strcmp(b,"+")==0)//2是+
{
scanf("%s",c);//3
scanf("%s",d);//4
if(strcmp(d,"=")==0) //4是=
{
if(strcmp(a,"zero")==0&&strcmp(c,"zero")==0) break;
else printf("%d\n",A(a)+A(c));
}
else
{
scanf("%s",e);//5是=
printf("%d\n",A(a)+A(c)*10+A(d));
}
}
else
{
scanf("%s",c);//3
scanf("%s",d);//4
scanf("%s",e);//5
if(strcmp(a,"=")==0)//5是=
{
printf("%d\n",A(a)*10+A(b)+A(d));
}
else
{
scanf("%s",f);
printf("%d\n",A(a)*10+A(b)+A(d)*10+A(e));
}
}
}
return 0;
}