简体   繁体   中英

Determine precedence of mathematic operators

I have to convert a non-fully parenthesized sequence from infix to postfix. This also needs to use stacks. There is a stack for operators to be stored. I need to determine the precedence of the operators to make sure that the proper operator is printed in the correct spot in the converted postfix sequence. Here is pseudocode:

do if (the next input is a left parenthesis) Read the left parenthesis and push it onto the stack. else if (the next input is a number or other operand) Read the operand and write it to the output. else if (the next input is one of the operation symbols) { Pop and print operations off the stack until one of three things occurs: (1) The stack becomes empty, (2) the next symbol on the stack is a left parenthesis, or (3) the next symbol on the stack is an operation with lower precedence than the next input symbol. When one of these situations occurs, stop popping, read the next input symbol, and push this symbol onto the stack. } else { Read and discard the next input symbol (which should be a right parenthesis). Pop and print operations off the stack until the next symbol on the stack is a left parenthesis. (If no left parenthesis is encountered, then print an error message indicating unbalanced parentheses and halt.) Finally, pop and discard the left parenthesis. } while (there is more of the expression to read);

The bolded text is the confusing part to me. Does anyone have a suggestion on an approach to this? Let me know if more information is needed....

Seems like the procedure described is a bit vague to me too but I think what it is saying is say you have

...(2 + 3 ^ 4 * 5 - 6)...

and your stack starts off looking like (

so you find 2 and do your thing with it and find + . The next symbol on the stack is ( so you read the + and push it on to the stack. Now your stack looks like (+ .

Then you find 3 , do your thing with it and move on to find ^ . The description makes it sound like you could start popping here because it meets the second else if but you don't because of what you've emboldened. The next symbol on the stack is + and of lower precedence. You don't know what the symbol after the ^ is so you can't start popping yet. Instead, you push ^ on to the stack and keep parsing. Your stack looks like (+^ .

You find 4 and do your thing with it. Then you parse forward and you find * . The next symbol on the stack is of higher precedence so you start popping until the next symbol on the stack is + . Being of lower precedence than * ("the next input"), you push * on to the stack and parse forward again. Your stack looks like (+* .

You find 5 and do your thing with it. I guess normally you'd have a #2 stack with the numbers in it if you were making a real calculator. Now the next input symbol is - so you pop all the way back until the next symbol on the stack is ( again. You stop popping again because you still have - as the next input waiting around for whatever is on the right side. (You are pretending you have 407 stored in memory somewhere from the popping you just did.)

You parse forward, get to the 6 and do your thing with it. The next symbol is ) which doesn't fit any of the inner conditions so you discard it. If you had a long line of operators piled up in the stack here, you would start popping again but instead you just print the - . Now the next symbol on the stack is ( again. You pop and discard it.

I'd guess that's how it's supposed to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM