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

  1. #16
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Quote Originally Posted by laserlight View Post
    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?


    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.


    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
    This is from my school question. Other than doing numbers, i also need to do the character. They specify to use operator overloading for the charcter RPN. Only need + and - for operator in character example:
    Normal
    XYZ+ A = XYZA
    XYZ - X =YZ
    RPN mode
    XYZ A = XYZA
    XYZ X - = YZ

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does + and - mean? You need to be sure of that.

    But before you jump into that, just concentrate on dealing with the numbers.
    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

  3. #18
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    just normal add and minus

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by evildotaing
    just normal add and minus
    I am talking about the character thing. There is no "normal add and minus" here.
    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

  5. #20
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Example:
    AB C + = ABC(if C is added to AB, it would add into ABC
    BEF F - = BE(if F is minus from BEF, irt become BE
    ABD D += ABD(if D is added while there are already a 'D' letter, would remain the same)

    Like adding characters, or removing. I would do the numbers RPN calculators first before the charcters.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by evildotaing
    Like adding characters, or removing.
    Rubbish. I don't want to be harsh, but you cannot just have a vague notion of what you are trying to do here. You need to be clear.

    Examples are great to illustrate, but unless your examples are exhaustive, they are inadequate compared to a formal description of what you are trying to do.

    Let me consider the examples that you have given, save the last one, loosely expressed in RPN:

    XYZ XY - = Z
    XYZ A + = XYZA
    XYZ X - = YZ
    AB C + = ABC
    BEF F - = BE

    Now, if I were shown ABD D + I would state:
    ABD D + = ABDD
    which turns out is wrong.

    Your last example shows that the characters form a set, but maybe order is important. We are not told. Originally, I thought that + is the string concatenation operator, but maybe it is actually the set union operator, or maybe...

    Again, you need to be clear on what you are doing.

    My suggestion is this: forget about the characters for the time being. Concentrate on implementing a basic algorithm for computing a numeric expression expressed in RPN.
    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

  7. #22
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    void main(void)
    {
    	bool expression;
    	char ch;
    	stack<int> a;
    	int op1, op2;
    
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter expression: " << endl;
    	while (true)
    	{
    		cin >> op1;
    		cin >> op2;
    		cin >> ch;
    		a.push(op1);
    		a.push(op2);
    	
    		switch (ch)
    		{
    		case '+':
    			stack.pop(op1 + op2);
    			break;
    		case '-':
    			stack.pop(op1 - op2);
    			break;
    		case '*':
    			stack.pop(op1 * op2);
    			break;
    		case '/':
    			stack.pop(op1 / op2);
    			break;
    		}
    
    		cout << a.top() << " ";
    	}
    	system ("pause");
    }
    1>------ Build started: Project: c++ assignment 2, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(70) : error C2955: 'std::stack' : use of class template requires template argument list
    1> c:\program files\microsoft visual studio 9.0\vc\include\stack(18) : see declaration of 'std::stack'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(70) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(70) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(73) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(73) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(76) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(76) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(79) : error C2143: syntax error : missing ';' before '.'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(79) : error C2143: syntax error : missing ';' before '.'
    1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\c++ assignment 2\c++ assignment 2\Debug\BuildLog.htm"
    1>c++ assignment 2 - 9 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Doing numbers RPN first. But encounter error. Why the program keep asking me to input the missing ; when I had already added. The switch structure should be correct ma?

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It would be easier to determine what is going on if your error messages matched the code you provided. Most of the errors are for lines which do not exist in the posted code.

    Jim

  9. #24
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    void main(void)
    {
    	bool expression;
    	char ch;
    	stack<int> a;
    	int op1, op2;
    
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter expression: " << endl;
    	while (true)
    	{
    		cin >> op1;
    		cin >> op2;
    		cin >> ch;
    		a.push(op1);
    		a.push(op2);
    	
    		switch (ch)
    		{
    		case '+':
    			a.push(op1 + op2);
    			break;
    		case '-':
    			a.push(op1 - op2);
    			break;
    		case '*':
    			a.push(op1 * op2);
    			break;
    		case '/':
    			a.push(op1 / op2);
    			break;
    		
    			
    			
    		}
    		//while (!a.empty())
    		//{
    			cout << "Your expression is " <<  a.top() << endl;
    			//a.pop();
    		//}
    		
    	}
    	system ("pause");
    }
    Nvm, i solve it, but only 2 variables and a operator allowed . No error checking. What should I do if I want to have more than 2 variables for the calculation, should I use array? And what should I do for the subaequent numbers. Anyway the program works only if the user press 2 numbers with a operator. I want to increase that.

  10. #25
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    void main(void)
    {
    	bool expression;
    	char ch;
    	char done;
    	stack<int> a;
    	int op1, op2;
    
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter expression: " << endl;
    	while (true)
    	{
    		cin >> op1;
    		cin >> op2;
    		cin >> ch;
    		a.push(op1);
    		a.push(op2);
    	
    		switch (ch)
    		{
    		case '+':
    			a.push(op1 + op2);
    			break;
    		case '-':
    			a.push(op1 - op2);
    			break;
    		case '*':
    			a.push(op1 * op2);
    			break;
    		case '/':
    			a.push(op1 / op2);
    			break;
    		
    		}
    		cout << "Press = when you had done" << endl;
    		cin >> done;
    		if (done = '=')
    		{
    			cout << "Your expression is " <<  a.top() << endl;
    		}
    		
    	}
    	system ("pause");
    }
    I want to make it like if the user press '=', the program would output the answer. But somehow, this code cant work if i press '='. Instead i manage to get the answer if i press 'Enter' in the keyboard.

  11. #26
    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;
    	char ch;
    	char done;
    	
    	int op1, op2;
    
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter expression: " << endl;
    	while (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;
    		
    	}
    }
    Using function. I still not sure how to do if I had more than 2 umbers and 1 operator. Helpppppppppppppppppppppppppppp!!!!!!!!!!!!!!!!!!! !!!!!!

  12. #27
    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, op2;
    
    	cout << "Reverse Polish Notation : " << endl;
    	cout << "Enter expression: " << endl;
    	while (!expression)
    	{
    		cin >> op1;
    		cin >> op2;
    		cin >> ch;
    		
    		calculation(op1, op2, ch);
    		if (!cin)
    			expression = false;
    		else
    		{
    			expression = true;
    			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;
    		
    	}
    }
    latest code: I still cant figure out what should I do> I want to make it like the function would do the calculations for every numbers that was pushed. Still, i manage to solve it with only operands. How to do it when I want to have more than 3 operands and the function to check all the calculation. No error checking required.

    If i try to add more than 2 numbers(eg, 3 numbers with 2 operands), the function would only calculate the first 2 operands and the operator(No error in program). Not sure how to do on the subsequent operands and operators
    Last edited by evildotaing; 01-01-2012 at 10:20 AM.

  13. #28
    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.

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

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why is line 23 a copy/paste of line 22?
    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.

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