C++ 在C+中使用堆栈+;计算后缀表达式的步骤
好的,我已经有了后缀符号,我正在发送一个字符串变量,它将有后缀符号,比如:5 15 2*+ 这是我的密码:C++ 在C+中使用堆栈+;计算后缀表达式的步骤,c++,stack,postfix-notation,C++,Stack,Postfix Notation,好的,我已经有了后缀符号,我正在发送一个字符串变量,它将有后缀符号,比如:5 15 2*+ 这是我的密码: int evaluatePostFix(string postfix_expression){ //Create a new stack stack<int> theStack; //Loops while the postfix expression string still contains values while(postfix_expression.length()&
int evaluatePostFix(string postfix_expression){
//Create a new stack
stack<int> theStack;
//Loops while the postfix expression string still contains values
while(postfix_expression.length()>=1){
//Loops on a number an whitespace
while(isdigit(postfix_expression.at(0)) || isspace(postfix_expression.at(0))){
//Holds a number that is above two digits to be added to the stack
string completeNum;
if(isdigit(postfix_expression.at(0))){
//Add the digit so it can become a full number if needed
completeNum+=postfix_expression.at(1);
}
else {
//Holds the integer version of completeNum
int intNum;
//Make completeNum an int
intNum=atoi(completeNum.c_str());
//push the number onto the stack
theStack.push(intNum);
}
//Check to see if it can be shortened
if(postfix_expression.length()>=1){
//Shorten the postfix expression
postfix_expression=postfix_expression.substr(1);
}
}
//An Operator has been found
while(isOperator(postfix_expression.at(0))){
int num1, num2;
char op;
//Grabs from the top of the stack
num1=theStack.top();
//Pops the value from the top of the stack - kinda stupid how it can return the value too
theStack.pop();
//Grabs the value from the top of the stack
num2=theStack.top();
//Pops the value from the top of the stack
theStack.pop();
//Grab the operation
op=postfix_expression.at(0);
//Shorten the postfix_expression
postfix_expression=postfix_expression.substr(1);
//Push result onto the stack
theStack.push(Calculate(num1,num2, op));
}
}
return theStack.top();
int evaluatePostFix(字符串后缀\u表达式){
//创建一个新堆栈
堆叠钉子;
//在后缀表达式字符串仍包含值时循环
while(postfix_expression.length()>=1){
//在数字上循环一个空格
while(isdigit(postfix_expression.at(0))| | isspace(postfix_expression.at(0))){
//保存要添加到堆栈中的两位数以上的数字
弦完整;
if(isdigit(postfix_expression.at(0)){
//添加数字,以便在需要时成为完整数字
completeneum+=postfix_表达式在(1)处;
}
否则{
//保存completeNum的整数版本
int intNum;
//将completeneum设为int
intNum=atoi(completeneum.c_str());
//将数字推到堆栈上
钉压(intNum);
}
//检查是否可以缩短
if(后缀表达式.length()>=1){
//缩短后缀表达式
后缀表达式=后缀表达式substr(1);
}
}
//找到了一个接线员
while(等位符(postfix_表达式.at(0))){
int num1,num2;
char op;
//从堆栈顶部抓取
num1=stack.top();
//从堆栈顶部弹出值-它如何返回值有点愚蠢
theStack.pop();
//从堆栈顶部获取值
num2=stack.top();
//从堆栈顶部弹出值
theStack.pop();
//抓紧手术
op=后缀_表达式(0);
//缩短后缀表达式
后缀表达式=后缀表达式substr(1);
//将结果推送到堆栈上
粘滞推力(计算(num1,num2,op));
}
}
返回stack.top();
}
我得到的错误是“Deque迭代器不可延迟”
如果我能在这个错误上得到任何帮助,我将不胜感激。
顺便说一下,我几年来还没有使用C++,所以我有点生疏了。 < P>如果你告诉我们哪一行是通过调试程序来导致错误的话,那就更容易了。然而,我想我可能已经发现了错误 在这段代码中
if(isdigit(postfix_expression.at(0))){
//Add the digit so it can become a full number if needed
completeNum+=postfix_expression.at(1);
}
您请求postfix_表达式.at(1),但从未检查该元素是否存在。由于没有检查,您可能正在访问错误的内存位置。为什么处理
isspace()
与isdigit()
相同?我觉得这是个错误。(虽然可能不是您遇到的错误消息)您粘贴了所有代码,因为您有语法错误?为什么不只发布导致错误的行呢?Hrm,您正在检查是否可以在使用部分字符串作为输入后缩短字符串。如果字符串以空格或数字结尾,我认为您永远不会终止外部while(postfix\u expression.length()>=1)
循环。不要检查是否可以减少字符串--您使用了输入,所以请减少其大小。此外,如果将12
作为输入,则仅从字符串中删除一个数字将失败;stack.pop()代码>东西?更惯用的是foo=stack.pop()代码>而不是先查看堆栈。