Infix to Postfix Conversion

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_SIZE 1000

// Function to return precedence of operators
// *, / have higher precedence (2) than +, - (1)
int get_precedence(char op) {
    if (op == '*' || op == '/') return 2;
    if (op == '+' || op == '-') return 1;
    return 0;
}

// Function to perform arithmetic calculations
double apply_op(double a, double b, char op) {
    switch(op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return a / b;
    }
    return 0;
}

int main() {
    char expression[MAX_SIZE];
    
    // Read the single line input string
    if (scanf("%s", expression) != 1) return 0;

    // Arrays to store the final postfix string and the operator stack
    char postfix[MAX_SIZE * 2]; 
    int p_idx = 0;
    
    char op_stack[MAX_SIZE];
    int op_top = -1;

    // --- Step 1: Infix to Postfix Conversion ---
    for (int i = 0; expression[i] != '\0'; i++) {
        char ch = expression[i];

        if (isdigit(ch)) {
            // If operand (0-9), add directly to output
            postfix[p_idx++] = ch;
            postfix[p_idx++] = ' ';
        } 
        else if (ch == '(') {
            // Push opening brace to stack
            op_stack[++op_top] = ch;
        } 
        else if (ch == ')') {
            // Pop until matching '(' is found
            while (op_top != -1 && op_stack[op_top] != '(') {
                postfix[p_idx++] = op_stack[op_top--];
                postfix[p_idx++] = ' ';
            }
            op_top--; // Discard the '('
        } 
        else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
            // If operator, pop elements with higher/equal precedence from stack to output
            while (op_top != -1 && get_precedence(op_stack[op_top]) >= get_precedence(ch)) {
                postfix[p_idx++] = op_stack[op_top--];
                postfix[p_idx++] = ' ';
            }
            op_stack[++op_top] = ch;
        }
    }

    // Pop any remaining operators in the stack
    while (op_top != -1) {
        postfix[p_idx++] = op_stack[op_top--];
        postfix[p_idx++] = ' ';
    }
    postfix[p_idx] = '\0'; // Null-terminate the string

    // --- Step 2: Evaluate Postfix Expression ---
    double val_stack[MAX_SIZE];
    int val_top = -1;

    for (int i = 0; postfix[i] != '\0'; i++) {
        char ch = postfix[i];

        if (isdigit(ch)) {
            // Convert char to double and push
            val_stack[++val_top] = (double)(ch - '0');
        } 
        else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
            // Pop two top numbers
            double b = val_stack[val_top--];
            double a = val_stack[val_top--];
            // Calculate and push result back
            val_stack[++val_top] = apply_op(a, b, ch);
        }
        // Note: Spaces are ignored automatically
    }

    // --- Step 3: Print Output ---
    // Line 1: Result (2 decimal places)
    printf("%.2f\n", val_stack[val_top]);
    
    // Line 2: Postfix string (already contains trailing space)
    printf("%s\n", postfix);

    return 0;
}

 

posted @ 2025-12-24 10:01  我不是青山  阅读(2)  评论(0)    收藏  举报