用英文单词模拟数学计算(输出要用英文单词)谢谢

问题描述:

用英文单词模拟数学计算(输出要用英文单词)谢谢
读入两个小于100的正整数A和B,计算A+B.需要注意的是:A和B的每一位数字由对应的英文单词给出.
具体的输入输出格式规定如下:
输入格式:测试输入包含若干测试用例,每个测试用例占一行,格式为 "A + B = ",相邻两字符串有一个空格间隔.当A和B同时为zero时输入结束,相应的结果不要输出.
输出格式:对每个测试用例输出1行,即A+B的值.
输入样例:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
输出样例:
three
nine zero
nine six
1个回答 分类:综合 2014-11-23

问题解答:

我来补答
#include
#include
#include
char num[10][6] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int parse(char * input)
{
int i = 0; int match = 0;
for (i = 0; i < 10; i++) {
match = strcmp(input,num[i]);
if (match == 0) return i;
}
return -1;
}
struct TStack {
int data;
struct TStack * next;
};
struct TStack * root = 0;
void stack_push(int data)
{
struct TStack * x = (struct TStack *)malloc(sizeof(struct TStack));
x->data = data;
x->next = root;
root = x;
}
int stack_pop(int * data)
{
struct TStack * x = root;
if (!x) return 0;
root = x->next ;
*data = x->data ;
free(x);
return 1;
}
int SumFromStack(void)
{
int x = 0,y = 0,z = 0,sum = 0;
int tens[] = {1,10,100,1000,10000};
do {
x = stack_pop(&y);
if (x == 0) break;
sum += y * tens[z++];
}while(z < 5); /*最大计算到五位数*/
return sum;
}
void print(int C)
{
char str[256]=""; size_t i = 0;
sprintf(str,"%d",C);
for (i = 0; i < strlen(str); i++) printf("%s ",num[str[i] - '0']);
printf("\n");
}
int main(void)
{
char str[256] = ""; int A = 0,B = 0,x = 0;
do {
A = 0; B = 0; root = 0; x = 0;
for(;;) {
str[0] = 0;
scanf("%s",str);
x = parse(str);
if (x < 0 ) {
if (str[0] == '+') A = SumFromStack();
if (str[0] == '=') {
B = SumFromStack();
break;
}
}else{
stack_push(x);
}
}
if (A || B) print(A + B);
}while(A || B);
system("pause");
return 0;
}
 
 
展开全文阅读
剩余:2000