[算法练习] 英语数字转换器 您所在的位置:网站首页 fiftythousand翻译数字 [算法练习] 英语数字转换器

[算法练习] 英语数字转换器

2024-07-17 17:56| 来源: 网络整理| 查看: 265

题目说明:

在这个问题中,将用英语给你一个或多个整数。你的任务是将这些数字转换成整型表示。数字范围从-999,999,999到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

输出将是每一个单独一行,每一个后面一个换行符

 

程序代码: #include #include #include using namespace std; enum TOKEN_TYPE { TYPE_NUMBER, TYPE_UNIT, TYPE_NEGATIVE }; struct Token { string StringValue; int Value; TOKEN_TYPE Type; }; Token sTokenTable[] = { {"zero", 0, TYPE_NUMBER}, {"one", 1, TYPE_NUMBER}, {"two", 2, TYPE_NUMBER}, {"three", 3, TYPE_NUMBER}, {"four", 4, TYPE_NUMBER}, {"five", 5, TYPE_NUMBER}, {"six", 6, TYPE_NUMBER}, {"seven", 7, TYPE_NUMBER}, {"eight", 8, TYPE_NUMBER}, {"nine", 9, TYPE_NUMBER}, {"ten", 10, TYPE_NUMBER}, {"eleven", 11, TYPE_NUMBER}, {"twelve", 12, TYPE_NUMBER}, {"thirteen", 13, TYPE_NUMBER}, {"fourteen", 14, TYPE_NUMBER}, {"fifteen", 15, TYPE_NUMBER}, {"sixteen", 16, TYPE_NUMBER}, {"seventeen", 17, TYPE_NUMBER}, {"eighteen", 18, TYPE_NUMBER}, {"nineteen", 19, TYPE_NUMBER}, {"twenty", 20, TYPE_NUMBER}, {"thirty", 30, TYPE_NUMBER}, {"forty", 40, TYPE_NUMBER}, {"fifty", 50, TYPE_NUMBER}, {"sixty", 60, TYPE_NUMBER}, {"seventy", 70, TYPE_NUMBER}, {"eighty", 80, TYPE_NUMBER}, {"ninety", 90, TYPE_NUMBER}, {"hundred", 100, TYPE_UNIT}, {"thousand", 1000, TYPE_UNIT}, {"million", 1000000, TYPE_UNIT}, {"negative", -1, TYPE_NEGATIVE} }; bool GetPreToken(const string& value, int& pos, Token& nextToken) { bool bRetValue = false; pos = value.find_last_not_of(" ", pos); if (pos == string::npos) { return false; } string tokenStr; string::size_type next = value.rfind(" ", pos); if (next != string::npos) { tokenStr = value.substr(next+1, pos-next); } else { tokenStr = value.substr(0, pos+1); } for (int i=0; i= CurrentUnitValue) { CurrentUnitValue = NextToken.Value; } else { CurrentUnitValue *= NextToken.Value; } break; } if (nPos == string::npos) { break; } } lResult *= nNegatvie; return lResult; } int main2() { char input[512]; cin.getline(input,512); string value(input); while (!value.empty()) { cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有