在TC2.0下写了一个,显示了中间过程。看看有没有帮助,可以更优化,自己完善。
程序代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
main()
{ char *n[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int c1=0,c2=0,sum;
char *s,*p1,*p2;
char *temp;
char data1[50],data2[50];
int i,j;
char output[10];
system("cls");
gets(s);
printf("Source string is:%s\n",s);
p1=strtok(s,"+");
printf("Left of '+' is: %s\n",p1);
p2=strtok(NULL,"=");
printf("Betwwen of '+' and '=' is: %s\n",p2);
for(i=0;i<=9;i++)
{ temp=strstr(p1,n[i]);
while(temp)
{ *temp=i+'0';
for(j=1;j<=strlen(n[i])-1;j++)
*(temp+j)=' ';
temp=strstr(p1,n[i]);
}
}
for(i=0;i<=9;i++)
{ temp=strstr(p2,n[i]);
while(temp)
{ *temp=i+'0';
for(j=1;j<=strlen(n[i])-1;j++)
*(temp+j)=' ';
temp=strstr(p2,n[i]);
}
}
printf("Expression is:%s+%s\n",p1,p2);
strcpy(data1,p1);
strcpy(data2,p2);
printf("data1 is %s\n",data1);
printf("data2 is %s\n",data2);
j=0;
for (i=strlen(data1)-1;i>=0;i--)
{
if (isdigit(data1[i]))
{ c1=c1+(data1[i]-'0')*pow(10,j);
j++;
}
}
j=0;
for(i=strlen(data2)-1;i>=0;i--)
{
if (isdigit(data2[i]))
{ c2=c2+(data2[i]-'0')*pow(10,j);
j++;
}
}
sum=c1+c2;
printf("%d+%d=%d\n",c1,c2,sum);
itoa(sum,output,10);
printf("output is %s\n",output);
i=strlen(output);
for(j=0;j<=i-1;j++)
{ printf("%s ",n[output[j]-'0']);
}
getch();
}
[
本帖最后由 lianyicq 于 2015-7-9 16:17 编辑 ]