Stack parsing arithmetic expressions

Source: Internet
Author: User

With stacks, parsing arithmetic expression problems becomes much easier at once. The sample code given can parse any arithmetic expression consisting of +,-,*,/, () and 0 to 9 digits.

infix Expressions and suffix expressions
Infix expressions are usually called arithmetic expressions, such as (1+2) *3-4.
A suffix expression is an expression that, after parsing, is followed by an operator after the operand, such as parsing a suffix expression on a 12+3*4-. This expression can be solved directly using the stack.

Precedence of Operators

Priority level

Operator

1

Brackets ()

2

Negative sign

3

exponentiation * *

4

Multiply by *, except/, to seek the remainder%

5

Plus +, minus-

6

Less than <, less than or equal to <=, greater than, greater than or equal to >=

7

equals = =, not equal to! =

8

Logic and &&

9

Logical OR | |


The general rule is that unary operators > two-ary operators > Multivariate operators.

4the process of parsing an arithmetic expression using a stack
The infix expression is translated into a suffix expression as follows:
(1) The data ch is obtained sequentially from left to right.
(2) If CH is the operand, direct output.
(3) If CH is an operator (with left and right brackets), then:
A: if ch = ' (', put the stack.
B: if ch = ') ', output the operator in the stack sequentially until ' (') is encountered.
C: If CH is not ') ' or ' (', then the operator top of the stack vertex position is compared with precedence.
1: If the CH priority is higher than top, then the CH is placed on the stack.
2: If the CH priority is below or equal to top, then output top, then put CH into the stack.
(4) If the expression has been read completed, and there are operators in the stack, the top output is followed.
If we have an expression (A-B) *c+d-e/f, to translate it into a suffix expression and store the suffix expression in a string called output, you can use the following steps.
(1) Read ' (', pressed into the stack, output is empty
(2) Read A, is the operand, output directly to the output string, the output = a
(3) Read '-', at which point there is only one ' (', so '-' will be pressed into the stack, output = a
(4) Read B, is an operand, output directly to the output string, the output = AB
(5) Read ') ', this time output stack inside the operator '-', and then is ' (', direct eject, output = ab-
(6) Read ' * ', is operator, because the stack is empty at this time, so directly pressed into the stack, output = ab-
(7) Read C, which is the operand, output directly to the output string, outputs = Ab-c
(8) Read ' + ', is operator, its priority is lower than ' * ', then pop ' * ', press ' + ', output = ab-c*
(9) Read D, is the operand, output directly to the output string, the output = Ab-c*d
(10) Read '-', is the operator, and the ' + ' priority, so pop ' + ', then press '-', output = ab-c*d+
(11) Read E, is the operand, output directly to the output string, the output = Ab-c*d+e
(12) Read '/', is the operator, higher than '-' priority, so press the stack, output = Ab-c*d+e
(13) Read F, is the operand, output directly to the output string, the output = Ab-c*d+ef
(14) The original string has been read, and the remaining operator in the stack pops up sequentially, output = ab-c*d+ef/-

5Calculating an arithmetic expression
When you have a suffix expression, the value of the expression is very easy. can be calculated according to the following process.
(1) Scan the expression from left to right, one takes out a data
(2) If data is the operand, it is pressed into the stack
(3) If data is an operator, the number of data that the operator needs to be popped out of the stack, the operation is performed, and the result is pressed onto the stack
(4) If the data is processed, the last remaining data in the stack is the final result.
For example, if we are dealing with a suffix expression 1234+*+65/-, the following steps are given.
(1) First 1,2,3,4 are operands, and they are all pressed into the stack
(2) Get ' + ', for operator, eject data 3, 4, get result 7, then press 7 into stack
(3) Get ' * ', for operator, eject data 7, 2, get Data 14, then press 14 into stack
(4) Get ' + ', for operator, eject data 14, 1, get result 15, then press 15 into stack
(5) 6,5 are all data and are pressed into the stack
(6) Obtain '/', for operator, eject data 6, 5, get result 1.2, then press 1.2 into stack
(7) Obtain '-', for operator, popup data 15, 1.2, to get data 13.8, this is the final result of the operation

Stack parsing arithmetic expressions

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.