BNU16490:Give Me the Number(模拟)

Numbers in English are written down in the following way (only numbers less than 109 are considered). Number abc,def,ghi is written as "[abc] million [def] thousand [ghi]". Here "[xyz] " means the written down number xyz .

In the written down number the part "[abc] million" is omitted if abc = 0 , "[def] thousand" is omitted if def = 0 , and "[ghi] " is omitted if ghi = 0 . If the whole number is equal to 0 it is written down as "zero". Note that words "million" and "thousand" are singular even if the number of millions or thousands respectively is greater than one.

Numbers under one thousand are written down in the following way. The number xyz is written as "[x] hundred and [yz] ”. ( If yz = 0 it should be only “[x] hundred”. Otherwise if y = 0 it should be only “[x] hundred and [z]”.) Here "[x] hundred and" is omitted if x = 0 . Note that "hundred" is also always singular.

Numbers under 20 are written down as "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", and "nineteen" respectively. Numbers from 20 to 99 are written down in the following way. Number xy is written as "[x0] [y] ", and numbers divisible by ten are written as "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", and "ninety" respectively.

For example, number 987,654,312 is written down as "nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve", number 100,000,037 as "one hundred million thirty seven", number 1,000 as "one thousand". Note that "one" is never omitted for millions, thousands and hundreds.

Give you the written down words of a number, please give out the original number.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1900) which is the number of test cases. It will be followed by T consecutive test cases.

Each test case contains only one line consisting of a sequence of English words representing a number.

Output

For each line of the English words output the corresponding integer in a single line. You can assume that the integer is smaller than 109.

Sample Input

3
one
eleven
one hundred and two

Sample Output

1
11
102
 
题意:将一个英文数字转化为阿拉伯数字
由于eighteen多写了个t,WA一次,蛋疼
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[100000];
char num[100];
long long check(char *s)
{
    if(!strcmp(s,"zero"))
        return 0;
    if(!strcmp(s,"one"))
        return 1;
    if(!strcmp(s,"two"))
        return 2;
    if(!strcmp(s,"three"))
        return 3;
    if(!strcmp(s,"four"))
        return 4;
    if(!strcmp(s,"five"))
        return 5;
    if(!strcmp(s,"six"))
        return 6;
    if(!strcmp(s,"seven"))
        return 7;
    if(!strcmp(s,"eight"))
        return 8;
    if(!strcmp(s,"nine"))
        return 9;
    if(!strcmp(s,"ten"))
        return 10;
    if(!strcmp(s,"eleven"))
        return 11;
    if(!strcmp(s,"twelve"))
        return 12;
    if(!strcmp(s,"thirteen"))
        return 13;
    if(!strcmp(s,"fourteen"))
        return 14;
    if(!strcmp(s,"fifteen"))
        return 15;
    if(!strcmp(s,"sixteen"))
        return 16;
    if(!strcmp(s,"seventeen"))
        return 17;
    if(!strcmp(s,"eighteen"))
        return 18;
    if(!strcmp(s,"nineteen"))
        return 19;
    if(!strcmp(s,"twenty"))
        return 20;
    if(!strcmp(s,"thirty"))
        return 30;
    if(!strcmp(s,"forty"))
        return 40;
    if(!strcmp(s,"fifty"))
        return 50;
    if(!strcmp(s,"sixty"))
        return 60;
    if(!strcmp(s,"seventy"))
        return 70;
    if(!strcmp(s,"eighty"))
        return 80;
    if(!strcmp(s,"ninety"))
        return 90;
    if(!strcmp(s,"hundred"))
        return 100;
    if(!strcmp(s,"thousand"))
        return 1000;
    if(!strcmp(s,"million"))
        return 1000000;
    return 0;
}

int main()
{
    int n,i,len,l,flag,ff;
    long long sum,tem;
    scanf("%d%*c",&n);
    while(n--)
    {
        gets(str);
        len = strlen(str);
        l = 0;
        sum = 0;
        tem = 0;
        str[len] = ' ';
        str[len+1] = '\0';
        len++;
        for(i = 0; i<len; i++)
        {
            if(str[i] == ' ')
            {
                ff = check(num);
                if(ff == 100)
                    tem*=ff;
                else if(ff == 1000)
                {
                    tem*=ff;
                    sum+=tem;
                    tem = 0;
                }
                else if(ff == 1000000)
                {
                    tem*=ff;
                    sum+=tem;
                    tem = 0;
                }
                else
                    tem+=ff;
                l = 0;
                memset(num,'\0',sizeof(num));
            }
            else
            {
                num[l++] = str[i];
            }
        }
        sum+=tem;
        printf("%lld\n",sum);
    }

    return 0;
}

你可能感兴趣的:(模拟,水,BNU)