在c ++中将单词转换为数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在c ++中将单词转换为数字相关的知识,希望对你有一定的参考价值。
我有问题将单词转换为数字
五千六百三十二 - 5632
如果有人这样做我甚至不知道怎么开始请指导我怎么做...
答案
在这里,我在python中做到了,它将从算法的角度帮助你或其他人。
#!/usr/bin/python
__author__ = 'tomcat'
all = {
"one" : 1,
"two" : 2,
"three" : 3,
"four" : 4,
"five" : 5,
"six" : 6,
"seven" : 7,
"eight" : 8,
"nine" : 9,
"ten" : 10,
"eleven": 11,
"twelve": 12,
"thirteen": 13,
"fourteen": 14,
"fifteen": 15,
"sixteen": 16,
"seventeen": 17,
"eighteen": 18,
"nineteen": 19,
"twenty" : 20,
"thirty" : 30,
"forty" : 40,
"fifty" : 50,
"sixty" : 60,
"seventy" : 70,
"eighty" : 80,
"ninety" : 90,
"hundred" : 100,
"thousand" : 1000,
"million" : 1000000,
"billion" : 1000000000,
"trillion" : 1000000000000,
"quadrillion" : 1000000000000000,
"quintillion" : 1000000000000000000,
"sextillion" : 1000000000000000000000,
"septillion" : 1000000000000000000000000,
"octillion" : 1000000000000000000000000000,
"nonillion" : 1000000000000000000000000000000
};
spliter = {
"thousand" : 1000,
"million" : 1000000,
"billion" : 1000000000,
"trillion" : 1000000000000,
"quadrillion" : 1000000000000000,
"quintillion" : 1000000000000000000,
"sextillion" : 1000000000000000000000,
"septillion" : 1000000000000000000000000,
"octillion" : 1000000000000000000000000000,
"nonillion" : 1000000000000000000000000000000
};
inputnumber = raw_input("Please enter string number : ");
tokens = inputnumber.split(" ");
result = 0;
partial_result = 0;
for index in range(len(tokens)):
if tokens[index] in spliter :
if partial_result == 0:
partial_result = 1;
partial_result *= all[tokens[index]];
result += partial_result;
partial_result = 0;
else:
if tokens[index] == "hundred" :
if partial_result == 0:
partial_result = 1;
partial_result *= all[tokens[index]];
else:
partial_result += all[tokens[index]];
result += partial_result;
print result;
另一答案
一般来说,如果你不需要编写代码,你会怎么做?在这个例子中,你的单词集合是:
Five Thousand Six Hundred Thirty two
我们可以将每个转换为数字以获得以下集合:
5 1000 6 100 30 2
从5开始(提示:5 <1000在1000的左边。这表明......!??)在获得数字5632时你会采取什么步骤?
如果这个号码怎么办?
六三十三亿五十四百万二万二千三四?
你能想出某种规则(或更好的算法)吗?
一旦你将大问题分解成一系列小问题,那么接下来的战斗就是找到正确解决每个小问题的正确编码方法
另一答案
希望this给你一些开始: -
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, int> reference;
string ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
void storeOnes(){
for(int i = 0; i < 11; i++){
reference[ones[i]] = i;
}
}
int main(){
//set up
storeOnes();
string test = "onetwothreetwofour";
string buffer;
for(int i = 0; i < test.length(); i++){
buffer.push_back(test.at(i));
map<string, int>::iterator it = reference.find(buffer);
if(it != reference.end()){
cout << (*it).second;
buffer = "";
}
}
cout << endl << endl;
system("pause");
return 0;
}
另一答案
考虑使用map
。说five thousand six hundred three ten two
。
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,int> digits;
digits["one"] = 1;
digits["two"] = 2;
digits["three"] = 3;
digits["four"] = 4;
digits["five"] = 5;
digits["six"] = 6;
digits["seven"] = 7;
digits["eight"] = 8;
digits["nine"] = 9;
digits["ten"] = 10;
digits["hundred"] = 10;
digits["thousand"] = 1000;
const int num_len = 7;
string num_str[num_len]={"five", "thousand", "six", "hundred", "three", "ten", "two"};
int number = digits[num_str[0]]*digits[num_str[1]] +
digits[num_str[2]]*digits[num_str[3]] +
digits[num_str[4]]*digits[num_str[5]] +
digits[num_str[6]];
cout << number;
}
另一答案
另一种方法是通过递归来完成(在java和c ++中)
https://github.com/jman27182818/words_to_numbers
大多数代码都是关于解析字符串以获得可以递归操作的字符串向量。基本上算法是
A[] = String array //example {"three","hundred"}
Integer converter(A){
if(length(A) <= 4)
handle_base_case;
//either A only has small values or is of the form
//{"three","billion"}
//if length is greater than 4 the array must have a large value
index = find_first_value_greater_than_100(A);
arrayl = A[1:index-1];
arrayr = A[index+1:A.end()];
return (convert_hundreds(arrayl) * Value_of(A[index])+ converter(arrayr) );
}
其中“convert_hundreds”接受一个数组的数组,每个字符串的值不大于100(或西班牙语为1,000)并返回数值。这个算法比前一个算法更加耗费内存,但我喜欢它,因为它似乎更好地推广到其他语言。
以上是关于在c ++中将单词转换为数字的主要内容,如果未能解决你的问题,请参考以下文章