C++字数到整数
C++ Word-Number to int
我正在开发一个程序,它使用单词而不是数字进行基本计算。 例如,五 + 二将输出七。
程序变得更加复杂,需要输入two_hundred_one + five_thousand_six(201 + 5006)
通过运算符重载方法,我拆分每个数字并将其分配给它自己的数组索引。
两个是 [0],一百是 [1],一个是 [2]。然后阵列回收 5006。
我的问题是,要执行实际计算,我需要将存储在数组中的单词转换为实际整数。
我有常量字符串数组,例如作为单词库:
const string units[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
const string teens[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string tens[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
如果我的"token"数组在索引 0、1 和 2 中存储了 2001,我不确定将它们转换为整数的最佳方法是什么。
对我来说,部分技巧是向后解析令牌,而不是向前解析。并在您向后移动令牌时保持比例因子。对于每个令牌,您可以添加到当前值(按比例因子的当前值缩放)或调整比例因子,具体取决于您遇到的令牌。
这是我的实现(不包括某些位,您需要添加一些逻辑来处理数百万个)。
#include <vector>
#include <string>
#include <assert.h>
int tokens_to_int( const std::vector<std::string> &s )
{
int scale = 1;
int rv=0;
for(
std::vector<std::string>::const_reverse_iterator it = s.rbegin();
it!=s.rend();
++it
)
{
std::string cw = *it;
//Things that add to the current value
if( cw == "one" ) { rv += 1 * scale; }
if( cw == "two" ) { rv += 2 * scale; }
if( cw == "three" ) { rv += 3 * scale; }
if( cw == "four" ) { rv += 4 * scale; }
// ...
if( cw == "nine" ) { rv += 9 * scale; }
if( cw == "ten" ) { rv += 10 * scale; }
// Teens
if( cw == "eleven" ) { rv += 11 * scale; }
if( cw == "twelve" ) { rv += 12 * scale; }
// ...
if( cw == "nineteen" ) { rv += 19 * scale; }
// Multiples of 10
if( cw == "twenty" ) { rv += 20 * scale; }
if( cw == "thirty" ) { rv += 30 * scale; }
if( cw == "fourty" ) { rv += 40 * scale; }
// ...
if( cw == "ninety" ) { rv += 90 * scale; }
//Things that effect scale for following entries
if( cw == "hundred" ) { scale *= 100; }
if( cw == "thousand" ) { if( scale==100) { scale=1000; } else { scale*=1000; } }
}
return rv;
}
template<typename T>
struct as_vec
{
as_vec<T>& operator()(const T & t )
{
v.push_back(t);
return *this;
}
std::vector<T> build() { return v; }
std::vector<T> v;
};
int main()
{
assert(421 == tokens_to_int( as_vec<std::string>()("four")("hundred")("twenty")("one").build() ) );
assert(422 == tokens_to_int( as_vec<std::string>()("four")("hundred")("twenty")("two").build() ) );
assert(11000 == tokens_to_int( as_vec<std::string>()("eleven")("thousand").build() ) );
assert(21201 == tokens_to_int( as_vec<std::string>()("twenty")("one")("thousand")("two")("hundred")("one").build() ) );
assert(100001 == tokens_to_int( as_vec<std::string>()("one")("hundred")("thousand")("one").build() ) );
assert(101000 == tokens_to_int( as_vec<std::string>()("one")("hundred")("one")("thousand").build() ) );
assert(411201 == tokens_to_int( as_vec<std::string>()("four")("hundred")("eleven")("thousand")("two")("hundred")("one").build() ) );
assert(999999 == tokens_to_int( as_vec<std::string>()("nine")("hundred")("ninety")("nine")("thousand")("nine")("hundred")("ninety")("nine").build() ) );
}
我同意 Jesse 关于使用映射将值映射到数组的观点,从那时起,您将能够执行以下操作:
mymap["two"] * mymap["hundred"] + mymap["one"]
当然,您必须小心诸如"五百二千五十七"之类的情况,在这种情况下,您必须检查青少年以外的特殊情况:
const string temp[] = {"five","hundred","two","thousand","fifty","seven"};
int handleThree(int start) {
if( mymap[temp[start+ 3]] > 100) //check for special case, in which case you handle 4 words instead of three
return (myap[temp[start]] * mymap[temp[start+1]] + mymap[temp[start+2]]) * mymap[temp[start+3]];
else
return mymap[temp[start]] * mymap[temp[start+1]] + handleTwo(start+2)
}
int handleTwo(int start) {
if(mymap[temp[start]] / 10 == 1) //check if it's a teen, return teen value
return mymap[temp[start]]
if(mymap[temp[start+1]] > 90) //check for another special case, "five thousand one hundred and fifty seven", for example should use this case
return mymap[temp[start]] * mymap[temp[start+1]]
else
return mymap[temp[start]] + mymap[temp[start+1]];
}
当然,还有一些情况你需要注意(例如"五十六万二千一十三"),但它会窃取你的乐趣和学习经验来帮助你处理这些案件,但我相信这应该足以让你开始。
另外,我不是最擅长C++的,所以如果你找到它们,我为我的代码的任何问题道歉,但从我的测试来看,它适用于我编写的主循环(如有必要,我会提供它,但我认为你自己写它会让你受益)
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- std::当在256字节边界上写入整数时,流的奇怪行为
- 内联程序集printf将整数解释为地址
- 是否基于数组B整数打印数组A中的整数
- 如何在C++中将整数转换为其数字数组