Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RPN Calculator Using Stacks

Status
Not open for further replies.

Quiksilver89

Programmer
Mar 28, 2006
2
CA
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::push (double x)
{
stack[top] = x;
top++;
}




//Constructor 2
double STACK::pop ()
{
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;
}
 
1. Use CODE tag for snippets (see Process TGML link on the form).
2. Please, correct syntax in your sources: for example, what's the a variable in RPN::add()? You (and me;) can't debug this code: we can't compile it now...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top