C++ 实现基于栈的简单计算器:解析算术表达式
使用 C++ 完成基于栈的简单计算器
问题说明
利用栈实现一个简单的计算器软件,实现算术表达式求值。表达式可以包含 +、-、、/ 四个双目运算符,允许小数点和科学计数法,允许单目运算符(-),允许包含多层嵌套的括号。例如:3.5(7+2) /(-6)。
对一个表达式的计算可以通过两个步骤来实现:
(1) 先把表达式变换为相应的后缀表达式; (2) 根据后缀表达式计算表达式的值。
基本要求
(1) 编写控制台程序,正确解释表达式; (2) 符合四则运算规则:先乘除、后加减,从左到右运算,先括号内,后括号外; (3) 输出最后的计算结果。
代码示例
#include <iostream>
#include <stack>
#include <string>
#include <cmath>
bool isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
bool isHigherPrecedence(char op1, char op2) {
// 判断运算符的优先级
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return true;
}
return false;
}
double performOperation(double operand1, double operand2, char operation) {
// 执行运算
switch (operation) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
return operand1 / operand2;
default:
return 0;
}
}
double evaluateExpression(std::string expression) {
std::stack<double> operandStack; // 操作数栈
std::stack<char> operatorStack; // 运算符栈
for (int i = 0; i < expression.length(); i++) {
char currentChar = expression[i];
if (currentChar == ' ') {
continue;
}
else if (isdigit(currentChar) || currentChar == '.') {
// 解析数字
int j = i;
std::string numberStr = '';
while (j < expression.length() && (isdigit(expression[j]) || expression[j] == '.')) {
numberStr += expression[j];
j++;
}
double number = std::stod(numberStr);
operandStack.push(number);
i = j - 1;
}
else if (currentChar == '(') {
// 左括号入栈
operatorStack.push(currentChar);
}
else if (currentChar == ')') {
// 计算右括号前的表达式
while (!operatorStack.empty() && operatorStack.top() != '(') {
char op = operatorStack.top();
operatorStack.pop();
double operand2 = operandStack.top();
operandStack.pop();
double operand1 = operandStack.top();
operandStack.pop();
double result = performOperation(operand1, operand2, op);
operandStack.push(result);
}
if (!operatorStack.empty()) {
operatorStack.pop(); // 弹出左括号
}
}
else if (isOperator(currentChar)) {
// 计算前面优先级更高的运算符
while (!operatorStack.empty() && isOperator(operatorStack.top()) && isHigherPrecedence(operatorStack.top(), currentChar)) {
char op = operatorStack.top();
operatorStack.pop();
double operand2 = operandStack.top();
operandStack.pop();
double operand1 = operandStack.top();
operandStack.pop();
double result = performOperation(operand1, operand2, op);
operandStack.push(result);
}
operatorStack.push(currentChar);
}
}
// 计算剩余的表达式
while (!operatorStack.empty()) {
char op = operatorStack.top();
operatorStack.pop();
double operand2 = operandStack.top();
operandStack.pop();
double operand1 = operandStack.top();
operandStack.pop();
double result = performOperation(operand1, operand2, op);
operandStack.push(result);
}
// 返回最终结果
if (!operandStack.empty()) {
return operandStack.top();
}
return 0;
}
int main() {
std::string expression;
std::cout << 'Enter an arithmetic expression: ';
std::getline(std::cin, expression);
double result = evaluateExpression(expression);
std::cout << 'Result: ' << result << std::endl;
return 0;
}
代码说明
这个程序通过使用两个栈,一个存储操作数,一个存储运算符,来实现了基于栈的简单计算器。它遵循四则运算规则,先乘除后加减,从左到右运算,先计算括号内表达式。它能够正确解释包含双目运算符、小数点、科学计数法和多层嵌套括号的算术表达式,并输出最终的计算结果。
总结
本文介绍了使用 C++ 语言实现基于栈的简单计算器的方法,并提供了代码示例和详细解释。希望能够帮助你理解栈在解析算术表达式中的应用。
原文地址: https://www.cveoy.top/t/topic/Cpr 著作权归作者所有。请勿转载和采集!