c++ – How to remove extra parentheses from a mathematical expression?


The following problem came across: "using a queue to remove unnecessary parentheses from an arithmetic expression." Initially it seemed very simple, but in practice it caused a lot of difficulties.

Adding @mymedia :

Extra brackets are those that can be removed while retaining the meaning of the expression. For example, (5*7)+35*7+3 , ((a+b))a+b .


The task is really not the simplest one and cannot be solved through the stack. The problem of determining the correctness / incorrectness of the parenthesis sequence is solved through the stack. Extra brackets cannot be removed like this. HERE you can read about the reverse Polish notation. Expand the expression, put it back together and put parentheses where necessary. Then there will be no superfluous ones.

PS queue / stack is used there to save operations.

Scroll to Top