这是我在coursrea上做练习的时候碰到的一道题。原题如下:
1. 英语数字转换器
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
在这个问题中,将用英语给你一个或多个整数。你的任务是将这些数字转换成整型表示。数字范围从-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
输入
输入包括多个样例,注意:
1.负数前面有词negative
2.当能用thousand的时候,将不用hundred。例如1500将写为”one thousand five hundred”,而不是”fifteen hundred”.
输入将以一个空行结束
输出
输出将是每一个单独一行,每一个后面一个换行符
样例输入
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two
样例输出
6
-729
1000101
814022解题思路:
首先,涉及到字符和字符所对应的数字。对于不同的数字,如hundred,thousand和million表示的是基数单位类型不同。由此建立了结构体,包含字符、字符所对应的数字和字符的基数大小。这样存放单词表的数据结构就确定了。
然后,输入的是一个字符串,根据“ ”不同位置,就能把每个单词划分出来,用string自带的find函数就可以解决这个问题。根据单词,在单词表中就有对应的数字和基数类型。将找出来的数字和基数存分别存放在一维数组中。
最后,根据基数类型不同,将数组中的数字转换成long类型的数字。
ps:代码中的命名太随意,求勿喷。
#include<iostream>
#include<string>
using namespace std;
struct strCha{
string s;
int t;
int i;
strCha(string s1, int t1, int i1) :s(s1), t(t1), i(i1){}
};
strCha stand[32] = {
{ "negative", -1, 1 },
{ "zero", 0, 0 },
{ "one", 1, 0 },
{ "two", 2, 0 },
{ "three", 3, 0 },
{ "four", 4, 0 },
{ "five", 5, 0 },
{ "six", 6, 0 },
{ "seven", 7, 0 },
{ "eight", 8, 0 },
{ "nine", 9, 0 },
{ "ten", 10, 0 },
{ "eleven", 11, 0 },
{ "twelve", 12, 0 },
{ "thirteen", 13, 0 },
{ "fourteen", 14, 0 },
{ "fifteen", 15, 0 },
{ "sixteen", 16, 0 },
{ "seventeen", 17, 0 },
{ "eighteen", 18, 0 },
{ "nineteen", 19, 0 },
{ "twenty", 20, 0 },
{ "thirty", 30, 0 },
{ "forty", 40, 0 },
{ "fifty", 50, 0 },
{ "sixty", 60, 0 },
{ "seventy", 70, 0 },
{ "eighty", 80, 0 },
{ "ninety", 90, 0 },
{ "hundred", 100, 2 },
{ "thousand", 1000, 3 },
{ "million", 1000000, 4 },
};
long ExchangeS(string& InputS)
{
long resul=0;
string valueS;
int pos = 0, next = -1;
int j = 0;
int flag = 1;
int num = InputS.size();
int Shu[32];
int np[32];
while (pos != string::npos)
{
pos = InputS.find(" ", pos+1);
if (pos != string::npos)
{
valueS = InputS.substr(next+1,pos-next-1);
}
else
{
valueS = InputS.substr(next+1);
}
//在stand中寻找相匹配的字符串
for (int i = 0; i < 32;i++)
{
if (valueS==stand[i].s)
{
Shu[j] = stand[i].t;
np[j] = stand[i].i;
j++;
break;
}
}
next = pos;
}
//转换成数字
int i = 0;
long nu = 0;
long res = 0;
while ( i < j)
{
switch (np[i])
{
case 1:flag = -1; i++;
case 0:resul += Shu[i]; i++;
if (i >= j){ nu = nu + resul; resul = 0; break; }
else continue;//nu = nu + resul; resul = 0;
case 2:if (nu<100){ nu += resul; nu *= Shu[i]; resul = 0; i++; break; }
else{ resul *= Shu[i]; i++; nu = nu + resul; resul = 0; break; }
case 3:if (nu < 1000){ nu += resul; nu *= Shu[i]; resul = 0; i++; break; }
else{ resul *= Shu[i]; i++; nu = nu + resul; resul = 0; i++; break; }
case 4:if (resul != 0)nu = resul+nu;
nu *= Shu[i]; i++; resul = 0; res = nu; nu = 0; break;
default:nu = nu + Shu[i]; resul = 0; break;
}
}
if (res==0)
return nu*flag;
else return(nu + res)*flag;
}
int main()
{
char s[512];
cin.getline(s, 512);
string InputS(s);
while (!InputS.empty())
{
cout << ExchangeS(InputS) << endl;
cin.getline(s,512);
InputS = s;
}
return 0;
}