求助,有哪位大牛可以帮我写一下这道题,谢谢。。。

SmallKind 2012-05-13 04:13:59
用英文单词模拟数学计算
读入两个小于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
...全文
209 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
SmallKind 2012-05-13
  • 打赏
  • 举报
回复
谢谢各位大牛啦。。
sxldfang 2012-05-13
  • 打赏
  • 举报
回复
也来一个,看看咋样~~~


#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;
}


D:\1.txt 的内容:
one + two =  
three four + five six =
zero seven + eight nine =
zero + zero =


屏幕输出:
three
nine zero
nine six


请按任意键继续. . .
hen_hao_ji 2012-05-13
  • 打赏
  • 举报
回复
这题是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;
}



DataChat.Club 2012-05-13
  • 打赏
  • 举报
回复

#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;
}
SmallKind 2012-05-13
  • 打赏
  • 举报
回复
汗,我在看看。。谢谢
代码Dog 2012-05-13
  • 打赏
  • 举报
回复
你把每个数字字符串与具体的数字映射起来,计算結果反映射输出就行,不用字符串解析这么麻烦。。
DataChat.Club 2012-05-13
  • 打赏
  • 举报
回复
one + two =3是咋算的?

反过来就行了吧

[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
自己读取字符串然后进行解析吧。

就是那个最后求出结果的不是数字,就是在把他转换成字符怎么写,就是这个不知道写啊,比如one + two =3,然后在把3转换成three,这个不知道啦,哎,很纠结,麻烦啊
[/Quote]
SmallKind 2012-05-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
自己读取字符串然后进行解析吧。
[/Quote]
就是那个最后求出结果的不是数字,就是在把他转换成字符怎么写,就是这个不知道写啊,比如one + two =3,然后在把3转换成three,这个不知道啦,哎,很纠结,麻烦啊
W170532934 2012-05-13
  • 打赏
  • 举报
回复
自己读取字符串然后进行解析吧。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧