UVA 486 English-Number Translator

题目:把数字的英文转化成数字(int)

问题:字符串的处理 。模拟。

分析:将每个输入看成下面三部分:x…xmillionx…xthousandx…x
即例:1,000,000每个逗号当作一个结点。处理好 加 与 乘,局部与整体的关系。

注意:用好stringstream 可以很方便。

代码一:c++ 用了stringstream

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int integer[]={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};
string english_number[]={"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 integer_word(string word);
int main()
{
    string line,word;stringstream ss;
    while(getline(cin,line))
    {
        ss<<line;
        ss>>word;int sum=0,total=0;
        if(word=="negative"){cout<<'-';ss>>word;}
        do
        {
           if(word=="million")
           {
               sum*=1000000;total+=sum;sum=0;
           }
           else if(word=="thousand")
           {
               sum*=1000;total+=sum;sum=0;
           }
           else if(word=="hundred")
           {
                sum*=100;
           }
           else
           {
               sum+=integer[integer_word(word)];
           }
        }while(ss>>word);
         total+=sum;
        cout<<total<<endl;
        ss.clear();
        ss.str("");
    }
}

int integer_word(string word)
{
    for(int i=0;;++i)
    {
        if(word==english_number[i])
            return i;
    }
}

代码二:面向对象思想(老师的代码之一)strtok的使用

#include<iostream>
#include<string>
using namespace std;
class uva486{
public:
    bool input();
    friend ostream& operator<<(ostream& out, uva486& ent);
private:
    int total;
    bool sign;
    static string english[];
    static int number[];
    int search(string word);
};
string uva486::english[]={"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 uva486::number[]={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};
bool uva486::input(){
    char line[110]={0};//数组长度为101时,提交为wrong answer;放大后ac。
    if( cin.getline(line, 110)){
        char *first;
        if(line[0]=='n' && line[1]=='e'){//负数
            sign=true;
            first=strtok(&line[9], " \n");
        }
        else{
            sign=false;
            first=strtok(line, " \n");
        }
        total=0;//存放总和
        int sum=0;/存放3部分的分和,每部分累加入total后,清零。
        while( first ){
            string word=first;
            if( word=="hundred" ){
                sum*=100;
            }
            else if( word=="thousand" ){
                sum*=1000;
                total+=sum;
                sum=0;
            }
            else if( word=="million" ){
                sum*=1000000;
                total+=sum;
                sum=0;
            }
            else 
                sum+=number[search(word)];
            first=strtok(NULL, " \n");
        }
        if( sum ) total+=sum;
        return true;
    }
    return false;
}
int uva486::search(string word){
    for(int i=0; ; i++)
        if( word==english[i] )
            return i;
}
ostream& operator<<(ostream& out, uva486& ent){
    if(ent.sign)
        out<<'-';
    out<<ent.total;
}
int main()
{
    uva486 ent;
    while( ent.input() ){
        cout<<ent<<endl;
    }
    return 0;       
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值