|
|
Algorithm 1. Scan the infix expression from left to right. 2. If the scanned character is an operand, output it. 3. Else, …..3.1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty), push it. …..3.2 Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. Push the scanned operator to the stack. 4. If the scanned character is an ‘(‘, push it to the stack. 5. If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered. 6. Repeat steps 2-6 until infix expression is scanned. 7. Pop and output from the stack until it is not empty. |
infixtopostfix()
is modifying the input string exp
even before it has finished reading from it. That seems risky, at the very least. So the first thing I'd suggest is to use separate strings for the input and output processing. Since the input and output strings may be of differing lengths, I'd suggest, instead of
|
|
|
|
string exp = "a*(b+c)";
gives an infinite loop in the while
loop at line 97.
|
|
|
|
but another thing. how did u know there was an infinite loop at line 97 but then found the problem was at "peek" function. did you use debugging tools or just looked at code? i dont know debugging, so curious... |
char
but was trying to return an int
. I suppose that was enough of a clue, and I could have figured it out without even running the program. But that would have involved understanding the code and since I wasn't the author, it involved getting to grips with the design and how it was supposed to work. At that point I decided to start running the program with different test data.