我正在开发一个程序,该程序使用单词而不是数字进行基本计算。例如。 5 + 2将输出7。
程序变得更复杂,需要输入诸如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中存储了211,我不确定将这些转换为int的最佳方法是什么。
最佳答案
对我而言,技巧的一部分是向后解析 token ,而不是向前解析 token 。并在返回 token 时保持比例因子。对于每个 token ,您可以将其添加到当前值(按比例因子的当前值进行缩放),也可以根据遇到的 token 来调整比例因子。
这是我的实现(不包括一些位,您需要添加一些逻辑来处理数百万个)。
#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() ) );
}
关于c++ - C++将数字转换为整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10083143/