69,369
社区成员
发帖
与我相关
我的任务
分享
#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;
}
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
three
nine zero
nine six
请按任意键继续. . .
#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>
#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;
}