Postfix Calculator Using Stack, Linked Lists

This is a discussion on Postfix Calculator Using Stack, Linked Lists within the C++ Programming forums, part of the General Programming Boards category; Hello, I've got a program I'm doing that does +, -, *, and / operations on positive integers in a ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Postfix Calculator Using Stack, Linked Lists

    Hello,

    I've got a program I'm doing that does +, -, *, and / operations on positive integers in a postfix notation. The code I have at the moment compiles, but upon running the code, it stalls once I enter the expression. When I hit Enter, it should recognize the '\n' character and break the loop but it does not. Any help on fixing this error (and any others)?

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class node
    {
      public:
        float number;
        node *next;
    };
    
    node* push(node *stack, float data)
    {
      node *utility;
      utility = new node;
      utility -> number = data;
      utility -> next = stack;
      return utility;
    }
    
    node* pop(node *stack, float &data)
    {
      node *temp;
      if (stack != NULL)
      {
        temp = stack;
        data = stack -> number;
        stack = stack -> next;
        delete temp;
      }
      else cout << "\nERROR: Empty stack.\n";
      return stack;
    }
    
    int main()
    {
      float answer, operand1, operand2;
      char ch;
      node *utility, *top;
    
      utility = new node;
      utility -> number = 0;
      utility -> next = NULL;
      top = new node;
      top -> number = 0;
      top -> next = utility;
    
      cout << "Postfix Calculator\n\n"
           << "Enter a legal positive integer postfix operation.\n\n";
      cin >> ch;
      while(ch != '\n')
        {
            cin >> ch;
            int operand = 0;
            while(ch == ' ')
              cin >> ch;
            if((ch >= '0')&&(ch <= '9')){
              while(ch != ' '){
                operand = operand*10 + (ch-48);
                cin >> ch;}
              top = push(top, operand);}
            else{
              pop(top, operand1);
              pop(top, operand2);
              switch(ch){
                case '+': answer = operand2 + operand1;
                case '-': answer = operand2 - operand1;
                case '*': answer = operand2 * operand1;
                case '/': answer = operand2 / operand1;}
              push(top, answer);}
        }
      pop(top, answer);
      cout << "\nAnswer: " << answer << endl;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The cin >> ch at the very top of the loop means you don't process the first number read in.

    I also don't see why you need to seed the stack with two "blank" values.

    Edit: And don't forget some breaks after your switch cases.

    Edit edit: And the magic phrase is "cin >> noskipws".
    Last edited by tabstop; 12-04-2008 at 08:50 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I always forget those breaks...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    However even with making those fixes it still doesn't advance once I hit Enter/Return...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should only need to do noskipws once, somewhere -- I put it before any other reads in your code.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class node
    {
      public:
        float number;
        node *next;
    };
    
    node* push(node *stack, float data)
    {
      node *utility;
      utility = new node;
      utility -> number = data;
      utility -> next = stack;
      return utility;
    }
    
    node* pop(node *stack, float &data)
    {
      node *temp;
      if (stack != NULL)
      {
        temp = stack;
        data = stack -> number;
        stack = stack -> next;
        delete temp;
      }
      else cout << "\nERROR: Empty stack.\n";
      return stack;
    }
    
    int main()
    {
      float answer, operand1, operand2;
      char ch = ' ';                                     //just so that the while loop runs
      node *utility, *top;
    
      utility = new node;
      utility -> number = 0;
      utility -> next = NULL;
      top = new node;
      top -> number = 0;
      top -> next = utility;
    
      cout << "Postfix Calculator\n\n"
           << "Enter a legal positive integer postfix operation.\n\n";
     while(ch != '\n')
        {
            cin >> noskipws >> ch;
            int operand = 0;
            while(ch == ' ')
              cin >> ch;
            if((ch >= '0')&&(ch <= '9')){
              while(ch != ' '){
                operand = operand*10 + (ch-48);
                cin >> ch;}
              top = push(top, operand);}
            else{
              pop(top, operand1);
              pop(top, operand2);
              switch(ch){
                case '+': answer = operand2 + operand1;
                case '-': answer = operand2 - operand1;
                case '*': answer = operand2 * operand1;
                case '/': answer = operand2 / operand1;}
              push(top, answer);}
        }
      pop(top, answer);
      cout << "\nAnswer: " << answer << endl;
    }
    This is where I am now and the problem still hasn't righted itself... it doesn't recognize the new-line character at all.

    EDIT: Never mind, the program gives me an answer... except it's not the right one. 2 3 45 + + gives me 90 instead of 50. Hmm...
    Last edited by Velocity; 12-05-2008 at 07:52 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do realize pop is unable to change the value of its first parameter? Just like you have top = push, you should to top = pop.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I don't get what you mean. Are you meaning to put:

    top = pop(top, operand1);
    top = pop(top, operand2);

    in the "else" part of the main function?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes. Otherwise top will still be pointing at 45, forever and ever.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I tried that as soon as I posted last, and it gave me:

    ERROR: Empty stack.

    ERROR: Empty stack.

    Answer: 2

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you've got one stray push down at the bottom that needs to be top = push too.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Brilliant, that did the trick. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21