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 *
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 *
데이터구조데이터구
Token 주어진수식은숫자, 연산자 (+, -, *, /, %) , 괄호(‘(‘, ‘)’) 들의조합으로이루어지며 스페이스문자 (‘ ‘)에의해서구분이된다조합으로이루어지며, 스페이스문자 ( )에의해서구분이된다. 이렇게숫자, 연산자, 괄호를하나의 token으로정의한다.
infixVectorInfix 수식을가지고있는 token의리스트
postfixVectorPostfix 수식으로변환된 token의리스트
operatorStack연산자를저장하는스택
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.}
Infix to postfix conversionp
infixVector
( a + b c ) * d ( e + f )
postfi Vector
( a + b - c ) d – ( e + f )
postfixVector
operatorStack
Infix to postfix conversionp
infixVector
a + b c ) * d ( e + f )
postfi Vector
a + b - c ) d – ( e + f )
postfixVector
(
operatorStack
Infix to postfix conversionp
infixVector
+ b c ) * d ( e + f )
postfi Vector
+ b - c ) d – ( e + f )
postfixVector
a
(
operatorStack
Infix to postfix conversionp
infixVector
b c ) * d ( e + f )
postfi Vector
b - c ) d – ( e + f )
postfixVector
a
+
(
operatorStack
Infix to postfix conversionp
infixVector
c ) * d ( e + f )
postfi Vector
- c ) d – ( e + f )
postfixVector
a b
+
(
operatorStack
Infix to postfix conversionp
infixVector
c ) * d ( e + f )
postfi Vector
c ) d – ( e + f )
postfixVector
a b +
-
(
operatorStack
Infix to postfix conversionp
infixVector
) * d ( e + f )
postfi Vector
) d – ( e + f )
postfixVector
a b + c
-
(
operatorStack
Infix to postfix conversionp
infixVector
* d ( e + f )
postfi Vector
d – ( e + f )
postfixVector
a b + c -
operatorStack
Infix to postfix conversionp
infixVector
d ( e + f )
postfi Vector
d – ( e + f )
postfixVector
a b + c -
*
operatorStack
Infix to postfix conversionp
infixVector
( e + f )
postfi Vector
– ( e + f )
postfixVector
a b + c - d
*
operatorStack
Infix to postfix conversionp
infixVector
( e + f )
postfi Vector
( e + f )
postfixVector
a b + c – d *
-
operatorStack
Infix to postfix conversionp
infixVector
e + f )
postfi Vector
e + f )
postfixVector
a b + c – d *
(
-
(
operatorStack
Infix to postfix conversionp
infixVector
+ f )
postfi Vector
+ f )
postfixVector
a b + c – d * e
(
-
(
operatorStack
Infix to postfix conversionp
infixVector
f )
postfi Vector
f )
postfixVector
a b + c – d * e+
(
+
-
(
operatorStack
Infix to postfix conversionp
infixVector
)
postfi Vector
)
postfixVector
a b + c – d * e f+
(
+
-
(
operatorStack
Infix to postfix conversionp
infixVector
postfi VectorpostfixVector
a b + c – d * e f +
-
operatorStack
Infix to postfix conversionp
infixVector
postfi VectorpostfixVector
a b + c – d * e f + -
operatorStack
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;