C Program and Algorithm for Evaluation of a Postfix Expression. C Program for Array Representation of Stack. Evaluation of a postfix expression using a stack is. C program to implement evaluation of Postfix expression using Stack. C Program to implement Infix to Postfix Expression conversion algorithm. C program to implement evaluation of Postfix expression using. Infix Expression Evaluation. Tokens and the Tokenizer. The purpose of this lecture is to develop a program that can correctly evaluate complex arithmetic expressions like. The code I am going to show below is based on code found in Data Structures and Problem Solving Using C++, Second Edition by Mark Allen Weiss. The first step in writing such a program is breaking up the input into a sequence of tokens, where each token represents either an arithmetic operation, a number, or parentheses. Weiss has written a token class to represent individual tokens and a tokenizer class that takes a line of characters from some input stream and breaks it up into a stream of tokens. Here is the code for these two classes. When it sees something that looks like the beginning of a number it puts that character back on the stream and then asks to read a number using the usual > > notation. Postfix Evaluation using C++ Stack. Tree Traversal using C Programming C program to implement Binary. Implementation Postfix Expression Evaluation. C Program To Implement Postfix Expression Evaluation CPP Program To Implement Postfix Expression Evaluation. C Program To Implement Postfix Expression Evaluation : #include #include #include #include #include. Stack Applications; Evaluation of postfix expression. Balancing Parentheses. A very simple application of the stack class and the tokenizer is to read an arithmetic expression and determine whether or not the parentheses in the expression are correctly matched. Here is an algorithm for solving this problem. Scan the tokens from left to right ignoring everything but open parenthesis and close parenthesis tokens. Whenever you encounter an open parenthesis token, push it on the stack. Whenever you encounter a close parenthesis token, pop the stack. If the stack is empty, output an error message. When you have read all the tokens, check to see whether or not the stack is empty. If the stack is empty, declare success. If the stack still has something on it you have an unbalanced open parenthesis in your expression. Here is code for a program that implements this algorithm. An alternative form is postfix form, which writes the operator after the two things it operates on. For example, the postfix equivalent of. One useful feature of postfix notation is that it never requires parentheses. For example, the postfix equivalent of. There is a very simple algorithm for evaluating postfix expressions. Scan the expression from left to right. Push any numbers you encounter on a value stack. Any time you encounter an operator, pop the top two numbers off the value stack, apply the operation to them, and push the result back on the stack. The number that appears at the top of the stack at the end of the scan is the result of the calculation. If you try this algorithm with any of the examples shown here you will see that it produces the right results. Here is the code for a simple program that carries out this postfix evaluation algorithm. The algorithm uses a queue to store the results and a stack to store operators while the algorithm runs. Each operator has a precedence associated with it, as shown in the table. Here is the outline of the algorithm. Scan the tokens from left to right. If you encounter a number token, move it immediately to the result queue. If you encounter an operator and the operator stack is empty, push the operator on the stack. If you encounter an operator whose precedence is greater than that of the operator at the top of the stack, push the new operator on the stack. If you encounter an operator whose precedence is less than or equal to the precedence of the operator at the top of the stack, pop the stack and move the operator from the stack to the output queue. Repeat this step until either the stack empties or an operator appears at the top of the stack whose precedence is smaller than the precedence of the current operator. Push the new operator on the stack. When you reach the end of the input, move any remaining operators from the stack to the result queue. Handling Parentheses. The algorithm is relatively easy to modify to handle parentheses. Whenever you encounter an open parenthesis, push it on the stack. When you encounter a close parenthesis, pop operators off the stack and move them to the output queue until you encounter the matching open parenthesis. Left Associative vs. Right Associative Operators. There is one correction we will have to make to rule 5 above. This correction applies to multiple appearances of the same operator. The rule as it is written does the correct thing with the example. The rule effectively treats this expression as if it were written. That is the correct postfix equivalent. Next, consider this example. Most arithmetic operators are left associative, which means that multiple repetitions are grouped from left to right. The exponentiation operator on the other hand is right associative, which means it groups from right to left. How do we modify rule 5 to do the right thing with both left associative operators and right associative operators? The solution that Weiss uses is to give operators one precedence when they appear at the top of the operator stack and a slightly different precedence when they appear as a new operator. Furthermore, you can also extend this rule to handle parentheses as a special case of operators. Here is a more complete operator precedence table. A simple optimization. The algorithm above describes how to correctly convert an expression in infix form into an expression in postfix form. The algorithm uses a queue designed to receive the tokens that make up the postfix expression as we build it from left to right and a stack that holds operator tokens. The stack simply helps to re- order the operators relative to the operands so we can do the conversion. When the conversion to postfix is complete, we could then read the contents of the queue from left to right by removing successive items from the front of the queue. We would then use a second stack to hold the operands as if we were running the usual postfix evaluation algorithm. A simple optimization that can be applied to this process is to effectively eliminate the queue. What we do instead is transfer numbers directly from the input to the postfix stack, bypassing the queue. Instead of pushing operators on the queue only to remove them later, we can use up our operators right away by operating on the postfix stack. This is the optimization that the code below will use to simultaneously do the infix to postfix transformation and the postfix evaluation. This optimization allows us to optimize away the queue that would normally store the postfix expression before evaluation. Hence, you will see two stacks in the code below, but no queue. Source code. We are now finally ready to see some code. The code below uses an Evaluator class to hold the major methods that implement the algorithm.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |