<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include<string>
#include<map>
#include<algorithm>
usingnamespacestd;
map<string,int>mapTable;
voidinitMapTable()
{
mapTable["zero"]=0;
mapTable["one"]=1;
mapTable["two"]=2;
mapTable["three"]=3;
mapTable["four"]=4;
mapTable["five"]=5;
mapTable["six"]=6;
mapTable["seven"]=7;
mapTable["eight"]=8;
mapTable["nine"]=9;
mapTable["ten"]=10;
mapTable["eleven"]=11;
mapTable["twelve"]=12;
mapTable["thirteen"]=13;
mapTable["fourteen"]=14;
mapTable["fifteen"]=15;
mapTable["sixteen"]=16;
mapTable["seventeen"]=17;
mapTable["eighteen"]=18;
mapTable["nineteen"]=19;
mapTable["twenty"]=20;
mapTable["thirty"]=30;
mapTable["forty"]=40;
mapTable["fifty"]=50;
mapTable["sixty"]=60;
mapTable["seventy"]=70;
mapTable["eighty"]=80;
mapTable["hundred"]=100;
mapTable["thousand"]=1000;
mapTable["million"]=1000000;
}
voidsolve(string&str)
{
string::size_typepos=str.find_first_of('');
if(pos==string::npos)
{//只有一个单词
if(str=="negative")
{
cout<<"-0"<<endl;
}
elseif(mapTable.find(str)!=mapTable.end())
{
cout<<mapTable[str]<<endl;
}
}
else
{//至少个单词
stringinput=str;
intsum=0,msum=0,tsum=0,hsum=0;
boolbMinus=false;
intcount=0;
string::iteratorstart=input.begin();
string::iteratorend=input.end();
do
{
stringword;
end=find(start,input.end(),'');//找到分隔符
copy(start,end,back_inserter(word));
++count;//单词计数
if(word=="negative")
{
bMinus=true;
}
else
{
if(count==2&&word=="zero")
{
bMinus=false;
}
intcurNum=mapTable[word];
if(curNum==100)
{
hsum=sum*100;
sum=0;
}
elseif(curNum==1000)
{
tsum=(hsum+sum)*1000;
hsum=0;
sum=0;
}
elseif(curNum==1000000)
{
msum=(tsum+hsum+sum)*1000000;
tsum=0;
hsum=0;
sum=0;
}
else
{
sum+=curNum;
}
}
if(end==input.end())
{
if(bMinus)
cout<<"-";
cout<<msum+tsum+hsum+sum<<endl;
break;
}
start=++end;
}while(end!=input.end());
}
}
intmain()
{
stringstr;
initMapTable();
while(getline(cin,str))
{
if(str.length()==0||str.length()==1)
continue;
solve(str);
}
return0;
}
#include<string>
#include<map>
#include<algorithm>
usingnamespacestd;
map<string,int>mapTable;
voidinitMapTable()
{
mapTable["zero"]=0;
mapTable["one"]=1;
mapTable["two"]=2;
mapTable["three"]=3;
mapTable["four"]=4;
mapTable["five"]=5;
mapTable["six"]=6;
mapTable["seven"]=7;
mapTable["eight"]=8;
mapTable["nine"]=9;
mapTable["ten"]=10;
mapTable["eleven"]=11;
mapTable["twelve"]=12;
mapTable["thirteen"]=13;
mapTable["fourteen"]=14;
mapTable["fifteen"]=15;
mapTable["sixteen"]=16;
mapTable["seventeen"]=17;
mapTable["eighteen"]=18;
mapTable["nineteen"]=19;
mapTable["twenty"]=20;
mapTable["thirty"]=30;
mapTable["forty"]=40;
mapTable["fifty"]=50;
mapTable["sixty"]=60;
mapTable["seventy"]=70;
mapTable["eighty"]=80;
mapTable["hundred"]=100;
mapTable["thousand"]=1000;
mapTable["million"]=1000000;
}
voidsolve(string&str)
{
string::size_typepos=str.find_first_of('');
if(pos==string::npos)
{//只有一个单词
if(str=="negative")
{
cout<<"-0"<<endl;
}
elseif(mapTable.find(str)!=mapTable.end())
{
cout<<mapTable[str]<<endl;
}
}
else
{//至少个单词
stringinput=str;
intsum=0,msum=0,tsum=0,hsum=0;
boolbMinus=false;
intcount=0;
string::iteratorstart=input.begin();
string::iteratorend=input.end();
do
{
stringword;
end=find(start,input.end(),'');//找到分隔符
copy(start,end,back_inserter(word));
++count;//单词计数
if(word=="negative")
{
bMinus=true;
}
else
{
if(count==2&&word=="zero")
{
bMinus=false;
}
intcurNum=mapTable[word];
if(curNum==100)
{
hsum=sum*100;
sum=0;
}
elseif(curNum==1000)
{
tsum=(hsum+sum)*1000;
hsum=0;
sum=0;
}
elseif(curNum==1000000)
{
msum=(tsum+hsum+sum)*1000000;
tsum=0;
hsum=0;
sum=0;
}
else
{
sum+=curNum;
}
}
if(end==input.end())
{
if(bMinus)
cout<<"-";
cout<<msum+tsum+hsum+sum<<endl;
break;
}
start=++end;
}while(end!=input.end());
}
}
intmain()
{
stringstr;
initMapTable();
while(getline(cin,str))
{
if(str.length()==0||str.length()==1)
continue;
solve(str);
}
return0;
}