杭电1228 A +B

提议不解释,完全汉字。
关键是如何把字母转换为数字,这里要讲一个函数strcmp(字符串1,字符串2),它的作用是自左向右比较两个字符串的大小。
1:字符串1 == 字符串2 返回值为0;
2:字符串1 > 字符串2 返回值为正数;
3:字符串1 < 字符串2 返回值为负数。
其次是for循环的用法,且看代码。

#include<stdio.h>
#include<string.h>
int cmp(char a[])
{
    if(!strcmp(a,"zero")) return 0;
    if(!strcmp(a,"one")) return 1;
    if(!strcmp(a,"two")) return 2;
    if(!strcmp(a,"three")) return 3;
    if(!strcmp(a,"four")) return 4;
    if(!strcmp(a,"five")) return 5;
    if(!strcmp(a,"six")) return 6;
    if(!strcmp(a,"seven")) return 7;
    if(!strcmp(a,"eight")) return 8;
    if(!strcmp(a,"nine")) return 9;
    return 0;
}
int main()
{
    char a[10];
    int t1,t2;
    while(1)
    {
        for(t1 = 0,scanf("%s",a);a[0] != '+';scanf("%s",a))
            t1 = t1*10 + cmp(a);
        for(t2 = 0,scanf("%s",a);a[0] != '=';scanf("%s",a))
            t2 = t2*10 + cmp(a);
        if(t1 + t2 == 0) break;
        printf("%d\n",t1+t2);
    }
    return 0;
}

你可能感兴趣的:(函数)