C++实现Reverse Polish Notation计算器测试不通过问题排查
问题核心错误排查
你代码中存在三个核心错误导致测试失败:
- 运算符处理逻辑完全错误:遇到运算符时没有从栈中取已压入的操作数,反而错误读取原始token数组中当前位置前两位的token计算,仅第一个长度为3的测试用例刚好命中正确取值,后续测试全部取值错误。
- 二元运算符的操作数顺序需要适配栈的后进先出特性:先弹出的是右操作数,后弹出的是左操作数,要保证运算顺序正确。
- 类成员默认是私有访问权限,main函数无法直接调用evaluateCountdown方法,需要修改为public权限。
修复后的完整代码
#include <string> #include <vector> #include <stack> class CountdownSolution { public: std::vector<std::string> stringToVector(const std::string newString,const char token) { std::vector<std::string> stringVector; size_t posLast = 0, pos = 0; while((pos = newString.find(token, pos)) != std::string::npos) { if(newString[pos] != newString[posLast]) stringVector.push_back(newString.substr(posLast, pos - posLast)); posLast = ++pos; } if(newString[posLast] != 0) stringVector.push_back(newString.substr(posLast)); return stringVector; } double evaluateCountdown(const std::string& rpnString) { std::vector<std::string> rpnStringVector = stringToVector(rpnString,' '); std::stack<double> rpnStack; for (auto& token : rpnStringVector) { if(token == "+" ){ double right = rpnStack.top(); rpnStack.pop(); double left = rpnStack.top(); rpnStack.pop(); rpnStack.push(left + right); } else if(token == "-"){ double right = rpnStack.top(); rpnStack.pop(); double left = rpnStack.top(); rpnStack.pop(); rpnStack.push(left - right); } else if(token == "*"){ double right = rpnStack.top(); rpnStack.pop(); double left = rpnStack.top(); rpnStack.pop(); rpnStack.push(left * right); } else if(token == "/"){ double right = rpnStack.top(); rpnStack.pop(); double left = rpnStack.top(); rpnStack.pop(); rpnStack.push(left / right); } else { rpnStack.push(std::stod(token)); } } return rpnStack.top(); } };
测试代码适配修改
因为evaluateCountdown是类成员方法,需要创建类实例调用:
#include <iostream> using std::cout; using std::endl; #include <string> int main() { CountdownSolution sol; int retval = 0; { double answer = sol.evaluateCountdown(std::string("3.0 4.0 +")); if (answer == 7.0) { cout << "1) Pass: evaluated '3.0 4.0 +', got 7.0\n"; } else { cout << "1) Fail: evaluated '3.0 4.0 +' expecting 7.0, but got " << answer << std::endl; ++retval; } } { double answer = sol.evaluateCountdown(std::string("3.0 4.0 - 4.0 +")); if (answer == 3.0) { cout << "2) Pass: evaluated '3.0 4.0 - 4.0 +', got 3.0\n"; } else { cout << "2) Fail: evaluated '3.0 4.0 - 4.0 +' expecting 3.0, but got " << answer << std::endl; ++retval; } } { double answer = sol.evaluateCountdown(std::string("3.0 4.0 - 2.0 *")); if (answer == -2.0) { cout << "3) Pass: evaluated '3.0 4.0 - 2.0 *', got -2.0\n"; } else { cout << "3) Fail: evaluated '3.0 4.0 - 2.0 *' expecting -2.0, but got " << answer << std::endl; ++retval; } } { double answer = sol.evaluateCountdown(std::string("100 20 / 4 +")); if (answer == 9) { cout << "4) Pass: evaluated '100 20 / 4 +', got 9\n"; } else { cout << "4) Fail: evaluated '100 20 / 4 +' expecting 9, but got " << answer << std::endl; ++retval; } } return retval; }
修复后运行测试所有用例均可通过。
内容的提问来源于stack exchange,提问作者Anna Lenkovska
相关产品推荐
相关产品推荐

