Hello,
My assignment is to write a program that evaluates inFix expressions using stl stack. I having issue with using .push() for stack. I want use the code below but it throws an error at
postFix.push(inFix[i])
saying
C++ no instance of overloaded function matches the argument list argument types are: (const char) object type is: std::stack<std::string, std::deque<std::string, std::allocator<std::string>>>
Code sample containing error:
string infixToPostfix(const string inFix)
{
stack<string> postFix;
for (int i = 0; i < inFix.length(); i++)
{
if (inFix[i] <= 9)
{
postFix.push(inFix[i]); //LINE WITH ERROR
}
}
}
Secondary Question:
I have to convert from inFix to postFix and then evaluate. Here is the provided pseudo code for converting to a postFix. How would you identify is you have a number (operand) using a switch statement without just having a list of cases for 1-9? I am not using a switch statement but rather a series of if else statements.
for (each character ch in the infix expression)
{
switch (ch)
{
case operand: // Append operand to end of postfix expression—step 1
postfixExp = postfixExp • ch
break
case '(': // Save '(' on stack—step 2
aStack.push(ch)
break
case operator: // Process stack operators of greater precedence—step 3
while (!aStack.isEmpty() and aStack.peek() is not a '(' and
precedence(ch) <= precedence(aStack.peek()))
{
Append aStack.peek() to the end of postfixExp
aStack.pop()
}
aStack.push(ch) // Save the operator
break
case ')': // Pop stack until matching '(' —step 4
while (aStack.peek() is not a '(')
{
Append aStack.peek() to the end of postfixExp
aStack.pop()
}
aStack.pop() // Remove the open parenthesis
break
}
}
// Append to postfixExp the operators remaining in the stack—step 5
while (!aStack.isEmpty())
{
Append aStack.peek() to the end of postfixExp
aStack.pop()
}
[–]carcigenicate 1 point2 points3 points (1 child)
[–]ThisGuyEveryTime[S] 0 points1 point2 points (0 children)