2026/4/18 6:42:35
网站建设
项目流程
中国工程建设企业协会网站,张家界网站制作公司,莆田有哪几家做网站设计的,专业微网站哪家专业一、核心原理在开始代码实现之前#xff0c;我们需要先明确两个核心步骤#xff1a;逆波兰表达式的求值过程#xff0c;以及中缀表达式转后缀表达式的转换规则。这两个步骤是计算器实现的基石#xff0c;理解其原理是后续代码编写的关键。1.1 逆波兰表达式#xff08;后缀…一、核心原理在开始代码实现之前我们需要先明确两个核心步骤逆波兰表达式的求值过程以及中缀表达式转后缀表达式的转换规则。这两个步骤是计算器实现的基石理解其原理是后续代码编写的关键。1.1 逆波兰表达式后缀表达式求值原理逆波兰表达式的求值过程依赖栈这种数据结构利用栈“后进先出”的特性能够快速完成运算。其核心逻辑如下建立一个栈用于存储运算数依次遍历逆波兰表达式中的每个元素如果当前元素是运算数直接将其入栈如果当前元素是运算符弹出栈顶的两个运算数注意先弹出的是右运算数后弹出的是左运算数使用当前运算符对两个运算数进行计算将结果作为新的运算数入栈遍历完表达式后栈中剩余的唯一元素就是最终的运算结果。举个简单的例子逆波兰表达式1 2 3 4 * -对应的中缀表达式为12-(3*4)的求值过程遍历到1入栈栈[1]遍历到2入栈栈[1,2]遍历到弹出 2右和 1左计算 123入栈栈[3]遍历到3入栈栈[3,3]遍历到4入栈栈[3,3,4]遍历到*弹出 4右和 3左计算 3*412入栈栈[3,12]遍历到-弹出 12右和 3左计算 3-12-9入栈栈[-9]遍历结束结果为 - 9。这个过程是无需考虑运算符优先级的因为逆波兰表达式已经在转换过程中处理了优先级和括号求值逻辑非常直观。1.2 中缀表达式转后缀表达式原理中缀表达式转后缀表达式是计算器实现的核心难点其关键在于利用栈处理运算符的优先级和括号。转换规则如下1.2.1 转换规则建立一个栈用于存储运算符依次遍历中缀表达式中的每个字符如果当前字符是运算数0-9直接输出到后缀表达式中如果当前字符是左括号(直接入栈如果当前字符是右括号)则弹出栈顶运算符并输出直到遇到左括号(为止左括号弹出但不输出如果当前字符是运算符、-、*、/若栈为空或栈顶元素是左括号(则当前运算符直接入栈若当前运算符的优先级高于栈顶运算符直接入栈若当前运算符的优先级低于或等于栈顶运算符则弹出栈顶运算符并输出重复此过程直到栈为空或栈顶运算符优先级低于当前运算符最后将当前运算符入栈遍历完中缀表达式后弹出栈中剩余的所有运算符依次输出到后缀表达式中。1.2.2 运算符优先级定义为了判断运算符优先级我们需要明确*和/的优先级高于和-。本文定义优先级如下、-优先级 1*、/优先级 2。1.2.3 转换案例这里以中缀表达式12-(3*45)-7为例为大家详细展示转换过程遍历字符栈状态运算符后缀表达式说明1空1运算数直接输出[]1栈空入栈2[]1 2运算数直接输出-[-]1 2 -优先级等于栈顶弹出输出栈空后入栈-([-, (]1 2 左括号直接入栈3[-, (]1 2 3运算数直接输出*[-, (, *]1 2 3栈顶是(入栈*4[-, (, *]1 2 3 4运算数直接输出[-, (, ]1 2 3 4 *优先级低于栈顶*弹出*输出栈顶是(入栈5[-, (, ]1 2 3 4 * 5运算数直接输出)[-]1 2 3 4 * 5 右括号弹出运算符直到((弹出不输出-[-]1 2 3 4 * 5 --优先级等于栈顶-弹出-输出栈空后入栈-7[-]1 2 3 4 * 5 - 7运算数直接输出遍历结束空1 2 3 4 * 5 - 7 -弹出栈中剩余-输出最终得到的后缀表达式为1 2 3 4 * 5 - 7 -对应的计算结果为12-(3*45)-7 3 - 17 -7 -21后续求值过程将验证这一结果。二、逆波兰表达式求值实现evalRPN2.1 功能说明实现一个函数evalRPN输入为逆波兰表达式的字符串数组每个元素为运算数或运算符输出为计算结果。2.2 代码实现代码语言javascriptAI代码解释#include iostream #include vector #include stack #include string #include cassert using namespace std; class Calculator { public: // 逆波兰表达式求值 int evalRPN(const vectorstring tokens) { stackint numStack; // 存储运算数的栈 for (size_t i 0; i tokens.size(); i) { const string token tokens[i]; // 判断当前token是运算数还是运算符 if (isOperator(token)) { // 运算符弹出两个运算数计算 assert(numStack.size() 2 逆波兰表达式格式错误运算符数量过多); int rightNum numStack.top(); numStack.pop(); int leftNum numStack.top(); numStack.pop(); // 根据运算符计算结果并入栈 int result calculate(leftNum, rightNum, token[0]); numStack.push(result); } else { // 运算数转换为int后入栈 numStack.push(stoi(token)); } } assert(numStack.size() 1 逆波兰表达式格式错误运算数数量过多); return numStack.top(); } private: // 判断字符串是否为运算符、-、*、/ bool isOperator(const string s) { return s || s - || s * || s /; } // 执行二元运算 int calculate(int left, int right, char op) { switch (op) { case : return left right; case -: return left - right; case *: return left * right; case /: // 注意这里默认除数不为0实际应用中需添加除数为0的判断 assert(right ! 0 除数不能为0); return left / right; default: assert(false 不支持的运算符); return 0; } } }; // 测试函数 void testEvalRPN() { Calculator calc; // 测试用例112-(3*45)-7 -21 vectorstring tokens1 {1, 2, , 3, 4, *, 5, , -, 7, -}; assert(calc.evalRPN(tokens1) -21); // 测试用例23*(4-5/2) 3*(4-2) 6 vectorstring tokens2 {3, 4, 5, 2, /, -, *}; assert(calc.evalRPN(tokens2) 6); // 测试用例3100/25 4 vectorstring tokens3 {100, 25, /}; assert(calc.evalRPN(tokens3) 4); cout 逆波兰表达式求值测试全部通过 endl; } int main() { testEvalRPN(); return 0; }2.3 代码解析数据结构选择使用stackint存储运算数利用栈的后进先出特性快速获取最近的两个运算数运算符判断通过isOperator函数判断当前字符串是否为支持的运算符、-、*、/运算逻辑当遇到运算符时弹出栈顶两个元素注意先弹出的是右运算数后弹出的是左运算数例如a - b栈中先入 a 再入 b弹出时先得到 b再得到 a计算 a - b异常处理使用assert断言处理逆波兰表达式格式错误如运算符过多、运算数过多和除数为 0 的情况。2.4 运行结果编译运行上述代码最终得到输出如下代码语言javascriptAI代码解释逆波兰表达式求值测试全部通过说明逆波兰表达式的求值逻辑正确能够处理各种合法的逆波兰表达式。三、中缀表达式转后缀表达式实现toRPN3.1 功能说明实现一个函数toRPN输入为中缀表达式字符串输出为对应的逆波兰表达式字符串数组。需要处理的主要问题有运算符优先级对比、括号的递归处理、多位数的读取。www.dongchedi.com/article/7595302803858293310www.dongchedi.com/article/7595302533728551486www.dongchedi.com/article/7595303526705152574www.dongchedi.com/article/7595302533728911934www.dongchedi.com/article/7595302732916277822www.dongchedi.com/article/7595300345145164313www.dongchedi.com/article/7595287730565710361www.dongchedi.com/article/7595287469617529368www.dongchedi.com/article/7595289256520663576www.dongchedi.com/article/7595287092394000920www.dongchedi.com/article/7595286612611957273www.dongchedi.com/article/7595285133738410521www.dongchedi.com/article/7595285905729487384www.dongchedi.com/article/7595287514580435481www.dongchedi.com/article/7595285072006382105www.dongchedi.com/article/7595285719334502936www.dongchedi.com/article/7595285631619007000www.dongchedi.com/article/7595277509785453081www.dongchedi.com/article/7595276630432760345www.dongchedi.com/article/7595275735409967640www.dongchedi.com/article/7595277089067549246www.dongchedi.com/article/7595276413155295769www.dongchedi.com/article/7595276373905195544www.dongchedi.com/article/7595274144955499033www.dongchedi.com/article/7595274423667048984www.dongchedi.com/article/7595275907217195545www.dongchedi.com/article/7595274913787953689www.dongchedi.com/article/7595274806057337368www.dongchedi.com/article/7595274833727406617www.dongchedi.com/article/7595255011878208062www.dongchedi.com/article/7595255232834159166www.dongchedi.com/article/7595244636982379033www.dongchedi.com/article/7595246619336000062www.dongchedi.com/article/7595245102663352894www.dongchedi.com/article/7595246113737982526www.dongchedi.com/article/7595238963515146814www.dongchedi.com/article/7595238605032292888www.dongchedi.com/article/7595237840809198105www.dongchedi.com/article/7595237829975212569www.dongchedi.com/article/7595237024668877336www.dongchedi.com/article/7594914712018600510www.dongchedi.com/article/7594913283375907352www.dongchedi.com/article/7594914016129106456www.dongchedi.com/article/7594914424213766718www.dongchedi.com/article/7594913083894891033www.dongchedi.com/article/7594913119710069310www.dongchedi.com/article/7594912459706696254www.dongchedi.com/article/7594911633613390360www.dongchedi.com/article/7594909036307595800www.dongchedi.com/article/7594910057444786750www.dongchedi.com/article/7594909893274927641www.dongchedi.com/article/7594909974816588350www.dongchedi.com/article/7594909035217404441www.dongchedi.com/article/7594908551181615678www.dongchedi.com/article/7594906883010855486www.dongchedi.com/article/7594907513641058878www.dongchedi.com/article/7594905549272285720www.dongchedi.com/article/7594906861996884505www.dongchedi.com/article/7594906134906421785www.dongchedi.com/article/7594905869373096472www.dongchedi.com/article/7594904114086560281www.dongchedi.com/article/7594903301414650392www.dongchedi.com/article/7594901235942228542www.dongchedi.com/article/7594901805579452953www.dongchedi.com/article/7594900315275657752www.dongchedi.com/article/7594899458794799641www.dongchedi.com/article/7594900266315645465www.dongchedi.com/article/7594900151853236798www.dongchedi.com/article/7594898846111580697www.dongchedi.com/article/7594899021756776984www.dongchedi.com/article/7594897166766965272www.dongchedi.com/article/7594897310665245246www.dongchedi.com/article/7594897192310407705www.dongchedi.com/article/7594897631617794585www.dongchedi.com/article/7594896580080435737www.dongchedi.com/article/7594895704896684568www.dongchedi.com/article/7594897374330503705www.dongchedi.com/article/7594895618720449048www.dongchedi.com/article/7594896034183397912www.dongchedi.com/article/7594896087656694334