# Thread: Simple String/Number conversion theory.

1. ## 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

Code:
```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:

Code:
```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] == " ")
y++;
// ... 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?

2. 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.

Code:
```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;
x++;
}
}```
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.

3. I remember asking a similar question some time ago.

If you were to solve equations like these two:-

Code:
```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:-

Code:
`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:-

http://kevklam.galacvalley.com/

<It's called AlgebraSolver>

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