1. 程式人生 > >Inglish-Number Translator (宇姐的英語小本)

Inglish-Number Translator (宇姐的英語小本)

Description

宇姐是個愛學習的好孩紙,尤其喜歡英歌歷史,但是對於數字的英文單詞,他總是傻傻分不清,於是他把一些常用或者典型的單詞都按照自己喜歡的方式整理到了自己的小本本上。有天,琿姐發現了這個祕密,想借他的本本學習一下這些單詞,卻發現看不懂這些單詞都表示數字幾,你能幫幫他嗎?

注意:所表示的數字-999,999,999≤N≤999,999,999。宇姐的本本上可能有:negative,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。

Input

輸入一行英文,即宇姐本本上的需要你翻譯的部分。 請注意: 1.負數前面有詞negative; 2.當能用thousand的時候,將不用hundred。例如:1500將寫為“one thousand five hundred”,而不是“fifteen hundred”。

Output

輸出單行結果,輸出對應的值。

Sample Input

negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

Sample Output

-7291000101

814022

題意是:將一串英文字母轉化成 羅馬數字

negative 代表的是 該數是負數

例如:

樣例一:-(7*100+29)=-729;

樣例二:1*1000000+1*100+1=1000101

              這一個樣例說明了 在 million 和 thousand 下邊為什麼要將 sum 清零

              如果不將 sum 清零 直接是 sum+= ..  的話,就變成了 (1*10000000+1)*100+1  結果就不一樣了

樣例三:(8*100+14)*1000+22=814022

一開始執行錯誤的原因是:

我用的是 gets 輸入一個字串,一直不出樣例,然後改成 cin>>s 就對了  就在這說一下 gets 和 cin 對輸入字串有什麼區別

cin不接受空格,TAB,換行符 等鍵的輸入,遇到這些鍵,字串會終止

而gets()則接受連續的輸入,包括空格,TAB

本題的樣例輸入是  先輸入一個單詞,然後看符合哪一個,計算出結果之後,再去輸入下一個單詞,直到遇見空字元,也就是換行

如果用 gets 輸入的話,輸入完 negative 之後,不知道你停止了,還在等著你輸入,所以就會不出結果

而用 cin 輸入的話,輸入完 negative和 空格 之後,cin 代表停止了,就會進入while 迴圈,執行下邊的語句

用陣列實現

程式碼如下:

1.第一種方法:

//用一個數組實現,找到對應的英文單詞,找出他的序號,用它的序號來間接表示他的羅馬數字

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int i,flag=0,sum=0,d=0;
    char s[101];
   //char a[][12]={"zero","0","one","1","two","2","three","3","four","4","five","5","six","6","seven","7","eight","8","nine","9","ten","10","eleven","11","twelve","12","thirteen","13","fourteen","14","fifteen","15","sixteen","16","seventeen","17","eighteen","18","nineteen","19","twenty","20","thirty","30","forty","40","fifty","50","sixty","60","seventy","70","eighty","80","ninety","90","hundred","thousand","million","negative"};
   chara[][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","hundred","thousand","million"};
     while(cin>>s){
        if(strcmp(s,"negative")==0)
            flag=1;
        if(strcmp(s,"million")==0){
            sum*=1000000;
            d+=sum;
            sum=0;
        }
        if(strcmp(s,"thousand")==0){
            sum*=1000;
            d+=sum;
            sum=0;
        }
        if(strcmp(s,"hundred")==0)
            sum*=100;
        else
        {
            for(i=0;i<28;i++){
                if(strcmp(s,a[i])==0){
                    if(i>=0&&i<=20){
                        sum+=i;
                        break;
                    }
                    if(i>=21&&i<=27){
                        sum+=(i-18)*10;
                        break;
                    }
                }

            }
        }
        if(getchar()=='\n'){
            if(flag==1)
               printf("-%d",d+sum);
            else
               printf("%d",d+sum);
            break;
        }
    }
}

2.第二種方法:

//用兩個陣列實現,一個存 英文單詞,一個存 羅馬數字,其中序號對應的就是相應的羅馬數字

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int i,flag=0,sum=0,d=0;
    char s[101];
    //char a[][12]={"zero","0","one","1","two","2","three","3","four","4","five","5","six","6","seven","7","eight","8","nine","9","ten","10","eleven","11","twelve","12","thirteen","13","fourteen","14","fifteen","15","sixteen","16","seventeen","17","eighteen","18","nineteen","19","twenty","20","thirty","30","forty","40","fifty","50","sixty","60","seventy","70","eighty","80","ninety","90","hundred","thousand","million","negative"};
    char a[][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","hundred","thousand","million"};
  int c[]={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};
    while(cin>>s)
    {
        if(strcmp(s,"negative")==0)
            flag=1;
        if(strcmp(s,"million")==0){
            sum*=1000000;
            d+=sum;
            sum=0;
        }
        if(strcmp(s,"thousand")==0){
            sum*=1000;
            d+=sum;
            sum=0;
        }
        if(strcmp(s,"hundred")==0)
            sum*=100;
        for(i=0;i<=27;i++)
            if(strcmp(s,a[i])==0){
                sum+=c[i];
                break;
            }

        if(getchar()=='\n')
        {
            if(flag==1)
                printf("-%d",d+sum);
            else
                printf("%d",d+sum);

            break;
        }

    }
}

也可以多組輸入