This program provides another and cases of the application of the Stack besides what I had mentioned in a previous a Rticle:(1) Class infixexpr converts an Infix expression to a Suffix expression by using a operator stack;(2) Class Suffix calculates the value of a Suffix expression by using a stack containing operands. Sample Input and Output: 1 ImportJava.util.*;2 ImportJava.io.*;3 4 classSuffix {5 PrivateStackStack;6 7 PublicSuffix ()
/****************************/ A /*to determine the precedence of an operator*/ - /****************************/ - intPriorityCharop) the { - Switch(OP) - { - Case '(':return 0; + Case '+': - Case '-':return 1; + Case '*': A Case '/':return 2; at default:return-1; - } - } - - /*********************************/ - /*infix expression, converted to postfix expressio
The infix expression is converted into a suffix expression and evaluated.
Algorithm:
How to convert an infix expression to a suffix expression:1. operations encountered: Direct output (added to the suffix expression)2. When the stack is empty, an operator is used to directly access the stack.3. Left brackets: add them to the stack4. Right parenthesis: Execute the stack exit Operation and output the elements
For more information about the stack API, see my other blog post: Chained storage for stacks-API implementationsInstance:5 + 4=> 5 4 +1 + 2 * 3 = 1 2 3 * +8 + (3–1) * 5 = 8 3 1–5 * +Infix expression conforms to human reading and thinking habitsThe suffix expression conforms to the computer's "operating Habits"infix suffix algorithm:Iterating through the numbers and symbols in an
Description
Convert infix expression to postfix expression ). Assume that the operands in the infix expression are represented by a single English letter and contain only the left brace '(', right brances') 'and Binary Arithmetic Operators + ,-,*,/.Input Format
The first line is the number of test samples n.The following n rows, each of which is a string that represents an
Tags: blog HTTP ar SP problem log html bs ef
Problem description:
Example of prefix fix:
(4 + 2) * (3 + 6) => * + 4 2 + 3 6
(3/4 + 2)-5 =>-+/3 4 2 5
(3 + 4/2)-5 =>-+ 3/4 2 5
If the expression is drawn into a tree structure, the prefix, infix, and suffix are the first, middle, and last traversal of the tree.
-----------------*------------------
------------/-----------\-----------
-------- + ---------------- + ---------
------/----\
Arithmetic is one of the important applications of stackInfix expression-to-suffix expression (inverse Polish algorithm) process
Iterating through infix expressions from left to right
Digital direct output as part of the suffix expression
If it is a symbol, the priority of the top element of the stack is judged
Above stack top element priority directly into the stack
The top element of the stack top priority stack is below or
For the use of low-level programming language is to achieve the processing of expressions, suffix (inverse polish) is the most convenient. The following is an algorithm that converts infix (common expressions) to Postfix:Stack bottom ' # ', read infix verbatim from left to right:A. When the current character is a number, the output is direct;B. When the current character is "(", the stack is pressed;C. When
it is an operator, then the top two elements of the stack pop up as a corresponding operation, the result is then into the stack, and finally when the expression scan, the stack is the result.The general algorithm for converting a normal middle-order expression to an inverse Polish expression is:(1) First constructs an operator stack, which follows the principle that the higher the top priority of the stack is in the stack.(2) read into a simple arithmetic expression expressed in
Suffix expressionin inverse Polish notation, the operator is placed after the operand. For example, when expressing "three plus four", write "3 4 +" instead of "3 + 4". If there are multiple operators, the operator is placed after the second operand, so the conventional infix notation of "3-4 + 5" in inverse Polish notation "3 4-5 +": first 3 minus 4, plus 5. One advantage of using inverse Polish notation is that you do not need to use parentheses. Fo
Person: infix expressionComputer: suffix expressionThe infix suffix is required and then calculated. Either step requires a stack:For (3 + 4) x5-6One infix turn suffixThe stack involved is used to store symbols such as "+-x/()"Thought:Meet the digital output; Meet the symbol into the stackWhen the empty stack or symbol is the left parenthesis, it is directly into
Lai Yonghao (http://laiyonghao.com)
Note: This article is basically a translation of this article.ArticleHttp://dev-tricks.net/pipe-infix-syntax-for-python ).
Through the pipe module, you can use the infix syntax in Python.
First, let's take a look at the traditional prefix syntaxCode:
Sum (select (where (take_while (FIB (), Lambda X: x
Hard to read? Let's take a look at the fix syntax code:
Infix expression---postfix expression
From left to right to iterate through the infix expression of each number and symbol, if the number is directly output, that is, as part of the suffix expression, if the symbol is divided into two cases, is the right parenthesis in the stack of left parenthesis (with the left parenthesis) of the elements out of the stack, not the right parenthesis to determine the prec
First come to the experiment Instruction book:First, the experimental taskdesign, compile and debug an infix expression into the suffix expression of the experimental program, deepen the analysis of the word, grammar analysis, semantic analysis and code generation understanding.Ii. contents of the experiment1. LexicalInput: Extend ASCII character set character. In addition to case 26 letters and numbers 0-9 (digit) and +-*/^ =; , (), all other charact
than +, then we will first put the * + eject and output, and then + stored in the stack. Note: Only the closing parenthesis can handle the opening parenthesisCases:We putA + b * C + (d * e + f) * gConverted toA b c * + D e * f + G * +The first stepOutput:aStaging stack:Step Two:Output:aStaging stack:+Step Three:Output:a BStaging stack:+Fourth Step:Output:a BStaging stack:+ *Fifth Step:Output:a b CStaging stack:+ *Sixth step:Output:a b c * +Staging stack:+Seventh Step:Output:a b c * +Staging st
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.