C语言实现中缀表达式求值:利用堆栈解析算术表达式
C语言实现中缀表达式求值:利用堆栈解析算术表达式
本文将介绍如何使用C语言实现中缀表达式的求值。中缀表达式是人类习惯的数学表达式书写方式,例如 '1 + 2 * 3'。为了让计算机理解并计算这样的表达式,我们需要使用堆栈来处理运算符的优先级和括号。
以下是完整的C语言代码:c#include <stdio.h>#include <stdlib.h>
// 定义运算符优先级int getPriority(char op) { switch(op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; }}
// 执行运算int calculate(int num1, int num2, char op) { switch(op) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': return num1 / num2; default: return 0; }}
int main() { char expression[100]; // 保存输入的表达式 int operandStack[100]; // 操作数栈 char operatorStack[100]; // 运算符栈 int operandTop = -1; // 操作数栈顶指针 int operatorTop = -1; // 运算符栈顶指针 printf('请输入中缀表达式:'); fgets(expression, sizeof(expression), stdin); // 遍历表达式 int i = 0; while (expression[i] != '�') { char current = expression[i]; // 如果是数字,将其转换为整数并入操作数栈 if (current >= '0' && current <= '9') { int num = 0; while (current >= '0' && current <= '9') { num = num * 10 + (current - '0'); i++; current = expression[i]; } operandTop++; operandStack[operandTop] = num; } // 如果是运算符 else if (current == '+' || current == '-' || current == '*' || current == '/') { // 如果运算符栈为空或当前运算符优先级大于运算符栈顶元素优先级,则将当前运算符入栈 if (operatorTop == -1 || getPriority(current) > getPriority(operatorStack[operatorTop])) { operatorTop++; operatorStack[operatorTop] = current; } // 否则,从栈顶取出一个运算符和两个操作数进行运算,并将结果入操作数栈,重复该过程直到当前运算符可以入栈 else { while (operatorTop != -1 && getPriority(current) <= getPriority(operatorStack[operatorTop])) { char op = operatorStack[operatorTop]; operatorTop--; int num2 = operandStack[operandTop]; operandTop--; int num1 = operandStack[operandTop]; operandTop--; int result = calculate(num1, num2, op); operandTop++; operandStack[operandTop] = result; } operatorTop++; operatorStack[operatorTop] = current; } } // 如果是左括号,直接入栈 else if (current == '(') { operatorTop++; operatorStack[operatorTop] = current; } // 如果是右括号,从栈顶取出一个运算符和两个操作数进行运算,并将结果入操作数栈,重复该过程直到遇到左括号 else if (current == ')') { while (operatorStack[operatorTop] != '(') { char op = operatorStack[operatorTop]; operatorTop--; int num2 = operandStack[operandTop]; operandTop--; int num1 = operandStack[operandTop]; operandTop--; int result = calculate(num1, num2, op); operandTop++; operandStack[operandTop] = result; } operatorTop--; // 弹出左括号 } i++; } // 处理剩余的运算符 while (operatorTop != -1) { char op = operatorStack[operatorTop]; operatorTop--; int num2 = operandStack[operandTop]; operandTop--; int num1 = operandStack[operandTop]; operandTop--; int result = calculate(num1, num2, op); operandTop++; operandStack[operandTop] = result; } // 输出结果 printf('表达式的求值结果为:%d ', operandStack[operandTop]); return 0;}
代码解释:
getPriority(char op)函数:返回运算符的优先级,'+' 和 '-' 的优先级为 1,'*' 和 '/' 的优先级为 2。2.calculate(int num1, int num2, char op)函数:根据运算符执行相应的算术运算。3.main()函数: - 从键盘读取中缀表达式。 - 使用两个堆栈:operandStack存储操作数,operatorStack存储运算符。 - 遍历表达式,根据字符类型进行处理: - 数字:转换为整数并压入操作数栈。 - 运算符: - 如果运算符栈为空或当前运算符优先级高于栈顶运算符,则将当前运算符压入运算符栈。 - 否则,从运算符栈顶取出运算符和操作数进行计算,将结果压入操作数栈,直到当前运算符可以入栈。 - 左括号:直接压入运算符栈。 - 右括号:从运算符栈顶取出运算符和操作数进行计算,直到遇到左括号。 - 处理完表达式后,将剩余的运算符和操作数进行计算。 - 最后,输出计算结果。
注意:
- 此代码仅处理整数运算,不支持浮点数运算。* 未处理除数为零等异常情况。
希望本文能够帮助你理解如何使用C语言实现中缀表达式的求值。
原文地址: https://www.cveoy.top/t/topic/bkjJ 著作权归作者所有。请勿转载和采集!