Converting words to numbers in c++

元气小坏坏 提交于 2019-12-09 23:46:01

问题


I have problem converting words into numbers like

Five Thousand Six Hundred Thirty two - 5632

I don't even know how to start if someone has done it please guide me how to do it...


回答1:


Here, i did it in python, it will help you or someone else from algorithmic perspective.

#!/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:


How would you do this, in general if you didn't have to code it up?? In this example your collection of words is:

Five Thousand Six Hundred Thirty two

We could convert each of those to numbers to get the following collection:

5 1000 6 100 30 2

Starting from 5 (hint: 5 < 1000 is to the left of 1000. This suggests...!??) what steps would you follow in getting to the number 5632?

What if the number was

Six hundred thirty three billion fifty four million two hundred twenty three thousand four?

Can you figure out some sort of rule (or better, an algorithm)?

Once you have broken the big problem down into a collection of little problems then the next battle is to find the correct way of coding something that correctly solves each little problem




回答3:


Hope this gives you some start:-

#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;
}



回答4:


Consider using map. Say 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;    
}



回答5:


Another way to do this is by recursion as is done here (in java and c++)

https://github.com/jman27182818/words_to_numbers

Most of that code is about parsing the string in order to obtain a string vector which can be acted upon recursively. Essentially the algorithms is

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) );
    }

where "convert_hundreds" takes an array with strings whose values are no large than 100 each (or 1,000 for spanish) and returns the numerical value. This algorithm is more memory intensive than the previous one but I like it because it seems to generalize to other languages better.



来源:https://stackoverflow.com/questions/18548345/converting-words-to-numbers-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!