C++ 实现数学表达式求值函数 - 支持括号、四则运算和浮点数
以下是一个使用 C++ 实现的函数,用于解析和计算包含括号、四则运算和浮点数的数学表达式:\n\nc++\n#include <iostream>\n#include <stack>\n#include <sstream>\n#include <cmath>\n\nusing namespace std;\n\nbool isOperator(char c) {\n return (c == '+' || c == '-' || c == '*' || c == '/');\n}\n\nint getPrecedence(char c) {\n if (c == '+' || c == '-')\n return 1;\n else if (c == '*' || c == '/')\n return 2;\n return 0;\n}\n\ndouble performOperation(double operand1, double operand2, char op) {\n switch (op) {\n case '+':\n return operand1 + operand2;\n case '-':\n return operand1 - operand2;\n case '*':\n return operand1 * operand2;\n case '/':\n return operand1 / operand2;\n default:\n return 0.0;\n }\n}\n\ndouble evaluateExpression(const string& expression) {\n stack<double> operandStack;\n stack<char> operatorStack;\n\n for (int i = 0; i < expression.length(); i++) {\n if (expression[i] == ' ')\n continue;\n\n if (isdigit(expression[i])) {\n stringstream ss;\n ss << expression[i];\n while (i + 1 < expression.length() && (isdigit(expression[i + 1]) || expression[i + 1] == '.'))\n ss << expression[++i];\n double operand;\n ss >> operand;\n operandStack.push(operand);\n } else if (expression[i] == '(') {\n operatorStack.push(expression[i]);\n } else if (expression[i] == ')') {\n while (!operatorStack.empty() && operatorStack.top() != '(') {\n char op = operatorStack.top();\n operatorStack.pop();\n\n if (operandStack.size() < 2)\n return 0.0;\n\n double operand2 = operandStack.top();\n operandStack.pop();\n double operand1 = operandStack.top();\n operandStack.pop();\n\n double result = performOperation(operand1, operand2, op);\n operandStack.push(result);\n }\n\n if (!operatorStack.empty())\n operatorStack.pop(); // Pop '('\n } else if (isOperator(expression[i])) {\n while (!operatorStack.empty() && getPrecedence(operatorStack.top()) >= getPrecedence(expression[i])) {\n char op = operatorStack.top();\n operatorStack.pop();\n\n if (operandStack.size() < 2)\n return 0.0;\n\n double operand2 = operandStack.top();\n operandStack.pop();\n double operand1 = operandStack.top();\n operandStack.pop();\n\n double result = performOperation(operand1, operand2, op);\n operandStack.push(result);\n }\n\n operatorStack.push(expression[i]);\n }\n }\n\n while (!operatorStack.empty()) {\n char op = operatorStack.top();\n operatorStack.pop();\n\n if (operandStack.size() < 2)\n return 0.0;\n\n double operand2 = operandStack.top();\n operandStack.pop();\n double operand1 = operandStack.top();\n operandStack.pop();\n\n double result = performOperation(operand1, operand2, op);\n operandStack.push(result);\n }\n\n if (operandStack.size() != 1)\n return 0.0;\n\n return operandStack.top();\n}\n\nint main() {\n string expression;\n cout << "Enter an expression: ";\n getline(cin, expression);\n\n double result = evaluateExpression(expression);\n cout << "Result: " << result << endl;\n\n return 0;\n}\n\n\n该函数使用两个栈,一个用于操作数,另一个用于操作符。它按照人类数学的方式处理表达式,包括括号、四则运算和浮点数。最后,它返回计算结果。\n\n请注意,此函数假定输入的表达式是有效的,并且不进行错误检查。在实际使用中,可能需要添加错误处理逻辑以处理无效的表达式。\n\n此外,该函数仅支持基本四则运算,不支持更复杂的数学函数。如果需要支持更复杂的函数,需要扩展函数逻辑,添加对函数的支持。\n\n希望本文对您有所帮助。如果您有任何问题或建议,请随时提出。\n
原文地址: https://www.cveoy.top/t/topic/pT6V 著作权归作者所有。请勿转载和采集!