博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[算法练习] 英语数字转换器
阅读量:6642 次
发布时间:2019-06-25

本文共 5307 字,大约阅读时间需要 17 分钟。

题目说明:

在这个问题中,将用英语给你一个或多个整数。你的任务是将这些数字转换成整型表示。数字范围从-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

 
Input

输入包括多个样例,注意:

1.负数前面有词negative

2.当能用thousand的时候,将不用hundred。例如1500将写为"one thousand five hundred",而不是"fifteen hundred".

输入将以一个空行结束

Output

输出将是每一个单独一行,每一个后面一个换行符

 

程序代码:

#include 
#include
#include
using namespace std;enum TOKEN_TYPE{ TYPE_NUMBER, TYPE_UNIT, TYPE_NEGATIVE};struct Token{ string StringValue; int Value; TOKEN_TYPE Type;};Token sTokenTable[] = { {
"zero", 0, TYPE_NUMBER}, {
"one", 1, TYPE_NUMBER}, {
"two", 2, TYPE_NUMBER}, {
"three", 3, TYPE_NUMBER}, {
"four", 4, TYPE_NUMBER}, {
"five", 5, TYPE_NUMBER}, {
"six", 6, TYPE_NUMBER}, {
"seven", 7, TYPE_NUMBER}, {
"eight", 8, TYPE_NUMBER}, {
"nine", 9, TYPE_NUMBER}, {
"ten", 10, TYPE_NUMBER}, {
"eleven", 11, TYPE_NUMBER}, {
"twelve", 12, TYPE_NUMBER}, {
"thirteen", 13, TYPE_NUMBER}, {
"fourteen", 14, TYPE_NUMBER}, {
"fifteen", 15, TYPE_NUMBER}, {
"sixteen", 16, TYPE_NUMBER}, {
"seventeen", 17, TYPE_NUMBER}, {
"eighteen", 18, TYPE_NUMBER}, {
"nineteen", 19, TYPE_NUMBER}, {
"twenty", 20, TYPE_NUMBER}, {
"thirty", 30, TYPE_NUMBER}, {
"forty", 40, TYPE_NUMBER}, {
"fifty", 50, TYPE_NUMBER}, {
"sixty", 60, TYPE_NUMBER}, {
"seventy", 70, TYPE_NUMBER}, {
"eighty", 80, TYPE_NUMBER}, {
"ninety", 90, TYPE_NUMBER}, {
"hundred", 100, TYPE_UNIT}, {
"thousand", 1000, TYPE_UNIT}, {
"million", 1000000, TYPE_UNIT}, {
"negative", -1, TYPE_NEGATIVE}};bool GetPreToken(const string& value, int& pos, Token& nextToken){ bool bRetValue = false; pos = value.find_last_not_of(" ", pos); if (pos == string::npos) { return false; } string tokenStr; string::size_type next = value.rfind(" ", pos); if (next != string::npos) { tokenStr = value.substr(next+1, pos-next); } else { tokenStr = value.substr(0, pos+1); } for (int i=0; i
= CurrentUnitValue) { CurrentUnitValue = NextToken.Value; } else { CurrentUnitValue *= NextToken.Value; } break; } if (nPos == string::npos) { break; } } lResult *= nNegatvie; return lResult;}int main2(){ char input[512]; cin.getline(input,512); string value(input); while (!value.empty()) { cout << Parse(value) << endl; cin.getline(input, 512); value = input; } return 0;}// http://dsalgo.openjudge.cn/201409week4/1/?lang=zh_CN// Test Case// zero // six// negative seven hundred twenty nine// one million one hundred one// eight hundred fourteen thousand twenty two// eight thousand fourteen hundred twenty twoTEST(BaiLian, tEnglishNumberTranslator){ //main2(); ASSERT_EQ(Parse("zero"), 0); ASSERT_EQ(Parse("six"), 6); ASSERT_EQ(Parse("negative seven hundred twenty nine"), -729); ASSERT_EQ(Parse("one million one hundred one"), 1000101); ASSERT_EQ(Parse("eight hundred fourteen thousand twenty two"), 814022); ASSERT_EQ(Parse("one"), 1); ASSERT_EQ(Parse("two"), 2); ASSERT_EQ(Parse("three"), 3); ASSERT_EQ(Parse("four"), 4); ASSERT_EQ(Parse("five"), 5); ASSERT_EQ(Parse("seven"), 7); ASSERT_EQ(Parse("eight"), 8); ASSERT_EQ(Parse("nine"), 9); ASSERT_EQ(Parse("ten"), 10); ASSERT_EQ(Parse("eleven"), 11); ASSERT_EQ(Parse("twelve"), 12); ASSERT_EQ(Parse("thirteen"), 13); ASSERT_EQ(Parse("fourteen"), 14); ASSERT_EQ(Parse("fifteen"), 15); ASSERT_EQ(Parse("sixteen"), 16); ASSERT_EQ(Parse("seventeen"), 17); ASSERT_EQ(Parse("eighteen"), 18); ASSERT_EQ(Parse("nineteen"), 19); ASSERT_EQ(Parse("twenty"), 20); ASSERT_EQ(Parse("thirty"), 30); ASSERT_EQ(Parse("forty one"), 41); ASSERT_EQ(Parse("fifty"), 50); ASSERT_EQ(Parse("sixty"), 60); ASSERT_EQ(Parse("seventy seven"), 77); ASSERT_EQ(Parse("eighty"), 80); ASSERT_EQ(Parse("ninety"), 90); ASSERT_EQ(Parse("one hundred three"), 103); ASSERT_EQ(Parse("negative one hundred twenty three million four hundred fifty six thousand seven hundred eighty"), -123456780); ASSERT_EQ(Parse("one hundred twenty three million four hundred fifty six thousand seven hundred eighty"), 123456780); ASSERT_EQ(Parse("nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine"), 999999999); ASSERT_EQ(Parse("negative nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine"), -999999999); }

转载于:https://www.cnblogs.com/Quincy/p/4730760.html

你可能感兴趣的文章
Spring Boot使用模板freemarker【从零开始学Spring Boot(转)
查看>>
【微信公众平台开发】微信刮刮乐,解决三星兼容性问题
查看>>
python模块之configparser
查看>>
【mysql】索引优化记录
查看>>
在Centos中yum安装和卸载软件的使用方法
查看>>
JavaScript ES6 module 模块
查看>>
如何规避适配风险?以《乱世王者》为例,探秘手游兼容性测试之路
查看>>
shell产生随机数七种方法
查看>>
Java 常用List集合使用场景分析
查看>>
创业维艰-->>书摘+乱七八糟
查看>>
IP数据报格式和IP地址路由
查看>>
hdu 4704 Sum (整数和分解+高速幂+费马小定理降幂)
查看>>
Python工作日类库Busines Holiday介绍
查看>>
Java基础-原码反码补码
查看>>
MySQL执行计划解读
查看>>
swift语言点评十六-Initialization && Deinitialization
查看>>
数据绑定流程分析(包括数据转换与格式化)
查看>>
mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决...
查看>>
第7件事 产品的5个要素
查看>>
在CentOS 7上安装Kafka
查看>>