+ 1

Hi, I really need help with this code. It's for a project

Hi, I've already solved the code now it works, yesterday it had a lot of errors and it didn't compile, well now it does, I just don't know how to make it so that if there are more than two elements in the stack it compares the string variable against each of the elements. I don't know if I can explain it, since entering 3*(5-7)-7*(10/2)+1 should result in 357-*7102/*-1+ for which the code yields 357-*7102/*1+-. https://code.sololearn.com/c4Dinz58ihmF/?ref=app Hola, realmente necesito ayuda con este código. Es para un proyecto Hola, ya he resuelto el código ahora que funciona, ayer tenía muchos errores y no compilaba, bueno ahora sí, sólo que no sé cómo hacerlo para que si hay más de dos elementos en la pila compare la variable de la cadena contra cada uno de los elementos. No sé si puedo explicarlo, ya que al introducir 3*(5-7)-7*(10/2)+1 debería resultar 357-*7102/*-1+ para lo cual el código da 357-*7102/*1+-.

20th Mar 2020, 12:59 AM
Jaime Contreras
Jaime Contreras - avatar
4 Answers
+ 2
Maybe a stack is not the best representation. I think that a trinary tree would represent the problem better than a stack. Each node in the tree would hold three parts: left operand, operator, right operand. Each operand may be either a literal value or a pointer to a sub node that gets added when you encounter an open parenthesis or another operator. In a special case for unary minus the left operand could be null. Yet there are minor complications. You must consider operator precedence when determining which operand becomes a subnode. Using your example, (8/2^3): the right operand 2^3 is a subnode. Whereas for (8^2/3): the left operand 8^2 is a subnode.
27th Mar 2020, 2:09 PM
Brian
Brian - avatar
+ 2
Jaime Contreras I went back to my textbooks and studied this further. I understand the code that you have built is a shift-reduce parser, and I figured out how to make it work with nested parentheses. First, be sure to implement the change that I recommended above to line 38 (only). Create a new integer variable that counts the number of open parentheses "(" as they get pushed onto the P stack. Whenever it pops a "(", then decrement the count. At line 41, if the count is non-zero then break out of the while statement; else, continue and finish emptying the P stack. That is all. Now solve the unary minus problem.
2nd Apr 2020, 7:43 AM
Brian
Brian - avatar
+ 1
I understand that your program converts an input string from Algebraic Notation (AN) into Reverse Polish Notation (RPN). Very cool! I cannot say I resolved the problem, but I observed that it outputs correctly 3 5 7 - * 7 10 2 / * - 1 + by changing line 38 to while (!P.empty) and line 41 to continue;. This lets it pop the P stack all the way when it needs to for this example. I don't know whether more logic is needed for the general case. I expect that my change would fail for nested parentheses. Also I observed that the parsing fails for unary minus. For example, 12*(-4*3). But I suppose you will solve that later.
21st Mar 2020, 6:34 PM
Brian
Brian - avatar
0
hi, you really help me but now when you enter this other 4*(5+6-(8/2^3)-7)-1 you get another result that shouldn't be
25th Mar 2020, 8:47 PM
Jaime Contreras
Jaime Contreras - avatar