九度OJ 题目1010:A + B

来源:互联网 发布:网络心理学的杂志 编辑:程序博客网 时间:2024/05/16 19:35

一.题目描述:
读入两个小于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

二.题目分析:

简单的模拟过程,注意输入的技巧,整数最大位数是两位,不要使用gets读入整行了。。。直接使用scanf读入字符串加上strcmp既可以很快的实现字符串转整数。

三.代码:

#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX 50int StrToInt(char *str){    if(strcmp(str,"zero")==0)        return 0;    else if(strcmp(str,"one")==0)        return 1;    else if(strcmp(str,"two")==0)        return 2;    else if(strcmp(str,"three")==0)        return 3;    else if(strcmp(str,"four")==0)        return 4;    else if(strcmp(str,"five")==0)        return 5;    else if(strcmp(str,"six")==0)        return 6;    else if(strcmp(str,"seven")==0)        return 7;    else if(strcmp(str,"eight")==0)        return 8;    else if(strcmp(str,"nine")==0)        return 9;    return -1;}int main(){    int i,j,k,a[2],b[2];    char str[MAX],ch;    i=0;    while(1)    {        scanf("%s",str);        a[0]=StrToInt(str);        scanf("%s",str);        if(strcmp(str,"+")==0)            a[1]=-1;        else        {            a[1]=StrToInt(str);            scanf("%s",str);    //+        }        //scan b        scanf("%s",str);        b[0]=StrToInt(str);        scanf("%s",str);        if(strcmp(str,"=")==0)            b[1]=-1;        else        {            b[1]=StrToInt(str);            scanf("%s",str); //=        }        int x,y;        if(a[1]==-1)            x=a[0];        else            x=a[0]*10+a[1];        if(b[1]==-1)            y=b[0];        else            y=b[0]*10+b[1];        if(x==0&&y==0)            break;        printf("%d\n",x+y);    }    return 0;}



0 0