求指导,为什么本人的程序不往下执行了

C++语言 码拜 7年前 (2017-05-09) 2661次浏览
最近学习了,然后就做了一道的题目,叫做口袋计算器,其他的函数本人都写出来了,但是就差一个问题,为什么本人输入字符的时候,连最基本的helpCommand()都调用不出来?本人本人分析了几遍,感觉没问题啊。求指导,C++代码如下:
/*口袋计算器 */
#include <iostream>
#include <string>
#include <cctype>
#include <stack>
#include <cstdlib>
using namespace std;
/*函数原型*/
void applyOperator(char op,stack<double> & operandStack);
void helpCommand();
void error(string msg);
void clearOperandStack(stack<double> & operandStack);
/*主函数*/
int main () {
cout << “RPN Calculator Simulation (type H for help)” << endl;
stack<double> operandStack;
while(true) {
cout << “>”;
string line;
getline(cin,line);
if (line.length() == 0) {
line = “Q”;
}
char ch = toupper(line[0]);
if (ch = “Q”) {
break;
} else if(ch = “H”) {
helpCommand();
} else if (ch = “C”) {
clearOperandStack(operandStack);
} else if (isdigit(ch)) {
operandStack.push(atof(line.c_str()));
} else {
applyOperator(ch,operandStack);
}
}
return 0;
}
void error(string msg) {
cerr << msg << endl;
exit(EXIT_FAILURE);
}
void applyOperator(char op, stack<double> & operandStack) {
double result;
double rhs = operandStack.top();
operandStack.pop();
double lhs = operandStack.top();
operandStack.pop();
switch (op) {
case “+”: result = lhs + rhs; break;
case “-“: result = lhs – rhs; break;
case “*”: result = lhs * rhs; break;
case “/”: result = lhs / rhs; break;
default: error(“Illegal operator”);
}
cout << result << endl;
operandStack.push(result);
}
void helpCommand() {
cout << “Enter expressions in Reverse Polish Notation,” << endl;
cout << “in which operators follow the operands to which” << endl;
cout << “they apply. Each line consists of a number, an” << endl;
cout << “operator, or one of the following commands:” << endl;
cout << ” Q — Quit the program” << endl;
cout << ” H — Display this help message” << endl;
cout << ” C — Clear the calculator stack” << endl;
}
void clearOperandStack(stack<double> & operandStack) {
while (!operandStack.empty()) {
operandStack.pop();
}
}
求高手指导
解决方案

20

if (ch = “Q”) {
break;
ch=”Q” 写错了,赋值语句,返回Q恒为true,直接跳出循环

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求指导,为什么本人的程序不往下执行了
喜欢 (0)
[1034331897@qq.com]
分享 (0)