// poj 2121-Inglish-Number Translator /* * 字符串处理 * 题意:读入一个用英文单词表示的数,将其用数字显示出来 * 形如:…hundred…million…hundred…thousand…hundred… * 其中的百万和千要做特殊处理,另设变量表示百万数和千数 * cin.getline() 串不接收回车 * fgets() 串接收回车 * !!!以空行结束,则串s为空时结束 * !读取每个单词时用串流sstream * !用map容器建立单词和数字之间的映射也可预处理两个数组表明其对应关系 * wa ac 180k 16ms */ #include <iostream> #include <map> #include <string> #include <string.h> #include <sstream> using namespace std; int main() { string s,str; map<string,int> pos; pos[string("zero")]=0; pos[string("one")]=1; pos[string("two")]=2; pos[string("three")]=3; pos[string("four")]=4; pos[string("five")]=5; pos[string("six")]=6; pos[string("seven")]=7; pos[string("eight")]=8; pos[string("nine")]=9; pos[string("ten")]=10; pos[string("eleven")]=11; pos[string("twelve")]=12; pos[string("thirteen")]=13; pos[string("fourteen")]=14; pos[string("fifteen")]=15; pos[string("sixteen")]=16; pos[string("seventeen")]=17; pos[string("eighteen")]=18; pos[string("nineteen")]=19; pos[string("twenty")]=20; pos[string("thirty")]=30; pos[string("forty")]=40; pos[string("fifty")]=50; pos[string("sixty")]=60; pos[string("seventy")]=70; pos[string("eighty")]=80; pos[string("ninety")]=90; while(getline(cin,s)){ if(s.empty()) break; stringstream ss(s); int add=0,ans=0; while(ss>>str){ if(str=="negative") cout<<'-'; if(str=="hundred"){ ans*=100; } if(str=="thousand"){ add+=ans*1000; ans=0; } if(str=="million"){ add+=ans*1000000; ans=0; } else{ ans+=pos[str]; } } cout<<add+ans<<endl; } return 0; }