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;
}