Thread: moving though std stack

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  2. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM