[C/C++基礎]- 純C中序轉後序與純C後序求值

[C/C++基礎]- 純C中序轉後序與純C後序求值

[C/C++基礎]- C中序轉後序C後序求值

 

本篇要分享純C中序轉後序C後序求值,有興趣的(C/P)同好,歡迎來(C/P)一下,哈哈 ^ ^

 

C中序轉後序_程式碼

#include <cstdlib>
#include <iostream>
#include <cstdio> 
usingnamespace std;
int postfix(char*); // 中序轉後序
int priority(char); // 決定運算子優先順序
int main(int argc, char *argv[])
{
    char input[80];
    printf("輸入中序運算式:");
    scanf("%s", input);
    postfix(input); 
    system("PAUSE");
    return EXIT_SUCCESS;
}
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; 
}

 

 

C後序求值_程式碼

#include <cstdlib>
#include <cstdio> 
#include <iostream>
void evalPf(char*); 
double cal(double, char, double); 
usingnamespace std;
 
int main(int argc, char *argv[])
{
    char input[80]; 
    printf("輸入後序式:"); 
    scanf("%s", input); 
    evalPf(input); 
 
    system("PAUSE");
    return EXIT_SUCCESS;
}
void evalPf(char* postfix) { 
    double stack[80] = {0.0}; 
    char temp[2]; 
    char token; 
    int top = 0, i = 0; 
 
    temp[1] = '\0'; 
 
    while(1) { 
        token = postfix[i]; 
        switch(token) { 
            case '\0': 
                printf("ans = %f\n", stack[top]); 
                return; 
            case '+': case '-': case '*': case '/': 
                stack[top-1] = 
                       cal(stack[top-1], token, stack[top]); 
                top--; 
                break; 
            default: 
                if(top < sizeof(stack) / sizeof(float)) { 
                    temp[0] = postfix[i]; 
                    top++; 
                    stack[top] = atof(temp); 
                } 
                break; 
        } 
        i++; 
    } 
}
double cal(double p1, char op, double p2) { 
    switch(op) { 
        case '+': 
            return p1 + p2; 
        case '-': 
            return p1 - p2; 
        case '*': 
            return p1 * p2; 
        case '/': 
            return p1 / p2; 
    } 
}

 

 


One thought on “[C/C++基礎]- 純C中序轉後序與純C後序求值

  1. 在找資料時發現您的blog,受益良多!
    有參考您的code實作一下,發現如果是以鍵入字元的方式來做換與計算,您code簡單易懂,但我實作起來似乎只能做個位數的運算,應該沒錯吧?
    不知是否有能做多位數的運算方法呢?
    謝謝!
    版主回覆:(07/23/2012 06:13:54 AM)
    我沒有繼續往下實作
    因為我在Windows 和Linux都有發現可以直接使用script方法
    所以舊沒有繼續執著於自己開發
    有需要可以提供給你

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *