Thread: Stack Postfix Calculator

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Stack Postfix Calculator

    I read a post about the same thing and didn't know how to comment on it so I just made a new thread. Here's the code and couldn't get it to run. If anyone could look at this and tell me what's wrong with it that'd be great. I get an answer, but its wrong, for example 23+ gives me 225.

    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;
    
    
      while(ch != 'q')
      { 
     cout << "calc> ";
     cin >> noskipws >> ch;
        if(ch == 'q')
        {
            return 0;
        }
     while(ch != '\n')
     {
          
            
            float operand = 0;
            if((ch >= '0')&&(ch <= '9'))
            {
              while(ch != '\n')
              {
                operand = operand*10 + (ch-48);
                  
                cin >> ch;
              }
              top = push(top, operand);
            }
            else
            {
                if((ch == '+')||(ch == '-')||(ch == '*')||(ch == '/')){
              top = pop(top, operand1);
              top = pop(top, operand2);
              switch(ch){
                case '+': answer = operand2 + operand1;
                    break;
                case '-': answer = operand2 - operand1;
                    break;
                case '*': answer = operand2 * operand1;
                    break;
                case '/': answer = operand2 / operand1;
                    break;
              }
              top = push(top, answer);}
                else {
                    cout << "Error! Wrong Character Entered";
                }
            }
            pop(top, answer);
      cout << "\nAnswer: " << answer << endl;
      
    }
      }
    }

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    I can't find the error at first glance, but let me just ask you, why are you implementing your own stack for this? This has been done a million times before, don't reinvent the wheel, use std::stack, then you can focus on getting the RPN and arithmetics correct.

    Also, why on earth would you use a linked-list for a stack implementation? What's wrong with an array, seems much simpler! (Or even better, a std::vector!)
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I guess the answer is to add some DEBUG code to help you figure out where it's going wrong.
    Though at some point, it's better to learn how to use a debugger.

    Anyway.
    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;
    
      while (ch != 'q') {
        cout << "calc> ";
        cin >> noskipws >> ch;
        if (ch == 'q') {
          return 0;
        }
        while (ch != '\n') {
          float operand = 0;
          if ((ch >= '0') && (ch <= '9')) {
            while (ch != '\n') {
              operand = operand * 10 + (ch - 48);
              cin >> ch;
            }
            cout << "Debug: pushed " << operand << endl;
            top = push(top, operand);
          } else {
            if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/')) {
              top = pop(top, operand1);
              top = pop(top, operand2);
              cout << "Debug: popped " << operand1 << " " << operand2 << endl;
              switch (ch) {
              case '+':
                answer = operand2 + operand1;
                break;
              case '-':
                answer = operand2 - operand1;
                break;
              case '*':
                answer = operand2 * operand1;
                break;
              case '/':
                answer = operand2 / operand1;
                break;
              }
              top = push(top, answer);
            } else {
              cout << "Error! Wrong Character Entered";
            }
          }
          pop(top, answer);
          cout << "\nAnswer: " << answer << endl;
        }
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Postfix Calculator
    By Bruins77 in forum C Programming
    Replies: 2
    Last Post: 02-07-2012, 01:21 PM
  2. what is a postfix calculator?
    By blogchama in forum C Programming
    Replies: 6
    Last Post: 05-16-2010, 02:23 AM
  3. Postfix Calculator Using Stack, Linked Lists
    By Velocity in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2008, 05:29 PM
  4. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  5. Replies: 4
    Last Post: 03-12-2006, 02:17 PM