Thread: Simple String/Number conversion theory.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope

    Simple String/Number conversion theory.

    Ok, let's say you wanted to write a program where you weren't sure what order your user would input their arguements.

    I'll use an example of a program that accepts an equation with the variable and can solve for the variable. The user should be allowed to input

    5 + x = 3 or
    x + 5 = 3 or
    25x = 100 or so on...
    The idea being, you don't know whether the next value would be a number or a sign or the variable or whatever. To me the obvious answer would be to read the entire thing into a string and parse it. Then it made me think, "What would be the best way to parse a number?" This is the method I came up with.

    After you read the input into a string, you go through the string one element at a time. If it's a number, you convert it and add it to an int variable. Then you check the next element. If that's also a number, you multiply your int by 10 then convert the next element and add it. This seemed like the best method to me and I'll explain why later, but first some code to explain this method:

    for (int x = 0; x < string.length(); x++) {
       if (atoi(string[x]) <= 9 && atoi(string[x]) >= 0)
          i[y] = (i[y] * 10) + atoi(string[x]);
       else if (string[x] == " ") 
       // ... and whatever else I would do for signs, variables,
       // and such...
    Is there a better way of doing this? Perhaps getline() and using space as the delimeter, then working with that to see if it's a number?
    Last edited by SlyMaelstrom; 11-08-2005 at 01:18 AM.
    Sent from my iPadŽ

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    First, I would grab all the numbers out of the input and put them into an array. Then I would parse the operators out and store them into a struct containing the operator and position of it. From there you could calculate the result and have room for expansion.

    int NumberOffset = 1; //For multiplying
    for( int i=string.length-1, int x=0; i>=0; i-- )
          if( atoi(string[i]) <= 9 && atoi(string[i]) >= 0 )
             FinalNumber[x] += (atoi(string[i]) * NumberOffset);
             NumberOffset *= 10;
          else // Its not a number, reset values to handle the next number
              NumberOffset = 1;
    When you find the operators store their position in a struct to find out what numbers need to be transformed. For example, there are 2 numbers with a + operator inbetween them, lets say 56+88. The position would be 1 because its the first operator to come up =). Then you could find the numbers in FinalNumber[] to associate with the operator, in this case, [0] and [1] or position-1 '+' position. That would leave room for more expansion, but that's just how I would start it out, you can do it how you want it.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    I remember asking a similar question some time ago.

    If you were to solve equations like these two:-

    5 + x = 3
    5x - 5 + 3 = 4x - 2
    You can quite happily use the method you have outlined above.

    However, your program will become cumbersome when you consider more complicated expressions such as:-

    5(7x - 3(4 - 2)) = 7(x + 1)
    It so happens that the best way to solve an expression, such as the one shown above, would be to implement a binary tree.

    Or use a binary tree as an expression solver. I've done this to handle equations of this nature.

    An executable version of this exact method (done by somebody else on this forum) is available here:-

    <It's called AlgebraSolver>

    As you can probably tell. The nature of such a program is far from trivial.

    Here is some more information about building an expression solver using binary trees:-
    Last edited by treenef; 11-08-2005 at 03:39 AM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    Wow, thanks for the site.

    Nice job on the program, Hunter2. I'm looking into it.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Conversion tool
    By Thrakity in forum C Programming
    Replies: 18
    Last Post: 04-06-2009, 03:29 AM
  2. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  5. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM