Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,633 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,056 people online right now. Registration is fast and FREE... Join Now!




RPN Calculator Using Stacks

 
Reply to this topicStart new topic

RPN Calculator Using Stacks, Inherited Classes

Quiksilver
post 28 Mar, 2006 - 09:10 PM
Post #1


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


hey, right now im working on making an RPN (Reverse Polish Notation) 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

CODE

#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()
{
   STACK a;
   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[i]=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;
}


This post has been edited by Dark_Nexus: 29 Mar, 2006 - 09:14 AM
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 29 Mar, 2006 - 01:27 PM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


CODE

void RPN::add()
{
  STACK a;
  double first=0, second=0, ans=0;
  a.pop();
  first=a.current();
  a.pop();
  second=a.current();
  ans=first+second;
  cout<<ans<<endl<<endl;
}


variable a of type STACK is never given any other values than what the constructor initializes. This is where the problem comes from. Try pushing a few numbers in the stack before using it. Also make a isEmptry() function which you could use to check if the stack is empty.

This post has been edited by Mrafcho001: 29 Mar, 2006 - 01:28 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 04:01AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month