字符串操作

来源:互联网 发布:linux samba rpm 下载 编辑:程序博客网 时间:2024/06/05 01:15

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


代码:

#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;}




原创粉丝点击