C语言实现中缀表达式计算器

本程序使用C语言实现了一个中缀表达式计算器,支持加减乘除、幂运算和括号。表达式操作数为正整数,运算结果为整数,支持截尾取整和除数为0的INVALID输出。

代码实现

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

#define MAX_STACK_SIZE 1000

// 定义运算符的优先级
int getPriority(char operator) {
    if (operator == '+' || operator == '-')
        return 1;
    else if (operator == '*' || operator == '/')
        return 2;
    else if (operator == '^')
        return 3;
    else
        return 0;
}

// 计算两个操作数的运算结果
int calculate(int operand1, int operand2, char operator) {
    switch (operator) {
        case '+':
            return operand1 + operand2;
        case '-':
            return operand1 - operand2;
        case '*':
            return operand1 * operand2;
        case '/':
            if (operand2 == 0) {
                return -1; // 除数为0,返回INVALID
            }
            return operand1 / operand2;
        case '^':
            return (int) pow(operand1, operand2);
        default:
            return 0;
    }
}

// 判断是否为操作符
bool isOperator(char character) {
    return (character == '+' || character == '-' || character == '*' || character == '/' || character == '^');
}

// 判断是否为数字
bool isDigit(char character) {
    return isdigit(character);
}

// 执行中缀表达式的计算
int evaluateExpression(char *expression) {
    int operands[MAX_STACK_SIZE];
    char operators[MAX_STACK_SIZE];
    int operandIndex = -1;
    int operatorIndex = -1;

    for (int i = 0; i < strlen(expression); i++) {
        char character = expression[i];

        if (character == ' ') {
            continue;
        } else if (isDigit(character)) {
            int operand = 0;
            while (i < strlen(expression) && isDigit(expression[i])) {
                operand = operand * 10 + (expression[i] - '0');
                i++;
            }
            operands[++operandIndex] = operand;
            i--;
        } else if (character == '(') {
            operators[++operatorIndex] = character;
        } else if (character == ')') {
            while (operatorIndex >= 0 && operators[operatorIndex] != '(') {
                int operand2 = operands[operandIndex--];
                int operand1 = operands[operandIndex--];
                char operator = operators[operatorIndex--];
                int result = calculate(operand1, operand2, operator);
                if (result == -1) {
                    return -1; // 除数为0,返回INVALID
                }
                operands[++operandIndex] = result;
            }
            operatorIndex--;  // 弹出左括号
        } else if (isOperator(character)) {
            while (operatorIndex >= 0 && getPriority(operators[operatorIndex]) >= getPriority(character)) {
                int operand2 = operands[operandIndex--];
                int operand1 = operands[operandIndex--];
                char operator = operators[operatorIndex--];
                int result = calculate(operand1, operand2, operator);
                if (result == -1) {
                    return -1; // 除数为0,返回INVALID
                }
                operands[++operandIndex] = result;
            }
            operators[++operatorIndex] = character;
        }
    }

    while (operatorIndex >= 0) {
        int operand2 = operands[operandIndex--];
        int operand1 = operands[operandIndex--];
        char operator = operators[operatorIndex--];
        int result = calculate(operand1, operand2, operator);
        if (result == -1) {
            return -1; // 除数为0,返回INVALID
        }
        operands[++operandIndex] = result;
    }

    return operands[0];
}

int main() {
    char expression[1000];

    while (fgets(expression, sizeof(expression), stdin)) {
        if (expression[0] == '
') {
            break;
        }

        expression[strcspn(expression, "\n")] = '\0';
        int result = evaluateExpression(expression);
        if (result == -1) {
            printf("INVALID\n");
        } else {
            printf("%d\n", result);
        }
    }

    return 0;
}

代码解析

  1. 定义运算符优先级: 函数 getPriority 用于获取不同运算符的优先级,例如乘除运算优先级高于加减运算。
  2. 计算两个操作数的运算结果: 函数 calculate 用于根据运算符计算两个操作数的运算结果,并考虑除数为0的情况,返回 -1 表示INVALID。
  3. 判断是否为操作符或数字: 函数 isOperatorisDigit 用于判断字符是否为操作符或数字。
  4. 执行中缀表达式的计算: 函数 evaluateExpression 使用两个栈分别存放操作数和运算符,遍历表达式,根据字符类型进行相应操作,并最终计算出表达式的值。
  5. 主函数: 主函数 main 读取输入的表达式,调用 evaluateExpression 计算结果,并根据结果输出相应的数值或INVALID。

运行方式

编译并运行该代码,输入中缀表达式,即可得到计算结果。例如:

输入:5+(10*2)-6
输出:19

输入:8*(999+1)
输出:8000

输入:1+5/(1-1)
输出:INVALID

输入:7*2^3
输出:56

注意点

  • 输入的表达式必须用空格隔开数字和运算符。
  • 表达式中的数字必须为正整数。
  • 幂运算的指数必须为非负数,底数必须不为0。
  • 除法运算结果为小数时,进行截尾取整。
  • 除数为0时,输出INVALID。

总结

这段代码展示了如何使用C语言实现一个中缀表达式计算器,并包含了对除数为0的处理、幂运算和截尾取整的实现。希望本代码可以帮助你更好地理解中缀表达式计算的原理和实现。


原文地址: https://www.cveoy.top/t/topic/iyS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录