-
moving though std stack
what i'm trying to do is convert a infix expression to a postfix expression using the std stack. for one of the cases (if the current character of the input string is an operator), i go to this function with the parameter being the operator:
Code:
void InfixToPostfix::operatorCase(char op)
{
string f;
//assert(!s.empty());
assert(s.top() != NULL);
for (int k = s.top(); (k != '(' || k != 0) &&
(precidence(k) >= precidence(s.top())); k--) //<--cant decrement like that
{
//cout << "yo";
s.push(op);
f = s.top();
s.pop();
}
postfix.append(f);
}
s is the stack of type char. so i need to move through the stack to go implement the following logic:
if the scanned symbol is an operator:
* pop and append to the postfix expression every operator from the stack that is above the most recently scanned left parenthesis (or bottom of stack if no left parenthesis) and has precedence greater than or equal to the new operator.
* push the new operator onto the stack.
any ideas? thanks
-
I'm completely confused about the correspondence between what you've written about the function and what it actually does.
And you should call your function precedence".
--
Mats
-
When I tackled this infamous exercise I found that this Wikipedia page has everything that is needed to evaluate a postfix expression. Check out the link to Shunting Yard to convert infix to postfix.