Recursion Concern

closed account (365X92yv)
I was wondering how you would code a function to be able to check the input stream to see if it had data still in it and be able to loop over a section of code. Say, if I inputted...

1/2 + 2/3 - 4/5 * 2/5

how would I write code to do this?

And then how would I get it to learn how to do the processes of operation in the right order?
Last edited on
I would bring it all into a string, locate the symbols for mathematics, extract the integers (as doubles), and then use the built in order of precedence to do the mathematical calculations.
Recursion is not nessesary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool done = false;
string input;
getline(cin, input);

for (int i = 0; string[i] != NULL ; i++)
{
    //Do something with each character here.  
    //Recognize the locations of operators, brackets, numbers
}

while (done == false)
{
    //start evaluating individual sections and sticking those in buckets seperated by operators.
    //In the example, we could detect that '1/2' is safe to evaluate and '2/3' is safe.
    //Evaluate those and replace them with 0.5 and 0.6666...
    if ("one element left with no operators") done = true;
}
Last edited on
Topic archived. No new replies allowed.