Top Banner
Infix to Postfix 수식 전환 예제 Infix 수식 표현 Postfix 수식 표현 12 + 5 * 3 / 4 12 5 3 * 4 / + ( 12 + 5 ) * ( 3 / 4 ) 12 5 + 3 4 / * ( 23 – 7 + 5 ) / 3 + 6 * 22 23 7 – 5 + 3 / 6 22 * + ( ( 23 – 7 + 5 ) / 3 + 6 ) * 22 23 7 – 5 + 3 / 6 + 22 *
34

Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Apr 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to Postfix 수식전환예제수식전환예제

Infix 수식표현 Postfix수식표현

12 + 5 * 3 / 4 12 5 3 * 4 / +

( 12 + 5 ) * ( 3 / 4 ) 12 5 + 3 4 / *

( 23 – 7 + 5 ) / 3 + 6 * 22 23 7 – 5 + 3 / 6 22 * +

( ( 23 – 7 + 5 ) / 3 + 6 ) * 22 23 7 – 5 + 3 / 6 + 22 *

Page 2: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

데이터구조데이터구

Token 주어진수식은숫자, 연산자 (+, -, *, /, %) , 괄호(‘(‘, ‘)’) 들의조합으로이루어지며 스페이스문자 (‘ ‘)에의해서구분이된다조합으로이루어지며, 스페이스문자 ( )에의해서구분이된다. 이렇게숫자, 연산자, 괄호를하나의 token으로정의한다.

infixVectorInfix 수식을가지고있는 token의리스트

postfixVectorPostfix 수식으로변환된 token의리스트

operatorStack연산자를저장하는스택

Page 3: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to Postfix 알고리즘while (infixVector is not empty) {

t th t t k i i fi V tx get the next token in infixVector;if (x is an operand) {

Add x to the end of the postfixVector.}else if (x is ‘(‘ ) {else if (x is ( ) {

Push x in the operatorStack. }else if (x is ‘)’) {

y pop a token in operatorStack;y p p p ;while (y is not ‘(’) {

Add y to the end of the postfixVector;y pop a token in operatorStack;

}If there is no ‘(‘ in the operatorStack, that expression is invalid.

}else if (x is an operator) {

y a token in the top of operatorStack;hil ( t St k i t t dwhile (operatorStack is not empty and

y is not ‘(‘ andy is an operator of higher or equal precedence than that of x) {

y pop the token in operatorStack;Add y to the end of the postfixVector;Add y to the end of the postfixVector;

}Push x in the operatorStack.

}}}while (operatorStack is not empty) {

Pop a token in the operatorStack and Add it to the end of the postfixVector.}

Page 4: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

( a + b c ) * d ( e + f )

postfi Vector

( a + b - c ) d – ( e + f )

postfixVector

operatorStack

Page 5: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

a + b c ) * d ( e + f )

postfi Vector

a + b - c ) d – ( e + f )

postfixVector

(

operatorStack

Page 6: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

+ b c ) * d ( e + f )

postfi Vector

+ b - c ) d – ( e + f )

postfixVector

a

(

operatorStack

Page 7: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

b c ) * d ( e + f )

postfi Vector

b - c ) d – ( e + f )

postfixVector

a

+

(

operatorStack

Page 8: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

c ) * d ( e + f )

postfi Vector

- c ) d – ( e + f )

postfixVector

a b

+

(

operatorStack

Page 9: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

c ) * d ( e + f )

postfi Vector

c ) d – ( e + f )

postfixVector

a b +

-

(

operatorStack

Page 10: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

) * d ( e + f )

postfi Vector

) d – ( e + f )

postfixVector

a b + c

-

(

operatorStack

Page 11: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

* d ( e + f )

postfi Vector

d – ( e + f )

postfixVector

a b + c -

operatorStack

Page 12: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

d ( e + f )

postfi Vector

d – ( e + f )

postfixVector

a b + c -

*

operatorStack

Page 13: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

( e + f )

postfi Vector

– ( e + f )

postfixVector

a b + c - d

*

operatorStack

Page 14: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

( e + f )

postfi Vector

( e + f )

postfixVector

a b + c – d *

-

operatorStack

Page 15: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

e + f )

postfi Vector

e + f )

postfixVector

a b + c – d *

(

-

(

operatorStack

Page 16: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

+ f )

postfi Vector

+ f )

postfixVector

a b + c – d * e

(

-

(

operatorStack

Page 17: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

f )

postfi Vector

f )

postfixVector

a b + c – d * e+

(

+

-

(

operatorStack

Page 18: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

)

postfi Vector

)

postfixVector

a b + c – d * e f+

(

+

-

(

operatorStack

Page 19: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

postfi VectorpostfixVector

a b + c – d * e f +

-

operatorStack

Page 20: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Infix to postfix conversionp

infixVector

postfi VectorpostfixVector

a b + c – d * e f + -

operatorStack

Page 21: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Postfix 수식계산수식계산

필 한데이터필요한데이터구조postfixVector :

postfix 수식표현을간직하는리스트operandStackp피연산자를보관하는스택

Page 22: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Postfix 수식계산알고리즘수식계산알 리즘while (postfixVector is not empty) {

x get the next token in postfixVector;x get the next token in postfixVector;if (x is an operand) {

Push x in the operandStack.}else if (x is an operator) {

y pop an operand in operandStack;y pop an operand in operandStack;z pop an operand in operandStack;k Evaluate the value of the operator x for z and y;Push k in the operandStack.

} }}result pop an operand in operandStack;

Page 23: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

a b + c – d * e f + -

operandStack

Page 24: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

b + c – d * e f + -

a

operandStack

Page 25: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

+ c – d * e f + -

b

a

operandStack

Page 26: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

c – d * e f + -

(a+b)

operandStack

Page 27: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

- d * e f + -

c

(a+b)

operandStack

Page 28: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

d * e f + -

((a+b)-c)

operandStack

Page 29: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

* e f + -

d

((a+b)-c)

operandStack

Page 30: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

e f + -

(((a+b)-c)*d)

operandStack

Page 31: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

f + -

e

(((a+b)-c)*d)

operandStack

Page 32: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

+ -f

e

f

(((a+b)-c)*d)

operandStack

Page 33: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

-

(e+f)

(((a+b)-c)*d)

operandStack

Page 34: Infix to Postfix 수식전환예제rts.gnu.ac.kr/class/2008spring/java_prog/Simple...Postfix 수식계산 필한필요한데이터구조 postfixVector : postfix 수식표현을간직하는리스트

Evaluating postfixg p

postfi VectorpostfixVector

((((a+b)-c)*d)-(e+f))

operandStack