You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于C语言的RPN程序运行异常求助:小值正常大值崩溃

Fixing Your RPN Program Crash with Large Numerical Inputs

Let's walk through the key issues in your code that are causing crashes with inputs like 100+2*100/50, and fix them one by one.

Key Issues Identified

  • Array Overflow: Your input buffer is too small and you're reading more characters than it can hold, leading to stack corruption.
  • No Multi-digit Number Handling: Your code treats each digit as a separate operand, which breaks large numbers and causes incorrect calculations (and crashes in some cases).
  • Input Array Reuse: You're overwriting the original infix expression array with the postfix expression, which can cause memory issues when the postfix expression needs more space.

Step-by-Step Fixes

1. Fix Array Overflow

In main(), you declared char exp[10]; but used scanf("%14s", exp); — this writes 14 characters into a 10-byte buffer, causing immediate stack corruption. Resize the buffer and limit input to its capacity:

// Replace this
char exp[10];
scanf("%14s", exp);

// With this
char exp[100];
scanf("%99s", exp); // Read up to 99 chars to leave room for the null terminator

2. Add Multi-digit Number Support

Your current code processes each digit individually. We need to capture consecutive digits as a single number, and separate operands with spaces in the postfix expression so the evaluator can parse them correctly.

Modify infixToPostfix for Multi-digit Numbers

Rewrite the function to use a separate buffer for the postfix expression and capture full numbers:

char* infixToPostfix(char* exp) {
    int i, k = 0;
    int len = strlen(exp);
    // Allocate enough space for postfix expression (worst case: all operands + operators + spaces)
    char* postfix = (char*)malloc((2 * len + 1) * sizeof(char));
    struct Stack* stack = createStack(len);
    if (!stack || !postfix) return NULL;

    for (i = 0; exp[i]; ++i) {
        // Skip any whitespace (optional, adds input flexibility)
        if (isspace(exp[i])) continue;

        // Handle multi-digit numbers
        if (isdigit(exp[i])) {
            while (isdigit(exp[i])) {
                postfix[k++] = exp[i++];
            }
            postfix[k++] = ' '; // Add space to separate operands
            i--; // Adjust index since we incremented past the last digit
        }
        else if (exp[i] == '(') {
            push(stack, exp[i]);
        }
        else if (exp[i] == ')') {
            while (!isEmpty(stack) && peek(stack) != '(')
                postfix[k++] = pop(stack);
            if (!isEmpty(stack) && peek(stack) != '(') {
                free(postfix);
                return NULL; // Invalid expression
            }
            else
                pop(stack);
        }
        else { // Operator
            while (!isEmpty(stack) && Prec(exp[i]) <= Prec(peek(stack)))
                postfix[k++] = pop(stack);
            push(stack, exp[i]);
        }
    }

    // Pop remaining operators from stack
    while (!isEmpty(stack))
        postfix[k++] = pop(stack);

    postfix[k] = '\0';
    return postfix;
}

Modify evaluatePostfix to Parse Multi-digit Numbers

Update the evaluator to read full numbers instead of single digits:

int evaluatePostfix(char* exp) {
    struct Stack* stack = createStack(strlen(exp));
    int i, num = 0;
    if (!stack) return -1;

    for (i = 0; exp[i]; ++i) {
        // Skip spaces
        if (isspace(exp[i])) continue;

        // Build multi-digit number
        if (isdigit(exp[i])) {
            num = 0;
            while (isdigit(exp[i])) {
                num = num * 10 + (exp[i] - '0');
                i++;
            }
            push(stack, num);
            i--; // Adjust index
        }
        else { // Operator
            int val1 = pop(stack);
            int val2 = pop(stack);
            switch (exp[i]) {
                case '+': push(stack, val2 + val1); break;
                case '-': push(stack, val2 - val1); break;
                case '*': push(stack, val2 * val1); break;
                case '/': 
                    if (val1 == 0) {
                        printf("Error: Division by zero\n");
                        return -1;
                    }
                    push(stack, val2 / val1); 
                    break;
            }
        }
    }
    return pop(stack);
}

3. Update main() for New Functionality

Adjust main() to handle the dynamically allocated postfix array and clean up memory to avoid leaks:

int main() {
    char exp[100];
    printf("Enter infix expression: ");
    scanf("%99s", exp);
    printf("Expression:\n%s\n", exp);

    char* postfix = infixToPostfix(exp);
    if (!postfix) {
        printf("Error converting to postfix\n");
        return 1;
    }

    printf("Reverse Polish Notation:\n%s\n", postfix);
    int result = evaluatePostfix(postfix);
    printf("Result:\n%d\n", result);

    // Clean up allocated memory
    free(postfix);
    return 0;
}

Additional Improvements

  • Added a division-by-zero check to make the program more robust.
  • The postfix expression now uses spaces to separate operands, making it easier to read and parse.
  • Dynamic allocation for the postfix buffer avoids overflow issues with longer expressions.

Testing with your input 100+2*100/50 will now output:

Expression:
100+2*100/50
Reverse Polish Notation:
100 2 100 * 50 / + 
Result:
104

内容的提问来源于stack exchange,提问作者M. Marayef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:47:31