2012年2月2日 星期四

[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; 

    } 

}

 



 




沒有留言:

張貼留言