Inglish-Number Translator
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5610 | Accepted: 2213 |
Description
In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:
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
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
The input consists of several instances. Notes on input:
The input is terminated by an empty line.
- Negative numbers will be preceded by the word negative.
- The word "hundred" is not used when "thousand" could be. For example, 1500 is written "one thousand five hundred", not "fifteen hundred".
The input is terminated by an empty line.
Output
The answers are expected to be on separate lines with a newline after each.
Sample Input
six negative seven hundred twenty nine one million one hundred one eight hundred fourteen thousand twenty two
Sample Output
6 -729 1000101 814022
Source
#include<iostream>
#include<algorithm>
#include<fstream>
#include<map>
#include<set>
#include<string>
#include<stack>
using namespace std;
struct coperation{
virtual int number(int num,int b){
return num;
}
virtual int base(int b){
return b;
}
};
struct caddop:public coperation{
int _toadd;
caddop(int toadd):_toadd(toadd){}
virtual int number(int num,int b){
return num+_toadd*b;
}
};
struct cnegop:public coperation{
cnegop(){}
virtual int number(int num,int b){
return -num;
}
};
struct cbasemop:public coperation{
int _basem;
cbasemop(int basem):_basem(basem){}
virtual int base(int b){
return b*_basem;
}
};
struct cbaseop:public coperation{
int _base;
cbaseop(int base):_base(base){}
virtual int base(int b){
return _base;
}
};
struct coperations{
map<string,coperation*> _o;
coperations()
{
_o["negative"] = new cnegop();
_o["zero"] = new caddop(0);
_o["one"] = new caddop(1);
_o["two"] = new caddop(2);
_o["three"] = new caddop(3);
_o["four"] = new caddop(4);
_o["five"] = new caddop(5);
_o["six"] = new caddop(6);
_o["seven"] = new caddop(7);
_o["eight"] = new caddop(8);
_o["nine"] = new caddop(9);
_o["ten"] = new caddop(10);
_o["eleven"] = new caddop(11);
_o["twelve"] = new caddop(12);
_o["thirteen"] = new caddop(13);
_o["fourteen"] = new caddop(14);
_o["fifteen"] = new caddop(15);
_o["sixteen"] = new caddop(16);
_o["seventeen"]= new caddop(17);
_o["eighteen"] = new caddop(18);
_o["nineteen"] = new caddop(19);
_o["twenty"] = new caddop(20);
_o["thirty"] = new caddop(30);
_o["forty"] = new caddop(40);
_o["fifty"] = new caddop(50);
_o["sixty"] = new caddop(60);
_o["seventy"] = new caddop(70);
_o["eighty"] = new caddop(80);
_o["ninety"] = new caddop(90);
_o["hundred"] = new cbasemop(100);
_o["thousand"] = new cbaseop(1000);
_o["million"] = new cbaseop( 1000000);
}
~coperations(){
for(map<string,coperation*>::iterator it=_o.begin();it!=_o.end();it++)
delete it->second;
}
}_ops;
int translate(stack<string>& sstack){
int num=0;
int b=1;
while(!sstack.empty()){
coperation* op=_ops._o[sstack.top()];
sstack.pop();
num=op->number(num,b);
b=op->base(b);
}
return num;
}
int main(){
stack<string> sstack;
while(1){
string s;
char c;
cin>>c;
while(c>='a'&&c<='z'){
s+=c;
c=cin.get();
}
if(!s.empty())
sstack.push(s);
if(c=='\n'){
if(sstack.empty())
break;
cout<<translate(sstack)<<endl;
}
}
return 0;
}