Quiksilver89
Programmer
hey, right now im working on making an RPN calculator using a stack array. i have it running so far, but i cant get the actual calculator functions to work. im working on the first one -> void RPN::add() and i can't seem to get it working, all it does for me is output scrambled numbers/letters (ie. an empty variable).
any help would be greatly appreciated. thanks
#include<iostream.h>
#include<fstream.h>
#include"apstring.h"
#include"apvector.h"
#include"apstring.cpp"
class STACK
{
private:
int top;
double stack[20];
public:
STACK();
void push(double x);
double pop();
double current ()
{
return stack[top];
}
};
class RPN: public STACK
{
public:
RPN();
void add();
void sub();
void mult();
void div();
};
RPN::RPN()
{
}
void RPN::add()
{
double first=0, second=0, ans=0;
a.pop();
first=a.current();
a.pop();
second=a.current();
ans=first+second;
cout<<ans<<endl<<endl;
}
//Default Constructor
STACK::STACK()
{
top=0;
for(int i=0;i<20;i++)
{
stack=0;
}
}
//Constructor 1
void STACK:ush (double x)
{
stack[top] = x;
top++;
}
//Constructor 2
double STACK:op ()
{
top--;
double x = stack[top];
return x;
}
//Main
int main ()
{
int pushnum=0, pushcounter=0;
apstring choice;
apstring calcchoice;
STACK a;
RPN b;
do{
cout<<"Enter 'push' or 'pop', and 'q' once you wish to make the calculations: ";
cin>>choice;
if(choice == "q")
{
break;
}
if(choice == "pop")
{
pushcounter--;
if(pushcounter>0)
{
a.pop();
cout<<"Current number is: "<<a.current()<<endl<<endl;
}else{
cout<<"\n\nPop limit reached!"<<endl<<endl;
}
}
if(choice == "push")
{
pushcounter++;
if(pushcounter<20)
{
cout<<"\n\nEnter number to push: ";
cin>>pushnum;
a.push(pushnum);
cout<<"\n\nCurrent number is: "<<pushnum<<endl<<endl;
}else{
cout<<"\n\nPush limit reached!";
}
}
}while(choice != "q"); //----------------------------------------------------------------------
do{
cout<<"\n\nEnter 'add', 'sub', 'mult', 'div', or 'q' to quit: ";
cin>>calcchoice;
//Calculator Commands
if(calcchoice == "q")
{
break;
}
if(calcchoice == "add")
{
b.add();
}
}while(choice != "q");
system("PAUSE");
return 1;
}
any help would be greatly appreciated. thanks
#include<iostream.h>
#include<fstream.h>
#include"apstring.h"
#include"apvector.h"
#include"apstring.cpp"
class STACK
{
private:
int top;
double stack[20];
public:
STACK();
void push(double x);
double pop();
double current ()
{
return stack[top];
}
};
class RPN: public STACK
{
public:
RPN();
void add();
void sub();
void mult();
void div();
};
RPN::RPN()
{
}
void RPN::add()
{
double first=0, second=0, ans=0;
a.pop();
first=a.current();
a.pop();
second=a.current();
ans=first+second;
cout<<ans<<endl<<endl;
}
//Default Constructor
STACK::STACK()
{
top=0;
for(int i=0;i<20;i++)
{
stack=0;
}
}
//Constructor 1
void STACK:ush (double x)
{
stack[top] = x;
top++;
}
//Constructor 2
double STACK:op ()
{
top--;
double x = stack[top];
return x;
}
//Main
int main ()
{
int pushnum=0, pushcounter=0;
apstring choice;
apstring calcchoice;
STACK a;
RPN b;
do{
cout<<"Enter 'push' or 'pop', and 'q' once you wish to make the calculations: ";
cin>>choice;
if(choice == "q")
{
break;
}
if(choice == "pop")
{
pushcounter--;
if(pushcounter>0)
{
a.pop();
cout<<"Current number is: "<<a.current()<<endl<<endl;
}else{
cout<<"\n\nPop limit reached!"<<endl<<endl;
}
}
if(choice == "push")
{
pushcounter++;
if(pushcounter<20)
{
cout<<"\n\nEnter number to push: ";
cin>>pushnum;
a.push(pushnum);
cout<<"\n\nCurrent number is: "<<pushnum<<endl<<endl;
}else{
cout<<"\n\nPush limit reached!";
}
}
}while(choice != "q"); //----------------------------------------------------------------------
do{
cout<<"\n\nEnter 'add', 'sub', 'mult', 'div', or 'q' to quit: ";
cin>>calcchoice;
//Calculator Commands
if(calcchoice == "q")
{
break;
}
if(calcchoice == "add")
{
b.add();
}
}while(choice != "q");
system("PAUSE");
return 1;
}