Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
4.6 kB
3
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

typedef long long LL;

LL MAX_LEVEL = 15000;

int priority(char c){//判斷各種二元運算子的優先級
    if(c == '/' || c == '*' || c == '%'){
        return 2;
    }
    else if(c == '+' || c == '-'){
        return 1;
    }
    else{
        return 0;
    }
}

void infixToPostfix(char string[]){
    char result[MAX_LEVEL];//轉換成後綴的形式 同時也會用來計算
    int resultIndex = 0;//用來控制這個偽stack的index
    int len = strlen(string);
    char stack[MAX_LEVEL];//用來裝標點符號的stack
    int stackIndex = -1;

    for(int i = 0; i < len; i++){
        if(isdigit(string[i]) != 0){
            if(isdigit(string[i+1]) != 0 && (i + 1 < len)){
                while(isdigit(string[i]) != 0){
                    result[resultIndex] = string[i];//如果是數字 就先全部都先裝到輸出陣列當中
                    resultIndex++;
                    i++;
                }
                i--;
                result[resultIndex] = ' ';
                resultIndex++;
            }
            else{
                result[resultIndex] = string[i];
                resultIndex++;
                result[resultIndex] = ' ';
                resultIndex++;
            }
        }
        else{
            if(string[i] == '('){
                stackIndex++;
                stack[stackIndex] = string[i];
            }
            else if(string[i] == ')'){
                while(stackIndex != -1 && stack[stackIndex] != '('){
                    result[resultIndex] = stack[stackIndex];
                    stackIndex--;
                    resultIndex++;
                }
                stackIndex--;
            }
            else{
                while(stackIndex != -1 && (priority(stack[stackIndex]) >= priority(string[i]))){
                    result[resultIndex] = stack[stackIndex];
                    stackIndex--;
                    resultIndex++;
                }
                stackIndex++;
                stack[stackIndex] = string[i];
            }
        }
    }
    while(stackIndex != -1){
        result[resultIndex] = stack[stackIndex];
        resultIndex++;
        stackIndex--;
    }
    for(int i = 0; i < resultIndex; i++){
        if(result[i] != ' ')
        printf("%c", result[i]);
    }

    printf("=");
    
    //postfix-evaluation

    LL calculateStack[MAX_LEVEL];
    int cStackIndex = -1;
    
    for(int i = 0; i < resultIndex; i++){
        if(isdigit(result[i]) != 0){
                char aString[MAX_LEVEL];//先切字串再轉換成數字
                int aStringIndex = 0;
                while(1){
                    aString[aStringIndex] = result[i];
                    i++;
                    aStringIndex++;
                    if(result[i] == ' '){
                        aString[aStringIndex] = '\0';
                        break;
                    }
                }
                cStackIndex++;
                calculateStack[cStackIndex] = atoi(aString);
                //printf("this is the stack box:%d\n", calculateStack[cStackIndex]);
        }
        else{
            LL number1 = calculateStack[cStackIndex];
            //printf("N1 %d\n", number1);
            cStackIndex--;
            
            LL number2 = calculateStack[cStackIndex];
            //printf("N2 %d\n", number2);
            if(result[i] == '+'){
                calculateStack[cStackIndex] = number1+number2;
            }
            else if(result[i] == '-'){
                calculateStack[cStackIndex] = number2-number1;
            }
            else if(result[i] == '*'){
                calculateStack[cStackIndex] = number1*number2;
            }
            else if(result[i] == '/'){
                calculateStack[cStackIndex] = number2/number1;
            }
            else if(result[i] == '%'){
                calculateStack[cStackIndex] = number2%number1;
            }
        }
        
    }
    
    printf("%lld\n", calculateStack[cStackIndex]);

    return;
}

int main(){
    
        char myString1[MAX_LEVEL];
        char myString2[MAX_LEVEL];
        char myString3[MAX_LEVEL];
        scanf("%s", myString1);
        scanf("%s", myString2);
        scanf("%s", myString3);
        infixToPostfix(myString1);
        infixToPostfix(myString2);
        infixToPostfix(myString3);
    

    return 0;
}
Leave a Comment