Ch3-3-InfixPostfix

# Ch3-3-InfixPostfix - DataStructureStackII SangYongHan...

This preview shows pages 1–8. Sign up to view the full content.

Data Structure – Stack II                 Sang Yong Han            http://ec.cse.cau.ac.kr/ Chung-Ang University Spring 2011 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Applications Infix to Postfix conversion     [Evaluation of Expressions]
X = a / b - c + d * e - a * c a = 4, b = c = 2, d = e = 3 Interpretation 1:  ((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1 Interpretation 2: (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666 How to generate the machine instructions  corresponding to a given expression?       precedence rule + associative rule Evaluation of Expressions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Infix Postfix 2+3*4 a*b+5 (1+2)*7 a*b/c (a/(b-c+d))*(e-a)*c a/b-c+d*e-a*c 234*+ ab*5+ 12+7* ab*c/ abc-d+/ea-*c* ab/c-de*ac*- user compiler Postfix: no parentheses, no precedence
Token Stack [0] [1] [2] Top 6 2 / 3 - 4 2 * + 6 6 2 6/2 6/2 3 6/2-3 6/2-3 4 6/2-3 4 2 6/2-3 4*2 6/2-3+4*2 0 1 0 1 0 1 2 1 0

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
#define MAX_STACK_SIZE 100 /* maximum stack size */ #define MAX_EXPR_SIZE 100 /* max size of expression */ typedef enum{1paran, rparen, plus, minus, times, divide,                           mod, eos, operand} precedence; int stack[MAX_STACK_SIZE]; /* global stack */ char expr[MAX_EXPR_SIZE]; /* input string */ Assumptions: operators: +, -, *, /, % operands: single digit integer Infix to Postfix
int eval(void) { /* evaluate a postfix expression, expr, maintained as a      global variable, ‘\0’ is the the end of the expression.     The stack and top of the stack are global variables.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/13/2011 for the course DATA STRUC 101 taught by Professor Hansanyong during the Spring '11 term at 한동대학교.

### Page1 / 17

Ch3-3-InfixPostfix - DataStructureStackII SangYongHan...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online