c++ Reverse Polish Calculator Help

• 11-12-2001
knight101
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 Sub();
void Multi();
void Divide();
void SQRoot();
void PrintStack();*/
};

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

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

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

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

X.push(X.pop()+X.pop());
}

//Start Main Function
void main(){
Stack X;
Calculator Y;
char input='a';
cout<<"n: push number"<<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;
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.
• 11-12-2001
knight101
Don't know why those smiley faces are there... but those should say push, pop, and peek.
• 11-12-2001
zen
Quote:

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; }```
• 11-12-2001
Unregistered
What is the code:

using namespace std;

for?
• 11-12-2001
knight101
What is the code:

using namespace std;

for?
• 11-12-2001
zen
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;