#include <iostream>
#include<string>
#include<queue>
#include <map>
#include<fstream>
using namespace std;
#define DEBUG
int parse(void)
{
#ifdef DEBUG
fstream cin("G:\\book\\algorithms\\acm\\Debug\\dat.txt");
#endif
queue<string> que;
map<string, string> table ;
table["One"] = "1"; table["Two"] = "2"; table["Three"] = "3"; table["Four"] = "4";
table["Five"] = "5"; table["Six"] = "6"; table["Seven"] = "7"; table["Eight"] = "8";
table["Nine"] = "9"; table["Zero"] = "0";
int n = 0;
cin >> n;
while (n-- > 0)
{
string tmp, input;
cin >> input;
int j = 0;
int twice = 0;
char buf[32];
int len = input.length();
for (int i = 0; i < len; i++)
{
char c = input[i];
if (isupper(c))
{
buf[j] = '\0'; /* get a token */
tmp = buf;
/* check special token */
if ("Double" == tmp)
{
twice = 1;
}
else if (1 == twice) /* check flag, do sth, clear flag */
{
que.push(tmp);
que.push(tmp);
twice = 0;
}
else
{
que.push(tmp);
}
j = 0;
}
buf[j++] = c;
}
buf[j] = '\0'; tmp = buf;
if (twice == 1)
{ que.push(tmp); que.push(tmp); twice = 0; }
else
que.push(tmp);
que.pop();
while (!que.empty())
{
//cout << que.front() << "==>" << table[que.front()] << "\n";
cout << table[que.front()] ;
que.pop();
}
cout << "\n";
}
return 0;
}
int main()
{
return parse();
}
程序有待完善: 在代码对特殊字符的处理中 采用 if else 的写法不是很好,现在只处理了一种特殊token (Double)。
如果要处理多种特殊token,而且处理方法又不相同,代码书写会变得难懂了