Thread: c++ Reverse Polish Calculator Help

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Question c++ Reverse Polish Calculator Help

    I need some help finishing my Reverse Polish Calculator. We have to have two classes, one for the stack of doubles that you will use for performing the operations and keeping track of the results... the other for the calculator that prompts for input, works with the stack and outputs the answers. Menu Functions... n (push number), + (add), - (subtract), * (multiply), / (divide), s (square root), p (print stack), x (exit).

    This is the code I have so far:

    #include <iostream>
    #include <math.h>

    //Creation of Class Stack
    class Stack{
    public:
    void push(double x);
    bool empty();
    double pop();
    double peek();
    Stack();

    private:
    double array[1000];
    int top;
    };

    //Sets the top of the Stack to zero
    Stack::Stack(){
    top=0;
    }

    //Creation of Class Calculator
    class Calculator{
    public:
    void Add(Stack X);
    /*void Sub();
    void Multi();
    void Divide();
    void SQRoot();
    void PrintStack();*/
    };

    //Pushes the Stack
    void Stack:ush(double x){
    cin>>top;
    top++;
    }

    //Pops the Stack
    double Stack:op(){
    top--;
    return array[top];
    }

    //Peeks (or looks) at the Stack's Top Value
    double Stack:eek(){
    return array[top-1];
    }

    //Bool function to check for empty Stack
    bool Stack::empty(){
    if ((top-1)==-1){
    return true;}
    else{
    return false;}
    }

    //Add Function
    void Calculator::Add(Stack X){
    X.push(X.pop()+X.pop());
    }


    //Start Main Function
    void main(){
    Stack X;
    Calculator Y;
    char input='a';
    cout<<"n: push number"<<endl;
    cout<<"+: add"<<endl;
    cout<<"-: subtract"<<endl;
    cout<<"*: multiply"<<endl;
    cout<<"/: divide"<<endl;
    cout<<"s: square root"<<endl;
    cout<<"p: print stack"<<endl;
    cout<<"x: exit"<<endl;
    while(input!='x'){
    cout<<"Option(n, +, -, *, /, s, p, x): ";
    cin>>input;
    switch(input){
    case 'n': X.push(x);
    break;
    case '+': Y.Add(Call);
    break;
    case '-': //Y.Sub(Stack Call);
    break;
    case '*': //Y.Multi(Stack Call);
    break;
    case '/': //Y.Divide(Stack Call);
    break;
    case 's': //Y.SQRoot(Stack Call);
    break;
    case 'p': //Y.PrintStack(Stack Call);
    break;
    case 'x':
    cout<<"The code had failed. Only a blue screen flashed itself mockingly at the user. Goodbye."
    ;
    break();
    default:
    cout<<"Error checking... gotta love it."<<endl;}
    }}

    Thanks for all your help.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Don't know why those smiley faces are there... but those should say push, pop, and peek.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I need some help finishing my Reverse Polish Calculator.
    I think you mean starting -

    Code:
    #include <iostream> 
    #include <cmath> 
    
    using namespace std;
    
    //Creation of Class Stack 
    class Stack{ 
    public: 
    	void push(double x); 
    	bool empty(); 
    	double pop(); 
    	double peek(); 
    	Stack(); 
    
    private: 
    	double array[1000]; 
    	int top; 
    }; 
    
    //Sets the top of the Stack to zero 
    Stack::Stack(){ 
    	top=0; 
    } 
    
    //Creation of Class Calculator 
    class Calculator{ 
    public: 
    	void Add(Stack& X); 
    	void Multi(Stack& X);
    	/*void Sub(); 
    	 
    	void Divide(); 
    	void SQRoot(); */
    	void PrintStack(Stack&  X); 
    }; 
    
    //Pushes the Stack 
    void Stack:: push(double x){ 
    	
    	if(top<9999)
    		array[top++]=x; 
    } 
    
    //Pops the Stack 
    double Stack:: pop(){ 
    	if(top>0)
    		return array[--top]; 
    	else
    		return 0;
    } 
    
    //Peeks (or looks) at the Stack's Top Value 
    double Stack:: peek(){
    	if(top>0)
    		return array[top-1]; 
    	else
    		return 0;
    } 
    
    //Bool function to check for empty Stack 
    bool Stack::empty(){ 
    	if (top-1==-1) 
    		return true; 
    	else 
    		return false; 
    } 
    
    //Add Function 
    void Calculator::Add(Stack&  X){ 
    	double x=X.pop();
    	double y=X.pop();
    	X.push(x+y); 
    } 
    
    void Calculator::Multi(Stack& X){
    	double x=X.pop();
    	double y=X.pop();
    	X.push(x*y);
    }
    void Calculator::PrintStack(Stack& X)
    {
    	cout << X.pop();
    }
    
    
    //Start Main Function 
    int main(){ 
    	Stack X; 
    	Calculator Y; 
    	char input='a'; 
    	cout<<"n: push number"<<endl; 
    	cout<<"+: add"<<endl; 
    	cout<<"-: subtract"<<endl; 
    	cout<<"*: multiply"<<endl; 
    	cout<<"/: divide"<<endl; 
    	cout<<"s: square root"<<endl; 
    	cout<<"p: print stack"<<endl; 
    	cout<<"x: exit"<<endl; 
    	while(input!='x'){ 
    		cout<<"Option(n, +, -, *, /, s, p, x): "; 
    		cin>>input; 
    		switch(input){ 
    		case 'n': 
    			{
    				cout << "Enter Number: ";
    				double z=0;
    				cin>>z;
    				X.push(z); 
    			}
    			break; 
    		case '+': Y.Add(X); 
    			break; 
    		case '-': //Y.Sub(Stack Call); 
    			break; 
    		case '*': Y.Multi(X); 
    			break; 
    		case '/': //Y.Divide(Stack Call); 
    			break; 
    		case 's': //Y.SQRoot(Stack Call); 
    			break; 
    		case 'p': Y.PrintStack(X); 
    			break; 
    		case 'x': 
    		cout<<"Goodbye."; 
    			break; 
    		default: 
    		cout<<"Error checking... gotta love it."<<endl;} 
    	}
    
    	return 0;
    }
    zen

  4. #4
    Unregistered
    Guest
    What is the code:

    using namespace std;

    for?

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    What is the code:

    using namespace std;

    for?

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The C++ standard requires the use of the new style headers, which include namespaces, which prevent name clashes. using namespace XXX, brings the XXX namespace into global scope, allowing use of any of the functions/classes/objects contained within this namespace to be used without qualification. Alternatively, you could do

    using std::cout;
    using std::cin;
    //etc

    for each function/class/object you're using, or qualify the function/class/object before each use -

    std::cout<<"n: push number"<<std::endl;
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Impix or reverse polish notation
    By P4R4N01D in forum C Programming
    Replies: 10
    Last Post: 11-18-2008, 11:42 PM
  2. Postfix (reverse polish notation) calculator
    By ottomated in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:32 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. infix vs Reverse Polish notation
    By dionys in forum C Programming
    Replies: 1
    Last Post: 05-10-2004, 02:25 PM
  5. Reverse Polish Calculator
    By BlasterStar in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 11:12 PM