It doesn't work? The first thing that springs to mind, is that you do no useful checking on what the user entered.
The second is that what happens if the first character entered is not a digit? I strongly suggest that you learn to use the debugger. Put a breakpoint on the line while (i.
You didn't push your operator on the stack, yet you try to pop it from an empty stack. editJust in case you don't know what I'm talking about, it's the switch statement. Your switch(operation.top)refers to an empty stack, causing the error. Replace it with switch (inputi)and it might work./edit P.S.: (for the sake of completeness) After a short exchange of a preliminary solution based on this answer (see Solution from OP), the additional fixes required were: - Change stackoperation; to stackoperation; - Change operation.push(inputi); to operation.push(inputi-'0'). When answering a question please:. Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad spelling and grammar. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem.
Insults are not welcome. Don't tell someone to read the manual. Chances are they have and don't get it.
Postfix Evaluation In C++
Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
Of the top of my head, your code is lacking in several departments: As @ChristianAichinger already pointed out, its formatting is horrible. This is a defect, since neither you nor anyone else will be able to read it. Your else if does not open a block and therefore only operates on the next statement, which is operand2=teststack.top; and not the whole part that follows it. This would have been more obvious with proper formatting. There is no part in your code that parses integers. Therefore, your code is missing a major part of its functionality. More errors may exist, but analysis is fairly hard as long the code is in such a state.
And I was wondering if it's possible to do something as elegant as this but for postfix evaluation. Postfix notation isn't meant to be processed with a call stack or recursion or really anything of that nature. Its designed for nice, tight implementations that just have a simple loop and a stack next to it. These fall into the realm of stack machines and are often found in embedded systems because of their simplicity. You find things like forth, postscript and the jvm as very successful stack machines (see ) along with venerable HP. The elegance of postfix can be seen in the simple loop: while not end of file read into var if var is not operand push var else if var is '+' pop into v1 pop into v2 push v1 + v2 else if var is '.'
![]()
pop into v1 pop info v2 push v1. v2 And thus you go through and implement all the operands and you're done. Dc has 27 operands (things like print (pop and print) P, print (just print) p, print the stack f, clear the stack c, etc.). You can read them. I will admit there are some more elegant structures for implementing the operands rather than a huge if else if cascade depending on the language. But you get the idea.
Java Postfix Evaluation Code
The elegance of the system is its simplicity. You don't have to worry about stack frames, calling conventions, and the like when implementing it. You've got a stack and a variable or two.
Steps To Evaluate a Postfix Expression. Scan the characters of the postfix string from left to right one by one.
If the character is an operand then push it on the stack. If the character is an operator then pop two elements from the stack and apply the operator to these two characters.
Now, push the result on the stack. After all the characters have been scanned, pop the remaining element of the stack and that is the value of the arithmetic postfix expression. Z-Ro. Example Infix Expression: (3 + 4). (2 / 2) Postfix Expression: 34+22/.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |