给定一个数的英文表示,输出其十进制的表示。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
char dc[30][20]= {"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"
};
int digit[28]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};
char word[200][20];
int main()
{
int cas;
char s[200];
scanf("%d", &cas);
getchar();
while(cas--)
{
memset(s, 0, sizeof(s));
memset(word, 0 ,sizeof(word));
gets(s);
int num_num = 0, k = 0;
int len = strlen(s);
for(int i = 0; i < len; i ++)
{
if(i >= 1 && s[i] == ' '&&s[i-1] !=' ')
{
num_num ++;
k = 0;
continue;
}
if(s[i] != ' ')
word[num_num][k++] = s[i];
}
int sum = 0,sum2 = 0;
int j;
for(int i = 0; i <= num_num; i ++)
{
for(j = 0; j < 28; j ++)
{
if(strcmp(word[i], dc[j]) == 0)
{
sum += digit[j];
break;
}
}
if(j == 28)
{
if(strcmp(word[i], "and") == 0)
continue;
if(strcmp(word[i], "thousand") == 0)
{
sum *= 1000;
sum2 += sum;
sum = 0;
}
else if(strcmp(word[i], "hundred") == 0)
{
sum *= 100;
}
else if(strcmp(word[i], "million") == 0)
{
sum *= 1000000;
sum2 += sum;
sum = 0;
}
}
}
printf("%d\n", sum2 + sum);
}
return 0;
}