题解 | #计算表达式#

计算表达式

https://www.nowcoder.com/practice/7b18aa6b7cc14f8eaae6b8acdebf890b

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

#define MAX_SIZE 100

typedef struct {
    double data[MAX_SIZE];
    int top;
} Stack;

void initStack(Stack* s) {
    s->top = -1;
}

void push(Stack* s, double value) {
    s->top++;
    s->data[s->top] = value;
}

double pop(Stack* s) {
    double value = s->data[s->top];
    s->top--;
    return value;
}

int isEmpty(Stack* s) {
    return s->top == -1;
}

int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/');
}

int precedence(char operator) {
    switch (operator) {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        default:
            return 0;
    }
}

double applyOperator(double operand1, double operand2, char operator) {
    switch (operator) {
        case '+':
            return operand1 + operand2;
        case '-':
            return operand1 - operand2;
        case '*':
            return operand1 * operand2;
        case '/':
            return operand1 / operand2;
        default:
            return 0;  // Error
    }
}

double evaluateExpression(const char* expression) {
    Stack operandStack, operatorStack;
    initStack(&operandStack);
    initStack(&operatorStack);

    for (int i = 0; expression[i] != '\0'; i++) {
        if (isdigit(expression[i])) {
            double operand = 0;
            while (isdigit(expression[i]) || expression[i] == '.') {
                operand = operand * 10 + (expression[i] - '0');
                i++;
            }
            if (expression[i] == '.') {
                double fraction = 0.1;
                i++;
                while (isdigit(expression[i])) {
                    operand += (expression[i] - '0') * fraction;
                    fraction *= 0.1;
                    i++;
                }
            }
            i--;  
            push(&operandStack, operand);
        } else if (expression[i] == '(') {
            push(&operatorStack, expression[i]);
        } else if (expression[i] == ')') {
            while (!isEmpty(&operatorStack) && operatorStack.data[operatorStack.top] != '(') {
                char op = pop(&operatorStack);
                double operand2 = pop(&operandStack);
                double operand1 = pop(&operandStack);
                push(&operandStack, applyOperator(operand1, operand2, op));
            }
            // Pop '('
            pop(&operatorStack);
        } else if (isOperator(expression[i])) {
            while (!isEmpty(&operatorStack) && precedence(expression[i]) <= precedence(operatorStack.data[operatorStack.top])) {
                char op = pop(&operatorStack);
                double operand2 = pop(&operandStack);
                double operand1 = pop(&operandStack);
                push(&operandStack, applyOperator(operand1, operand2, op));
            }
            push(&operatorStack, expression[i]);
        }
    }

    while (!isEmpty(&operatorStack)) {
        char op = pop(&operatorStack);
        double operand2 = pop(&operandStack);
        double operand1 = pop(&operandStack);
        push(&operandStack, applyOperator(operand1, operand2, op));
    }

    return pop(&operandStack);
}

int main() {
    char expression[MAX_SIZE];
    scanf("%s", expression);
    double result = evaluateExpression(expression);
    printf("%d\n", (int)result);
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
10-04 17:25
门头沟学院 Java
snqing:Java已经饱和了,根本不缺人。随便一个2000工资的都200人起投递
点赞 评论 收藏
分享
11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务