The infix expression is converted into a suffix expression and evaluated.

Source: Internet
Author: User

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 stack
4. Right parenthesis: Execute the stack exit Operation and output the elements of the stack until the left parenthesis is displayed and the left parenthesis is not output.
5. Other operators: subtraction, multiplication, division
6. Finally, the elements in the stack are output in sequence.
For example
A + B * c + (d * e + f) * g ----> abc * + de * f + g * +

In case of a: Direct output:
Suffix expression:
STACK: NULL

Encounter +: Stack: NULL, So + inbound Stack
Suffix expression:
STACK: +
B: Direct output
Suffix expression: AB
STACK: +
*: The stack is not empty, but the priority of + is not higher than *. Therefore, *
Suffix expression: AB
STACK: * +
C: Direct output
Suffix expression: abc
STACK: * +
+: The stack is not empty. The * priority in the stack is greater than +, and the output and output stacks. The + priority in the stack is equal to +, and the output and output stacks. Then, the operator (+) inbound Stack
Suffix expression: abc * +
STACK: +
Encounter (: directly into the stack
Suffix expression: abc * +
STACK: (+
D: Output
Suffix expression: abc * + d
STACK: (+
*: The stack is not empty, and the stack has a priority of less than *, so no stack exists.
Suffix expression: abc * + d
STACK: * (+
E: Output
Suffix expression: abc * + de
STACK: * (+
Encounter +: because the priority of * is greater than +, output and output stacks, but (the priority is lower than +, so the * out stack, + into Stack
Suffix expression: abc * + de *
STACK: + (+
F: Output
Suffix expression: abc * + de * f
STACK: + (+
): Execute the stack and output the elements until the left brackets are displayed. The brackets are not output.
Suffix expression: abc * + de * f +
STACK: +
*: The stack is empty.
Suffix expression: abc * + de * f +
STACK: * +
G: Output
Suffix expression: abc * + de * f + g
STACK: * +
End with an infix expression: all operators are displayed and output.
Suffix expression: abc * + de * f + g * +
STACK: NULL

Routine:

This is a simple computation program written by myself. It can only calculate the number within 10 and supports the +-*/() operator.

# Include
 
  
Using namespace std; bool IsOperator (char ch) {char ops [] = "+-*/"; for (int I = 0; I <sizeof (ops) /sizeof (char); I ++) {if (ch = ops [I]) return true;} return false ;} //////////////////////////////////////// /// // compares the two operators priority int Precedence (char op1, char op2) {if (op1 = '(') {return-1;} if (op1 = '+' | op1 = '-') {if (op2 = '*' | op2 = '/') {return-1;} else {return 0 ;}} if (op1 = '*' | op1 = '/') {if (op2 = '+' | op2 = '-') {return 1 ;} else {return 0 ;}}} //////////////////////////////////////// /// // The infix expression is converted suffix expression void inFix2PostFix (char * inFix, char * postFix) {int j = 0, len; char c; stack
  
   
St; len = strlen (inFix); for (int I = 0; I <len; I ++) {c = inFix [I]; if (c = '(') st. push (c); else if (c = ') {while (st. top ()! = '(') {PostFix [j ++] = st. top (); st. pop ();} st. pop () ;}else {if (! IsOperator (c) st. push (c); else {while (st. empty () = false & Precedence (st. top (), c)> = 0) {postFix [j ++] = st. top (); st. pop ();} st. push (c) ;}}while (st. empty () = false) {postFix [j ++] = st. top (); st. pop ();} postFix [j] = 0 ;} //////////////////////////////////////// /// // suffix expression value program double postFixEval (char * postFix) {stack
   
    
St; int len = strlen (postFix); char c; for (int I = 0; I <len; I ++) {c = postFix [I]; if (IsOperator (c) = false) {st. push (c-'0');} else {char op1, op2; int val; op1 = st. top (); st. pop (); op2 = st. top (); st. pop (); switch (c) {case '+': val = op1 + op2; break; case '-': val = op2-op1; break; case '*': val = op1 * op2; break; case '/': val = op2/op1; break;} st. push (val) ;}} return st. top () ;}int _ tmain (int argc, _ TCHAR * argv []) {char inFix [100]; char postFix [100]; double val; while (1) {printf ("enter an expression:"); gets_s (inFix); if (strlen (inFix) = 0) continue; printf ("\ n % s = ", inFix); inFix2PostFix (inFix, postFix); printf ("% s =", postFix); val = postFixEval (postFix); printf ("%. 3f \ n ", val);} return 0 ;}
   
  
 

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.