Recursivity problem

In my program I enter an expression, the program divides it based on + or - into subexpressions, so I can differentiate every simple subexpression or subsubexpression...

Since I have already divided the expression in subexpressions, this code analizes every subexpression and divides it even more... but there is a problem... lets say I have (2x+3)+(x+4), the program divides only the first one (2x+3) and finishes...

but it shouldn't...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    /*Process*/
    for(i=0; i<=numerosubexpresiones; i++){

        cout<<"\n Expresion actual: "<<subexpresiones[i];

        for(j=0; j<=subexpresiones[i].length(); j++){

           

            if((subexpresiones[i][j]=='-') || (subexpresiones[i][j]=='+') && (repeticion==false)){

                
                repeticion=true; 

                analizar(subexpresiones[i], repeticion); 

                return;
                
            }


        }


    }

}
/*FIN*/




Last edited on
It seems to me that this is just because you don't deal with ()s. Have a variable parenthesis_level = 0. Each time you find '(', increment it, each time you find ')', decrement it. Only look for operators when parenthesis level = 0. Also, if the first and last symbols are ( and ), remove them.
+1 Hamsterman
for me it is the insuffecient information you have given .
Hamster, the program deals with parenthesis while dividing the first expression into subexpressions, for example if I have:

(3x-2)+(3x-1), the program creates
s1: 3x-2
s2: +
s3: 3x-1

now the problem is when the program must subdivide using that cycle expressions like s1 and s3 into smaller ones like
s1.1 3x
s1.2 -
s1.3 2


It does, but only for the first one, then it stops... it is probably something with the "return" with Im not sure if I have to use it, but if I delete it, the program runs forever lol...

here is my complete code since it wouldn't let me publish more than 9000chars here...

http://codeviewer.org/view/code:227a

My program does this

user inputs expression
expressions goes to analizar()
there it is divided into subexpressions and a for analizes if it needes more subexpressions, then it calls analizar again but with the subexpression, then it is supossed to finish with the new analizar() process and go back to the previous analizar() process to continue checking if the program gets another subexpression to subdivide more...
But it doesn't...

http://codeviewer.org/view/code:227a
It is difficult to decide on the best way to solve this problem. The best way would be to parse the expression into an intermediate representation. Although it's a bit of work. You may be able to get away with what you're doing, but you seem to be making a mess of it. Your analizar should have 4 steps in it:
1. If 'expression' is surrounded by parentheses, remove them.
2. Try finding + or - not enclosed in any ()s.
3. If you've found + or -, split the expression in two halves at that point. Your code could be
1
2
3
analizar(first_halve);
cout << symbol_you_found;
analizar(second_halve);
The splitting can be done with string.substr() method.
4. If you haven't found + or -, there is nothing you can do. The approach you have chosen does not allow you to simplify (x+1)*x or etc. Now call reglas on it. I should add that it is lacking (do you have output elsewhere?). How do you differentiate x, 3x, or 1-x*x?
Topic archived. No new replies allowed.