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; doing testing on the function. I think is that is there any ways for the data types to store both ...

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

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    iMalc likes this.
    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.

  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 01: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
    21,448
    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.
    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. #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
    21,448
    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.
    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

  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
    21,448
    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".
    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

  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.

Page 3 of 4 FirstFirst 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