Uva sollution:486 - English-Number Translator

// Uva id: deepitauiu // problem link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=427 #include<bits/stdc++.h> using namespace std; int main(){ map<string,int> m; m["zero"]=0; m["one"]=1; m["two"]=2; m["three"]=3; m["four"]=4; m["five"]=5; m["six"]=6; m["seven"]=7; m["eight"]=8; m["nine"]=9; m["ten"]=10; m["eleven"]=11; m["twelve"]=12; m["thirteen"]=13; m["fourteen"]=14; m["fifteen"]=15; m["sixteen"]=16; m["seventeen"]=17; m["eighteen"]=18; m["nineteen"]=19; m["twenty"]=20; m["thirty"]=30; m["forty"]=40; m["fifty"]=50; m["sixty"]=60; m["seventy"]=70; m["eighty"]=80; m["ninety"]=90; m["hundred"]=100; m["thousand"]=1000; m["million"]=1000000; string s,word; while (getline (cin, s)){ bool negative = false; int num = 0, total = 0; stringstream ss(s); string word; while (ss >> word) { if (word == "negative") negative = true; else if (word == "million") { total += num * 1000000; num = 0; } else if (word == "thousand") { total += num * 1000; num = 0; } else if (word == "hundred") { num = num * 100; } else { num += m[word]; } } total += num; if (negative) total = -total; cout << total << '\n'; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.