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

大概题意:给出一串英文,翻译成阿拉伯数字。
思路:根据读数的特点,用millon和 thousand将数字分为三个三个一组,最多可分为三组。利用空格对该字符串进行分割分别读取.将特殊读法分别罗列出来进行配比。具体可见下面AC代码。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
using namespace std;
char a[10000];
char b[100];
long long int yf(char *s)
{
    if(strcmp(s,"zero")==0)
        return 0;
    if(strcmp(s,"one")==0)
       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;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(a);
        l=strlen(a);
        a[l]=' ';//便于最后一个数据的读入配比。
        a[l+1]='\0';
        l++;
        int p=0;
        int yy;
        long long int tem=0;//记录百位以及以下的数字
        long long int sum=0;//记录整个数字
        for(i=0;i<l;i++)
        {
            if(a[i]==' ')//遇到空格就对该小段的字符串进行配比,取得数字。
               {
                   yy=yf(b);
                   if(yy==100)
                   {
                       tem=tem*100;

                   }
                   else if(yy==1000)
                   {
                       tem=tem*1000;
                       sum=sum+tem;
                       tem=0;
                   }
                   else if(yy==1000000)
                   {
                    tem=tem*1000000;
                    sum=sum+tem;
                    tem=0;
                   }
                   else
                   {
                       tem=tem+yy;
                   }
                memset(b,'\0',sizeof(b));//因为要不断的对每个单词进行配比,所以b要先清空,再进行下一步新的字符的录入
                p=0;
               }
             else
                {
                    b[p]=a[i];
                    p++;
                }
        }
        sum=sum+tem;//要记得把最后一段数字加上
        printf("%lld\n",sum);
    }
    return 0;
}

题目传送门:Give Me the Number(点击可交题)

你可能感兴趣的:(less,AS,Numbers)