在c ++中将单词转换为数字

时间:2013-08-31 12:52:01

标签: c++ numbers words

我在将单词转换为

等数字时遇到问题

五千六百三十二 - 5632

如果有人这样做,我甚至不知道如何开始,请指导我如何做...

6 个答案:

答案 0 :(得分:2)

如果你不需要编写代码,你会怎么做?在此示例中,您的单词集合为:

Five Thousand Six Hundred Thirty two

我们可以将每个转换为数字以获得以下集合:

5 1000 6 100 30 2

从5开始(提示:5< 1000在1000的左边。这表明......!??)在获得数字5632时你会采取什么步骤?

如果数字是

怎么办?

六三十三亿五十四百万二万二千三四?

你能想出某种规则(或更好的算法)吗?

一旦你将大问题分解成一系列小问题,那么下一场战斗就是找到正确解决每个小问题的正确方法

答案 1 :(得分:2)

在这里,我是在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;

答案 2 :(得分:0)

希望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;
}

答案 3 :(得分: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;    
}

答案 4 :(得分:0)

另一种方法是通过递归来完成(在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) );
    }

其中&#34; convert_hundreds&#34;采用一个数组的字符串,其值不大于100(或西班牙语为1,000)并返回数值。这个算法比前一个算法更加节省内存,但我喜欢它,因为它似乎更好地推广到其他语言。

答案 5 :(得分:0)

这是C ++中的过程

输入:999.99

输出:9999

矢量“ talika”看起来像这样: 9 1000 9 100 9 10 9 1

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool endswith(string a, string b)
{
    //To check if the word ends with ty eg. thirty, forty

    int len = a.length();
    string last_two = a.substr(len-2, len-1); //ty

    if((last_two.compare(b)) == 0)return true;

    return false;
}

int main() {

    int t,i,j;
    int n,cnt, num;

    cin >> t;
    getchar();

    map<string, int>conv;
    vector<int>talika;

    conv["one"] = 1;
    conv["two"] = 2;
    conv["three"] = 3;
    conv["four"] = 4;
    conv["five"] = 5;
    conv["six"] = 6;
    conv["seven"] = 7;
    conv["eight"] = 8;
    conv["nine"] = 9;
    conv["ten"] = 10;

    conv["eleven"] = 11;
    conv["twelve"] = 12;
    conv["thirteen"] = 13;
    conv["fourteen"] = 14;
    conv["fifteen"] = 15;
    conv["sixteen"] = 16;
    conv["seventeen"] = 17;
    conv["eighteen"] = 18;
    conv["ninteen"] = 19;


    conv["thousand"] = 1000;
    conv["hundred"] = 100;

    conv["twenty"] = 20;
    conv["thirty"] = 30;
    conv["forty"] = 40;
    conv["fifty"] = 50;
    conv["sixty"] = 60;
    conv["seventy"] = 70;
    conv["eighty"] = 80;
    conv["ninety"] = 90;

    while(t--)
    {
        string num_in_word, ongsho;
        getline(cin,num_in_word);//get the number in words
        stringstream x(num_in_word);

        bool sheshe_ty = false;

        while(getline(x,ongsho, ' '))
        {
            num = conv[ongsho];
            if(endswith(ongsho,"ty"))
            {
                talika.push_back(num/10);
                talika.push_back(10);
                sheshe_ty = true;
                continue;
            }

            talika.push_back(num);
            sheshe_ty = false;
        }

        if(conv[ongsho] != 1000 && conv[ongsho] != 100 && sheshe_ty == false){
            talika.push_back(1);
        }


        num = 0;

        for(i=0;i < talika.size();i++)
        {
            num += talika[i] * talika[i+1];
            i++;
        }

        cout << "The Number: " << num << endl;

        talika.clear();

    }
    return 0;
}