A + B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
Read two integers A and B smaller than 100 and calculate A + B.
Note that each digit of A and B is given by the corresponding English word.
Input
The test input contains several test cases. Each test case occupies one line in the format of "A + B =". There is A space interval between two adjacent strings. when both A and B are 0, the input ends. Do not output the corresponding results.
Output
Output one line for each test case, that is, the value of A + B.
Sample Input
One + two =
Three four + five six =
Zero seven + eight nine =
Zero + zero =
Sample Output
3
90
96
Solution: Consider the equation to be processed as a string and use an s1 character array to record lower-case letters. When a word ends in the space age, then compare the word with the word of each digit to find out what the word represents. record the number and clear the elements in s1, save the word again from s1 [0] until the end of the string, and find the integers represented by the plus and minus parts.
AC code:
# Include <stdio. h> # include <string. h> int main () {char str [30], s1 [20]; int I, j, len, s, sum1, sum2, flag; while (gets (str )! = NULL) {len = strlen (str); sum1 = sum2 = 0; flag = 0; for (I = 0, j = 0; I <len; I ++) {if (str [I]> = 'A' & str [I] <= 'Z ') /* if it is a lowercase letter */s1 [j ++] = str [I]; /* save in s1 */else if (str [I] = ''& str [I-1]! = '+')/* Judge str [I-1]! = '+' Is used to avoid processing + as a string */{s1 [j] = '\ 0';/* never forget the string end flag */if (! Strcmp (s1, "zero") s = 0; else if (! Strcmp (s1, "one") s = 1; else if (! Strcmp (s1, "two") s = 2; else if (! Strcmp (s1, "three") s = 3; else if (! Strcmp (s1, "four") s = 4; else if (! Strcmp (s1, "five") s = 5; else if (! Strcmp (s1, "six") s = 6; else if (! Strcmp (s1, "seven") s = 7; else if (! Strcmp (s1, "eight") s = 8; else if (! Strcmp (s1, "nine") s = 9;/* compare the string and find the represented number */j = 0;/* Remember to start from scratch */if (! Flag) sum1 = sum1 * 10 + s;/* sum1 save + the number represented in the previous section */else sum2 = sum2 * 10 + s; /* sum2 save + the number in the following section */} else if (str [I] = '+') flag = 1; /* mark + appears */} if (sum1 = 0 & sum2 = 0)/* terminate the program when both values are 0 */break; else printf ("% d \ n", sum1 + sum2);} return 0 ;}