求助,有哪位大牛可以帮我写一下这道题,谢谢。。。
用英文单词模拟数学计算
读入两个小于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
[解决办法]
自己读取字符串然后进行解析吧。
[解决办法]
one + two =3是咋算的?
反过来就行了吧
#include <stdio.h>#include <stdlib.h>#include <string.h>//字符串转换成数字int str2int(char a[10]){ char c, d; int num; c = a[0]; d = a[1]; switch (c) { case 'z': num = 0;break; case 'o': num = 1;break; case 't': switch(d) { case 'w': num = 2;break; case 'h': num = 3;break; }break; case 'f':switch(d) { case 'o':num = 4;break; case 'i':num = 5;break; }break; case 's':switch(d) { case 'i':num = 6;break; case 'e':num = 7;break; }break; case 'e':num = 8;break; case 'n':num = 9;break; } return num;}//数字转换成字符串char *int2str(char n){ switch(n) { case '1': return "one ";break; case '2': return "two ";break; case '3': return "three ";break; case '4': return "four ";break; case '5': return "five ";break; case '6': return "six ";break; case '7': return "seven ";break; case '8': return "eight ";break; case '9': return "nine ";break; case '0': return "zero ";break; default: return "\n"; }}int main(){ FILE *fp, *out; char temp[6]; char *p; int num1, num2; if ((fp = fopen("in.txt", "r")) == NULL)//in.txt放置原始输入数据 { printf("cannot open file !\n"); exit(0); } if ((out = fopen("temp1.txt", "w")) == NULL)//temp1.txt放置转换成数字后的数据 { printf("cannot open file !\n"); exit(0); } while (!feof(fp)) { fscanf(fp, "%s", temp); if (strcmp(temp, "+") && strcmp(temp, "=")) { fprintf(out, "%d", str2int(temp)); } else { fprintf(out, "\n", 1); } } fclose(fp); fclose(out); if ((fp = fopen("temp1.txt", "r")) == NULL) { printf("cannot open file !\n"); exit(0); } if ((out = fopen("temp2.txt", "w")) == NULL)//temp2.txt放置计算后的数字结果 { printf("cannot open file !\n"); exit(0); } while (!feof(fp)) { fscanf(fp, "%d\n%d", &num1, &num2); if (!num1 || !num2) { break; } else { fprintf(out, "%d\n", num1+num2); } } fclose(fp); fclose(out); if ((fp = fopen("temp2.txt", "r")) == NULL) { printf("cannot open file !\n"); exit(0); } if ((out = fopen("out.txt", "w")) == NULL)//out.txt放置最后结果 { printf("cannot open file !\n"); exit(0); } while (!feof(fp)) { p = int2str(fgetc(fp)); fprintf(out, "%s", p); } fclose(fp); fclose(out); return 0;}
[解决办法]
这题是hdu 1228吧
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char strr[10][16]={"zero","one","two","three","four","five","six","seven","eight","nine"};char str[16];int Find(char st[]){ int i; for(i=0;i<=9;i++) if(strcmp(st,strr[i])==0) return i;}int main(){ //freopen("in.txt","r",stdin); int i1,i2,a,b; while(1) { a=b=0; scanf("%s",str); i1=Find(str); a=i1; scanf("%s",str); while(strcmp(str,"+")!=0) { i2=Find(str); a=i2+i1*10; scanf("%s",str); } //printf("%d\n",a); scanf("%s",str); i1=Find(str); b=i1; scanf("%s",str); while(strcmp(str,"=")!=0) { i2=Find(str); b=i2+i1*10; scanf("%s",str); } //printf("%d\n",b); if(a+b==0) break; else printf("%d\n",a+b); } return 0;}
[解决办法]
也来一个,看看咋样~~~
#include<stdio.h>char *p[]={"zero","one","two","three","four","five","six","seven","eight","nine","+","="};void outInt(unsigned int n){ if(n>=10)outInt(n/10); printf("%s ",p[n%10]);}int main(){ FILE *f=fopen("D:\\1.txt","r"); char buff[10]; int n1,n2,n3,m,i; while(!feof(f)) { n1=n2=m=0; while(1) { fscanf(f," %s",buff); for(i=0;i<12;++i) { if(strcmp(buff,p[i])==0) { if(i==11)goto Calc; if(i==10)m=1; else if(m)n2=n2*10+i; else n1=n1*10+i; break; } } }Calc: n3=n1+n2; if(n3!=0)outInt(n3); printf("\n"); } fclose(f); return 0;}