看来作为码农得天天写代码啊[hdu1228]

直接上代码:

1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4
5
6  const char* numbers[]={"zero","one","two","three","four","five","six",\
7 "seven","eight","nine",NULL};
8
9  int read_word(char*num,char**at)
10 {
11 char* start;
12 if(!**at)
13 return 0;
14
15 start = *at;
16 while(**at!=' ' && **at!='\0')
17 {
18 (*at)++;
19 }
20 **at = '\0';
21 (*at)++;
22 if(*start <'a' || *start > 'z')
23 {
24 return 0;
25 }
26 strcpy(num,start);
27 //now skip to the nonspace
28   while(**at==' ' && **at!='\0')
29 (*at)++;
30 return 1;
31 }
32
33 int word2num(const char* num)
34 {
35 int rt=0;
36 const char** i = numbers;
37 while(*i)
38 {
39 if(!strcmp(*i,num))
40 return rt;
41 rt++;
42 i++;
43 }
44 return rt;
45 }
46
47
48 int main()
49 {
50 char* line;
51 char* num;
52 int done=0;
53 line = (char*)malloc(50*sizeof(char));
54 num = (char*)malloc(10*sizeof(char));
55
56
57 while(!done)
58 {
59 int add11,add12,add21,add22;
60 char* at;
61 int rt;
62 fgets(line,50,stdin);
63
64 add11=add12=add21=add22=0;
65 at = line;
66
67 //oprand 1
68 rt = read_word(num,&at);
69 if(!rt)
70 {
71 printf("input error!\n");
72 return 0;
73 }
74 add11 = word2num(num);
75 //read the second num and skip the + or skip the +
76 if(read_word(num,&at))
77 {
78 add12 = word2num(num);
79 add11=add11*10;
80 read_word(num,&at);//skip the +
81 }
82 //anther oprand
83 read_word(num,&at);
84 add21 = word2num(num);
85 if(read_word(num,&at))
86 {
87 add21=add21*10;
88 add22= word2num(num);
89 }
90 if(add11==0&&add12==0&&add21==0&&add22==0)
91 {
92 done = 1;
93 break;
94 }
95 printf("%d\n",add11+add12+add21+add22);
96 }
97 free(num);
98 free(line);
99 return 0;
100 }
101
102
103
104

顺便说一下,hdu的gcc编译器什么版本,连//风格的注释都不支持...囧!

posted on 2011-03-07 21:00  Weifeng Wang  阅读(534)  评论(0编辑  收藏  举报

导航