题意就是:给你一个小于999,,999,999的数字的英语表达式,让你写出原数字。。。。。。题目不难,只是有点复杂。。。。。。用到了字符串的比较与分离。。。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long long num;
int main()
{
int t,n,m,i,j,k,g,len,flag;
char s[200],s1[20],ta[32][15]={"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen", "fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen","twenty","thirty","forty", "fifty",
"sixty","seventy","eighty","ninety","hundred","thousand","million","and"};
scanf("%d",&t);
getchar();
while(t--){
gets(s);
len=strlen(s);
num=0; k=len;
flag=0; n=1;
m=1;
for(i=len-1;i>=0;i--){
g=0;
if(s[i]==' '){
memset(s1,0,sizeof(s1));
for(j=i+1;j<k;j++)
s1[g++]=s[j];
k=i; flag=1;
for(j=0;j<32;j++){
if(strcmp(s1,ta[j])==0){
if(j>=0&&j<=20) { num+=n*j; } //num值在累加,n一直在更新。。。
else if(j>20&&j<28) { num+=(j-20+2)*10*n; }
else if(j==28) n=100*m;
else if(j==29) { n=1000*m; m=1000; }
else if(j==30) { n=1000000; m=1000000; }
break;
}
}
}
}
if(flag){
g=0;
memset(s1,0,sizeof(s1));
for(i=0;i<k;i++) //将最后一个空格之前的数字找出
s1[g++]=s[i];
for(i=0;i<32;i++)
if(strcmp(s1,ta[i])==0){
if(i>=0&&i<=20) num+=n*i;
else if(i>20&&i<28) num+=(i-20+2)*10*n;
break;
}
}
else{ //若字符串中没有出现空格,则说明该数字可以直接输出。。。
for(i=0;i<32;i++)
if(strcmp(s,ta[i])==0){
if(i>=0&&i<=20) num=i;
else if(i>20&&i<28)
num=(i-20+2)*10;
}
}
printf("%lld\n",num);
}
// system("pause");
return 0;
}