Thread: Help with my RPN calculator using c++ #include <stack>

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    
    void calculation(int, int, char);
    using namespace std;
    stack<int> a;
    
    void main(void)
    {
    	
    	bool expression = false;
    	char ch;
    	char done;
    	
    	int op1;
    
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter expression: " << endl;
    	while (!expression)
    	{
    		cin >> op1;
    		cin >> op1;
    		cin >> ch;
    		
    		calculation(op1, op1, ch);
    		if (!cin)
    			expression = false;
    		else
    		{
    			expression = true;
    			//cin >> op1;
    			//cin >> op2;
    			//cin >> ch;
    			//calculation(op1, op2, ch);
    		}
    	}
    	cout << "Your expression is " <<  a.top() << endl;
    	system ("pause");
    }
    
    void calculation(int oper1, int oper2, char chr)
    {
    	switch (chr)
    	{
    		case '+':
    			a.push(oper1 + oper2);
    			break;
    		case '-':
    			a.push(oper1 - oper2);
    			break;
    		case '*':
    			a.push(oper1 * oper2);
    			break;
    		case '/':
    			a.push(oper1 / oper2);
    			break;
    		
    	}
    }
    Guess what, if I let the both variables be op1, then if calculate: why that when i add 3 3 + ; no problem encounter it gives 6.
    When i add 2 3 +, it gives 6 instead of 5, i dun understand the logic and error.
    When i add 1 2+, it gives 4.
    Last edited by evildotaing; 01-01-2012 at 10:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me to write a calculator in C++ using stack?
    By anglin1024 in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2009, 08:12 AM
  2. Cannot open include file: 'stack.h'
    By GSalah in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2007, 03:18 PM
  3. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. Stack Calculator Error
    By shane1985 in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2003, 02:43 PM