九度oj 1010 A+B

题目描述:
读入两个小于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
这本来是一到水题,但一开始不太会用strcmp函数,又知道scanf(%s)会按空格分开读,所以一开始写出了如下的代码
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <string>
 5 
 6 #define HIGH 11
 7 #define LEN 8
 8 #define MAX 102
 9 using namespace std;
10 char s[HIGH][LEN] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
11 
12 char source[MAX];
13 char A[MAX];
14 char B[MAX];
15 int main(int argc, char const *argv[])
16 {
17     int a,b;
18     while(gets(source)) {
19         int i = 0;
20         a = 0;
21         b = 0;
22         int state = 0;
23         while(i < strlen(source)) {
24             if(source[i] == '+') {
25                 state = 1;
26                 i++;
27                 continue;
28             }
29             if(source[i] == ' ' || source[i] == '=') {
30                 i++;
31                 continue;
32             }
33             for(int j = 0; j < 10; j++) {
34                 if(source[i] == s[j][0]) {
35                     if(source[i] == 't') {
36                         if(source[i+1] == 'w') {
37                             j = 2;
38                         }
39                         else if(source[i+1] == 'h') {
40                             j = 3;
41                         }
42                     }
43                     else if(source[i] == 'f') {
44                         if(source[i+1] == 'o') {
45                             j = 4;
46                         }
47                         else if(source[i+1] == 'i') {
48                             j = 5;
49                         }
50                     }
51                     else if(source[i] == 's') {
52                         if(source[i+1] == 'e') {
53                             j = 7;
54                         }
55                         else if(source[i+1] == 'i') {
56                             j = 6;
57                         }
58                     }
59                     if(state == 0) {
60                         a = 10 * a + j;
61                     }
62                     else if(state == 1) {
63                         b = 10 * b + j;
64                         
65                     }
66                     i = i + strlen(s[j]);
67                     break;
68                 }
69                 
70             }
71             
72         }
73         if(a == 0 && b == 0) {
74             break;
75         }
76         int c = a + b;
77         printf("%d\n",c);
78     }
79     return 0;
80 }

但后来发现,scanf的这个性质其实可以利用起来,所以写出如下的代码

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <string>
 5 
 6 #define HIGH 11
 7 #define LEN 8
 8 #define MAX 102
 9 using namespace std;
10 char s[HIGH][LEN] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
11 
12 int main(int argc, char const *argv[])
13 {
14     char temp[MAX];
15     int state = 0;
16     int a = 0, b = 0;
17     while(scanf("%s",temp)) {
18         if(strcmp(temp,"+") == 0) {
19             state = 1;
20             continue;
21         }
22 
23         if(strcmp(temp,"=") == 0) {
24             if(a == 0 && b == 0) {
25                 break;
26             }
27             else {
28                 printf("%d\n",a+b);
29                 state = 0;
30                 a = 0;
31                 b = 0;
32                 continue;
33             }
34         }
35         for(int i = 0; i < 10; i++) {
36             if(strcmp(temp,s[i])== 0) {
37                 if(state == 0) {
38                     a = a * 10 + i;
39                 }
40                 else {
41                     b = b * 10 + i;
42                 }
43                 continue;
44             }
45         }
46 
47     }
48 
49     return 0;
50 }

需要注意的是,strcmp这个函数,

若str1=str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。
所以判断是否相等要判断其值是否为0
posted @ 2016-07-16 13:19  Jason杰  阅读(293)  评论(0编辑  收藏  举报