Like Tree1Likes

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

This is a discussion on Help with my RPN calculator using c++ #include <stack> within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <stack> using namespace std; void main(void) { stack <int> a; int number, number2; int sentinel = ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

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

    Code:
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    void main(void)
    {
    	stack <int> a;
    	int number, number2;
    	int sentinel = -999;
    	
    	//char end = ' ';
    	char operation;
    	bool doloops = true;
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter -999 to stop the calculator and calculate the value." << endl;
    	
    	while (doloops)
    	{
    		cout << "Enter number: " <<  endl;
    		cin >> number;
    		a.push(number);
    		
    		
    		if (number == sentinel)
    			doloops = false;
    		else
    			doloops = true;
    	}
    
    	while (!a.empty())
    	{
    		cout << a.top() << " ";
    		a.pop();
    		
    	}
    	cout << endl;
    	system ("pause");
    
    }
    I am now doing a RPN calculator. I just trying doing the first part where I want user to key in at least 2 numbers before the program asked if the user want to enter a operator such as (+, -, *, /) or another number for doing the RPN calculator.Now, i not doing the operator yet but just want the user to key in the number, push it before printing the number using pop in my code. -999 is to end the program to print the number with pop using <stack>.

    Now, i got a problem, let say we need to have at least 2 numbers before it is possible to do a RPN calculator. So, I had to include 2 variables. After initialise it and user enter 2 of the numbers, I want to pop 2 of the numbers and print it using pop with <stack>. The problem is there is no problem if I just used only one variable, but instead I am using 2 variables. Because we need to have at least 2 numbers before possible to do RPN calculator. How should I solve this.

    I am thinking of doing it with array but you see, after getting 2 numbers , i want the user to either select another number or operator, so it likes it is not possible using array. So i am using only variables and repeat the whole process using while/for loops.

    Anyway, regarding the RPN calculator, is it true that if you had n numbers. n represent the amount of operand or simply called the numbers using algebra. I need to have at least (n - 1) operator?

    Example, if you had 5 operands/numbers, you need at least 4 operators such as (+ , -, / , *) and not like only 3 or less operators or more than 4 operators.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Please help me how should I do it or any suggestions about it. Many thanks. My code did shown 2 variables. But got messed up if I try to do it with 2 variables. So i only done using 1 variable and worked perfectly.
    Last edited by evildotaing; 12-20-2011 at 08:02 AM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    help!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    I will quote from wiki the solution :
    "Interpreters of Reverse Polish notation are often stack-based; that is, operands are pushed onto a stack, and when an operation is performed, its operands are popped from a stack and its result pushed back on. So the value of postfix expression is on the top of the stack. Stacks, and therefore RPN, have the advantage of being easy to implement and very fast."
    Reverse Polish notation - Wikipedia, the free encyclopedia
    If you have more question then feel free to ask..

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Ok, no error checking required. But, how do i make it such that the user could press something like 4 2 - and the program would calculate it to 2.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by evildotaing
    But, how do i make it such that the user could press something like 4 2 - and the program would calculate it to 2.
    That is just making use of reverse Polish notation directly, so you don't need the Shunting Yard algorithm. As for how: refers to the article walla linked to in post #4.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    I wonder if this question need to use classes to create objects or operator overloading?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by evildotaing
    I wonder if this question need to use classes to create objects or operator overloading?
    No, since you already have std::stack<int>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Need some hints, lets said if the user gives more than 3 numbers and using more than 1 operator(+, -, /, *). for the program to work, do I need to declare an array integer, or using while/for loops, or declare indiduval variables(which I think not possible). How about the operands. The user can input 2, 3, 4, 5, 6 numbers with different amount of operators, should I use an char array.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Why are you using Reverse Polish notation? Isn't it more intuitive for the user to enter "1 + 2" instead of "1 2 +"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    My school assignment requires to create a RPN calculator, so no choice.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by evildotaing
    My school assignment requires to create a RPN calculator, so no choice.
    Right. I understand that you have this school assignment looming over your head right now, but let's pretend that you are doing this as a hobby. What do you find interesting about RPN?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    yes, it is completely different from the normal calculations. All the numbers would followed first before the operators unlike the normal calculation.
    Example: 3 / 5+ 3 and 5 would added.
    Example: 5 6 + 3 - 2 5 would be push into stack first followed by 6 which is also push into the stack. If there are a operator, the first 2 numbers would be pop and do the calculations.

    And i also need to do character RPN. Is it possible to do it such as XYZ XY- which gives Z as the solution using stack. I heard I need operator overloading for character RPN?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by evildotaing
    5 would be push into stack first followed by 6 which is also push into the stack. If there are a operator, the first 2 numbers would be pop and do the calculations.
    Okay. Notice that you did not talk about an array. You need to be clear on what you are trying to do. Why reach for an array before you are sure you need one?

    Quote Originally Posted by evildotaing
    And i also need to do character RPN. Is it possible to do it such as XYZ XY- which gives Z as the solution using stack.
    I have never heard of "character RPN". Your example is unclear since I have no clue what is "XYZ XY-" and I don't see why it would give Z as the solution.

    Quote Originally Posted by evildotaing
    I heard I need operator overloading for character RPN?
    After having read your thread on mastering C++, and now hearing your repeated talk of operator overloading, I am starting to suspect that you are infected with C++ featuritis. The symptom is a compulsion to use every feature of C++ that the sufferer knows, just because the sufferer can
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 4 1234 LastLast
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, 02: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, 01:43 PM

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