C++ C++;使用堆栈的后缀表达式求值。我不认为我';我正确地转换数据
我目前正在尝试让这个后缀表达式eval工作,但我相信在C++ C++;使用堆栈的后缀表达式求值。我不认为我';我正确地转换数据,c++,pointers,stack,C++,Pointers,Stack,我目前正在尝试让这个后缀表达式eval工作,但我相信在int EvaluatePostfix函数中,我使用的stackPtr->peek()是错误的,因为每当我尝试获取最大值并减去“0”(代码中未显示,mb)将其转换为int时,它会说它是一个“std::basic_string-char-”所以它不能用char类型进行减法运算 postfix.cpp: #include <iostream> #include <string> #include "ArraySt
int EvaluatePostfix
函数中,我使用的stackPtr->peek()
是错误的,因为每当我尝试获取最大值并减去“0”(代码中未显示,mb)将其转换为int时,它会说它是一个“std::basic_string-char-”所以它不能用char类型进行减法运算
postfix.cpp:
#include <iostream>
#include <string>
#include "ArrayStack.h"
bool IsNumericDigit(char C)
{
if(C >= '0' && C <= '9') return true;
return false;
}
// Function to verify whether a character is operator symbol or not.
bool IsOperator(char C)
{
if(C == '+' || C == '-' || C == '*' || C == '/')
return true;
return false;
}
// Function to perform an operation and return output.
int PerformOperation(char operation, int operand1, int operand2)
{
if(operation == '+') return operand1 +operand2;
else if(operation == '-') return operand1 - operand2;
else if(operation == '*') return operand1 * operand2;
else if(operation == '/') return operand1 / operand2;
else std::cout<<"Unexpected Error \n";
return -1;
}
int EvaluatePostfix(std::string expression, StackInterface<std::string>* stackPtr)
{
for(int i = 0;i< expression.length();i++)
{
// Scanning each character from left.
// If character is a delimiter, move on.
if(expression[i] == ' ' || expression[i] == ',') continue;
// If character is operator, pop two elements from stack, perform operation and push the result back.
else if(IsOperator(expression[i]))
{
// Pop two operands.
int operand2 = stackPtr->peek();
stackPtr->pop();
int operand1 = stackPtr->peek();
stackPtr->pop();
//operand1 and operand2 are reversed in case of Prefix Expression
// Perform operation
int result = PerformOperation(expression[i], operand1, operand2);
//Push back result of operation on stack.
stackPtr->push(result);
}
else if(IsNumericDigit(expression[i]))
{
// Extract the numeric operand from the string
// Keep incrementing i as long as you are getting a numeric digit.
int operand = 0;
while(i<expression.length() && IsNumericDigit(expression[i]))
{
// For a number with more than one digits, as we are scanning from left to right.
// Everytime , we get a digit towards right, we can multiply current total in operand by 10
// and add the new digit.
operand = (operand*10) + (expression[i] - '0');
std::cout << operand << std::endl;
i++;
}
// Finally, you will come out of while loop with i set to a non-numeric character or end of string
// decrement i because it will be incremented in increment section of loop once again.
// We do not want to skip the non-numeric character by incrementing i twice.
i--;
// Push operand on stack.
stackPtr->push(operand);
}
}
// If expression is in correct format, Stack will finally have one element. This will be the output.
return stackPtr->top();
}
int main(){
StackInterface<std::string>* stackPtr = new ArrayStack<std::string>();
std::string expression;
std::cout<<"Enter Postfix Expression \n";
std::getline(std::cin,expression);
EvaluatePostfix(expression, stackPtr)
std::cout << stackPtr->push(expression);
}
#包括
#包括
#包括“ArrayStack.h”
布尔ISnumericDigital(字符C)
{
如果(C>='0'&&C peek();
stackPtr->pop();
//对于前缀表达式,操作数1和操作数2是相反的
//执行操作
int result=执行操作(表达式[i],操作数1,操作数2);
//向后推堆栈上的操作结果。
stackPtr->push(结果);
}
else if(IsNumericDigit(表达式[i]))
{
//从字符串中提取数字操作数
//只要你得到一个数字,就继续递增i。
整数操作数=0;
while(i这里的问题是,您可以互换地使用std::string
、char
和int
,而它们不是
请注意,堆栈的数据类型是string
,并且没有从string
更改为int
或string
到char
的默认方式
根据您的描述,您试图从字符串
中获取第一个字符
,您可能会调用:
c = stackPtr->peek()[0];
或
string
toint
将调用std::stoi(stackPtr->peek())
,但不确定您是否需要它,因为您自己正在实现它
因此,您可能希望将此部分提取为单独的函数:
while(i<expression.length() && IsNumericDigit(expression[i]))
{
operand = (operand*10) + (expression[i] - '0');
std::cout << operand << std::endl;
i++;
}
while(i@Ranoiaetep我用堆栈实现编辑了这篇文章。我正在尝试从堆栈中检索最上面的项(它应该是一个基于用户以字符串/字符形式输入的数字)但是我很难将它转换成整数值。非常感谢。我回到代码中,不得不重新思考应该如何计算,并意识到你完全正确。我使用了太多不同的变量类型。我意识到,实际上我只需要在堆栈中使用整数(我试图使用的图书示例中有std::string堆栈指针示例,因此它也让我感到厌烦)。
c = stackPtr->peek().front();
while(i<expression.length() && IsNumericDigit(expression[i]))
{
operand = (operand*10) + (expression[i] - '0');
std::cout << operand << std::endl;
i++;
}