2018 South University computer examination questions b
Title Description
Reads two positive integer less than A and B 100 to calculate A + B, Note: A + each bit is given by the corresponding English letter B.
Entry
Test input comprising a plurality of test cases, each test case per line, the format of "A + B =", there is a space between two adjacent strings interval. When A and B are simultaneously input end is zero, do not output the corresponding results.
Export
An output line for each test case, i.e., the value of A + B.
Sample input
one + two =
three four + five six =
zero + zero =
Sample Output
3
90
Topic ideas
This question and talked before the word replace this idea is the same title, the word into a string replacement is about words, this question is the same. Then calculated. The Code
#include<stdio.h>
#include<string.h>
char digit[12][10] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int judge(char *s)
{
for(int i = 0; i < 10; i++)
{
if(!strcmp(digit[i], s))
return i;
}
}
int main()
{
char str[100];
int len, p;
char word[20][20];
int a, b, pow, temp, d[2][2], j;
while(gets(str))
{
int l = strlen(str);
p = len = 0;
for(int i = 0; i < l; i++)
{
if(str[i] == '=')
{
continue;
}
if(str[i] == ' ')
{
word[len++][p] = '\0';
p = 0;
}
else
word[len][p++] = str[i];
}
pow = 1;
p = 1;
j = 0;
memset(d, 0, sizeof(d));
for(int i = len - 1; i >= 0; i--)
{
if(!strcmp(word[i], "+"))
{
p = 1;
j++;
continue;
}
d[j][p] = judge(word[i]);
p--;
}
a = d[0][0] * 10 + d[0][1];
b = d[1][0] * 10 + d[1][1];
if(a == b && b == 0)
break;
printf("%d\n", a+b);
}
return 0;
}