Stack Postfix Calculator

This is a discussion on Stack Postfix Calculator within the C++ Programming forums, part of the General Programming Boards category; Can anybody tell me what's wrong with this? I get an answer but its wrong. For example 23+ gives me ...

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

    Stack Postfix Calculator

    Can anybody tell me what's wrong with this? 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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Please don't repost..
    Stack Postfix Calculator
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Postfix Calculator
    By programminfool in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2012, 10:09 PM
  2. Help with Postfix Calculator
    By Bruins77 in forum C Programming
    Replies: 2
    Last Post: 02-07-2012, 01:21 PM
  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, 09:17 AM
  5. Replies: 4
    Last Post: 03-12-2006, 02:17 PM

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