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

  1. #31
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    doing testing on the function. I think is that is there any ways for the data types to store both the character or integers instead of using int for integers only and char for character only. This is my biggest problem.Because my function store only 2 int with a operator sign, what should I do if I had 2 operators, 3 operands, Then 3 operators, 4 operands, not possible to store all in so many functions. Seriously this is very difficult.

  2. #32
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    void calculation(int &ope1, int &ope2, char &input);
    using namespace std;
    stack <int> a;
    
    void main(void)
    {
    	bool check = false;
    	int number;
    	char ch;
    	char oper;
    	while (!check)
    	{
    		cout << "Press # for numbers: " ;
    		cout << "Press your sign for calculation." ;
    		
    		cin >> ch;
    
    		if (ch == '#')
    		{
    			cout << "Enter number; ";
    			cin >> number;
    			a.push(number);
    		}
    		else if (ch != '#')
    		{
    			
    			calculation(number, number, ch);
    			check = true;
    		}
    	}
    	cout << a.top();
    	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;
    		
    	}
    }
    Another version, trying to do something like more than 2 operators, but still give error if i try to do this 2 3 + gives 6 instead of 5.

  3. #33
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    void calculation(int &ope1, int &ope2, char &input);
    using namespace std;
    stack <int> a;
    
    void main(void)
    {
    	bool check = false;
    	int number;
    	char ch;
    	char oper;
    	while (!check)
    	{
    		cout << endl;
    		cout << "Press # for numbers: " << endl;
    		cout << "Press your sign (+, -, /, * ) for calculation." << endl ;
    		cout << "press '=' to print the calculation." ;
    		cin >> ch;
    
    		if (ch == '#')
    		{
    			cout << "Enter number; ";
    			cin >> number;
    			a.push(number);
    		}
    		else if (ch != '#')
    		{
    			
    			calculation(number, number, ch);
    			if (ch == '=')
    			{
    				check = true;
    			}
    			else if (ch != '=')
    			{
    				check = false;
    			}
    		}
    	}
    	cout << "Your value is " << a.top() ;
    	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;
    		
    	}
    }
    Another version. Able to input numbers, operators. (no error checking. ) But the calculation completely wrong. Where is the wrong part.

  4. #34
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Where are all your pop() calls?

    The whole idea of RPN is that
    a) when you see a number, you push it onto the stack
    b) when you see an operator, you pop some arguments, calculate and push the result.
    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.

  5. #35
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    void calculation(int &ope1, int &ope2, char &input);
    using namespace std;
    stack <int> a;
    
    int main()
    {
    	bool check = false;
    	int number;
    	char ch;
    	char oper;
    	while (!check)
    	{
    		cout << endl;
    		cout << "Press # for input numbers: " << endl;
    		cout << "Press your sign (+, -, /, * ) for calculation." << endl ;
    		cout << "press '=' to print the calculation." ;
    		cin >> ch;
    
    		if (ch == '#')
    		{
    			cout << "Enter number; ";
    			cin >> number;
    			a.push(number);
    
    			
    		}
    		else if (ch != '#')
    		{
    			
    			calculation(number, number, ch);
    			if (ch == '=')
    			{
    				check = true;
    			}
    			else if (ch != '=')
    			{
    				check = false;
    			}
    		}
    	}
    	cout << "Your value is " << " " << a.top() ;
    	system ("pause");
    	return 0;
    }
    	
    void calculation(int &oper1, int &oper2, char &chr)
    {
    	switch (chr)
    	{
    		a.pop();
    		case '+':
    			a.push(oper1 + oper2);
    			//a.pop();
    			break;
    		case '-':
    			a.push(oper1 - oper2);
    			//a.pop();
    			break;
    		case '*':
    			a.push(oper1 * oper2);
    			//a.pop();
    			break;
    		case '/':
    			a.push(oper1 / oper2);
    			//a.pop();
    			break;
    		
    	}
    	
    }
    Added a pop() calls but still the answer remain the same. Just wonder how on earth the calculation always give me very funny value.

  6. #36
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    pop()
    Hey, where did that value go?

    Did you even read my previous post, or did you just gloss over it and throw in some pop statements for the hell of it.

    Perhaps if you made your function
    calculation(ch);
    it would force you to THINK about where the operands are coming from.
    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.

  7. #37
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    How to do it such that the program knows see the number, it would be push. If it is a charcter, the program would pop the argument,
    Code:
    int number;
    char ch;
    
    while (!cin >> ch) //how to code
    	{
    		cin >> number;
    	}
    Each variables have different data types, not possible to check if it was a char or int. Cant do like (while !ch) Cant possibly check the variables using cin
    Last edited by evildotaing; 01-03-2012 at 02:42 AM.

  8. #38
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    calculation(ch);
    if i do this, how am i going to do the calculation without the 2 value

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by evildotaing
    Each variables have different data types, not possible to check if it was a char or int.
    Read as a string, then check and parse.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Thought string only use for words, sentence

  11. #41
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Look at my post#35, i try to do the other way by input # if the user want to include a number, and the operator sign for calculation, it gives funny value.

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by evildotaing
    Thought string only use for words, sentence
    Well, you will only be reading as a string. After parsing, you still have numbers to work with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #43
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Quote Originally Posted by laserlight View Post
    Well, you will only be reading as a string. After parsing, you still have numbers to work with.
    cin >> string
    If lets say user press 6 4 + in a string

    How to check if the string that was input is the number or charcters, because string is grouped together.
    [code]

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You'll be reading 3 tokens as string: "6", "4" and "+". One approach is to check the first character of the string: '6' is not '+', '-' or some other operator, so you attempt to parse it as a number, e.g., as an int. The parsing succeeds, so you store 6 in the stack, and move on to "4".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #45
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    #include <string>
    
    void calculation(int &ope1, int &ope2, char &input);
    using namespace std;
    stack <int> a;
    
    int main()
    {
    	string post;
    
    	cout << "Enter your expression: " << endl;
    	
    	cin >> post;
    	getline(cin , post);
    	
    	cout << post;
    	system ("pause");
    }
    Should I do it like asking user to enter any input in a string." Or should I use character arrays.

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