Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:18:52+00:00 2026-06-01T17:18:52+00:00

I’m developing a program that makes basic calculations using words instead of numbers. E.g.

  • 0

I’m developing a program that makes basic calculations using words instead of numbers. E.g. five + two would output seven.

The program becomes more complex, taking input such as two_hundred_one + five_thousand_six
(201 + 5006)

Through operator overloading methods, I split each number and assign it to it’s own array index.

two would be [0], hundred is [1], and one is [2]. Then the array recycles for 5006.

My problem is, to perform the actual calculation, I need to convert the words stored in the array to actual integers.

I have const string arrays such as this as a library of the words:

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

If my ‘token’ array has stored in it two hundred one in index 0, 1, and 2, I’m not sure what the best way to convert these to ints would involve.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:18:53+00:00Added an answer on June 1, 2026 at 5:18 pm

    Part of the trick for me was to parse the tokens backwards, rather than forwards. And to maintain a scale factor as you move back through the tokens. For each token you either add to the current value (scaled by the current value of the scalefactor) or adjust the scalefactor, depending on which token you encounter.

    Here’s my implementation (some bits are not included, and you’d need to add a little logic to handle millions).

    #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() ) );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.