Ch3-3-InfixPostfix - DataStructureStackII SangYongHan...

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

View Full Document Right Arrow Icon
Data Structure – Stack II                 Sang Yong Han            http://ec.cse.cau.ac.kr/ Chung-Ang University Spring 2011 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Applications Infix to Postfix conversion     [Evaluation of Expressions]
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
#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
Background image of page 6
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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online