字符串操作

题目:给定一串英文字符串,输出其对应的数字


代码:

#include <iostream>
#include <cstring>
#include <map>
#include <strstream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    //freopen("in.txt","r",stdin);
    char line[256];
    int ans;
    map<string,int>entod;

    entod[string("zero")] = 0;
    entod[string("one")] = 1;
    entod[string("two")] = 2;
    entod[string("three")] = 3;
    entod[string("four")] = 4;
    entod[string("five")] = 5;
    entod[string("six")] = 6;
    entod[string("seven")] = 7;
    entod[string("eight")] = 8;
    entod[string("nine")] = 9;
    entod[string("ten")] = 10;
    entod[string("eleven")] = 11;
    entod[string("twelve")] = 12;
    entod[string("thirteen")] = 13;
    entod[string("fourteen")] = 14;
    entod[string("fifteen")] = 15;
    entod[string("sixteen")] = 16;
    entod[string("seventeen")] = 17;
    entod[string("eighteen")] = 18;
    entod[string("nineteen")] = 19;
    entod[string("twenty")] = 20;
    entod[string("thirty")] = 30;
    entod[string("forty")] = 40;
    entod[string("fifty")] = 50;
    entod[string("sixty")] = 60;
    entod[string("seventy")] = 70;
    entod[string("eighty")] = 80;
    entod[string("ninety")] = 90;

    while(cin.getline(line,256))
    {
        int len = strlen(line);
        if(len == 0)
            continue;
        istrstream istr(line,len);
        string en;
        int add = 0;
        ans = 0;

        while(istr >> en)
        {
            if(en == "negative")
                cout << "-";
            if(en == "hundred")
                add *= 100;
            if(en == "thousand")
            {
                ans += add * 1000;
                add = 0;
            }
            if(en == "million")
            {
                ans += add;
                ans *= 1000000;
                add = 0;
            }
            add += entod[en];
        }
        ans += add;
        cout << ans << endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值