Algorithm Gossip: 中序式轉後序式(前序式)

http://www.bcwhy.com/jdsf/AlgorithmGossip/InFixPostfix.htm


Algorithm Gossip: 中序式轉後序式(前序式)

說明

平常所使用的運算式,主要是將運算元放在運算子的兩旁,例如a+b/d這樣的式子,這稱之為中序(Infix)表示式,對於人類來說,這樣的式子很容易理 解,但由於電腦執行指令時是有順序的,遇到中序表示式時,無法直接進行運算,而必須進一步判斷運算的先後順序,所以必須將中序表示式轉換為另一種表示方 法。

可以將中序表示式轉換為後序(Postfix)表示式,後序表示式又稱之為逆向波蘭表示式(Reverse polish notation),它是由波蘭的數學家盧卡謝維奇提出,例如(a+b)*(c+d)這個式子,表示為後序表示式時是ab+cd+*。

解法

用手算的方式來計算後序式相當的簡單,將運算子兩旁的運算元依先後順序全括號起來,然後將所有的右括號取代為左邊最接近的運算子(從最內層括號開始),最後去掉所有的左括號就可以完成後序表示式,例如:
a+b*d+c/d   =>    ((a+(b*d))+(c/d)) -> bd*+cd/+

如果要用程式來進行中序轉後序,則必須使用堆疊,演算法很簡單,直接敘述的話就是使用迴圈,取出中序式的字元,遇運算元直接輸出,堆疊運算子與左括號, ISP>ICP的話直接輸出堆疊中的運算子,遇右括號輸出堆疊中的運算子至左括號。 

演算法

以下是虛擬碼的運算法,\0表示中序式讀取完畢: 
Procedure Postfix(infix) [
    Loop [
        op = infix(i) 
        case [
            :x = '\0': 
                while (stack not empty) 
                     // output all elements in stack 
                end 
                return 
             :x = '(': 
                 // put it into stack 
             :x is operator: 
                  while (priority(stack[top]) >= 
                         priority(op)) [
                       // out a element from stack 
                  ]
                  // save op into stack 
             :x = ')': 
                   while ( stack(top) != '(' ) [
                       // out a element from stack 
                   ]
                   top = top - 1  // not out '( 
             :else: 
                   // output current op 
        ]
        i++; 
    ]
] 

例如(a+b)*(c+d)這個式子,依演算法的輸出過程如下:
OP STACK OUTPUT
( ( -
a ( a
+ (+ a
b (+ ab
) - ab+
* * ab+
( *( ab+
c *( ab+c
+ *(+ ab+c
d *(+ ab+cd
) * ab+cd+
- - ab+cd+*

如果要將中序式轉為前序式,則在讀取中序式時是由後往前讀取,而左右括號的處理方式相反,其餘不變,但輸出之前必須先置入堆疊,待轉換完成後再將堆疊中的 值由上往下讀出,如此就是前序表示式。 

實作

  • C
#include <stdio.h> 
#include <stdlib.h> 

int postfix(char*); // 中序轉後序 
int priority(char); // 決定運算子優先順序 

int main(void) { 
    char input[80]; 

    printf("輸入中序運算式:"); 
    scanf("%s", input); 
    postfix(input); 

    return 0; 
} 

int postfix(char* infix) { 
    int i = 0, top = 0; 
    char stack[80] = {'\0'}; 
    char op; 

    while(1) { 
        op = infix[i]; 

        switch(op) { 
            case '\0': 
                while(top > 0) { 
                    printf("%c", stack[top]); 
                    top--; 
                } 
                printf("\n"); 
                return 0; 
            // 運算子堆疊 
            case '(': 
                if(top < (sizeof(stack) / sizeof(char))) { 
                    top++; 
                    stack[top] = op; 
                } 
                break; 
            case '+': case '-': case '*': case '/': 
                while(priority(stack[top]) >= priority(op)) { 
                    printf("%c", stack[top]); 
                    top--; 
                } 
                // 存入堆疊 
                if(top < (sizeof(stack) / sizeof(char))) { 
                    top++; 
                    stack[top] = op; 
                } 
                break; 
            // 遇 ) 輸出至 ( 
            case ')': 
                while(stack[top] != '(') { 
                    printf("%c", stack[top]); 
                    top--; 
                } 
                top--;  // 不輸出( 
                break; 
            // 運算元直接輸出 
            default: 
                printf("%c", op); 
                break; 
        } 
        i++; 
    } 
} 

int priority(char op) { 
    int p; 

    switch(op) { 
       case '+': case '-': 
            p = 1; 
            break; 
        case '*': case '/': 
            p = 2; 
            break; 
        default: 
            p = 0; 
            break; 
    } 

    return p; 
} 

  • Java
public class InFix {
    private static int priority(char op) {  
        switch(op) { 
           case '+': case '-': 
                return 1; 
            case '*': case '/': 
                return 2;
            default: 
                return 0;
        }  
    }
    
    public static char[] toPosfix(char[] infix) {
        char[] stack = new char[infix.length]; 
        char[] postfix = new char[infix.length];
        char op; 

        StringBuffer buffer = new StringBuffer();

        int top = 0;
        for(int i = 0; i < infix.length; i++) { 
            op = infix[i]; 
            switch(op) {  
                // 運算子堆疊 
                case '(': 
                    if(top < stack.length) { 
                        top++; 
                        stack[top] = op; 
                    } 
                    break; 
                case '+': case '-': case '*': case '/': 
                    while(priority(stack[top]) >= 
                          priority(op)) { 
                        buffer.append(stack[top]);
                        top--; 
                    } 
                    // 存入堆疊 
                    if(top < stack.length) { 
                        top++; 
                        stack[top] = op; 
                    } 
                    break; 
                // 遇 ) 輸出至 ( 
                case ')': 
                    while(stack[top] != '(') { 
                        buffer.append(stack[top]);
                        top--; 
                    } 
                    top--;  // 不輸出( 
                    break; 
                // 運算元直接輸出 
                default: 
                    buffer.append(op);
                    break; 
            } 
        } 
        
        while(top > 0) { 
            buffer.append(stack[top]);
            top--; 
        }
        
        return buffer.toString().toCharArray();
    }
    
    public static char[] toPrefix(char[] infix) {
        char[] stack = new char[infix.length];
        char op; 

        StringBuffer buffer = new StringBuffer();
        
        int top = 0;
        for(int i = infix.length - 1; i >= 0; i--) { 
            op = infix[i]; 
            switch(op) {  
                // 運算子堆疊 
                case ')': 
                    if(top < stack.length) { 
                        top++; 
                        stack[top] = op; 
                    } 
                    break; 
                case '+': case '-': case '*': case '/': 
                    while(priority(stack[top]) >= 
                          priority(op)) { 
                        buffer.append(stack[top]);
                        top--; 
                    } 
                    // 存入堆疊 
                    if(top < stack.length) { 
                        top++; 
                        stack[top] = op; 
                    } 
                    break; 
                // 遇 ( 輸出至 ) 
                case '(': 
                    while(stack[top] != ')') { 
                        buffer.append(stack[top]);
                        top--; 
                    } 
                    top--;  // 不輸出) 
                    break; 
                // 運算元直接輸出 
                default: 
                    buffer.append(op); 
                    break; 
            } 
        } 
        
        while(top > 0) { 
            buffer.append(stack[top]);
            top--; 
        } 
        
        return buffer.reverse().toString().toCharArray();
    }
    
    public static void main(String[] args) {
        String infix = "(a+b)*(c+d)";
        
        System.out.println(
                    InFix.toPosfix(infix.toCharArray()));
        System.out.println(
                    InFix.toPrefix(infix.toCharArray()));
    }
} 

posted @ 2012-11-16 16:19  好好学习,天天进步  阅读(331)  评论(0编辑  收藏  举报