storing abstract data on a stack?

This is a discussion on storing abstract data on a stack? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a calculator that gets its input by parsing a single line expression that includes numbers and ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    5

    storing abstract data on a stack?

    I'm trying to write a calculator that gets its input by parsing a single line expression that includes numbers and their operators, like 1+2-3

    The number and operator will both be stored in a class derived from the abstract class Token, one called Number and the other Operator. How do I make a stack hold a Token when Token is abstract? I can declare a Token stack[SIZE]; but what will the push and pop functions look like?

    Code:
    const int SIZE=10;
    class stack
    {
      int head;
      Token stk[SIZE];
    
       public:
        //init etc
        void push(Token* var) //doesn't work!
        {stk[++head] = *var;}
        Token* pop()
        {return stk[head--];}
    };

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How are you planning on storing your numbers? As actual numbers, or as strings? If it's just as strings, use the same storage for both the operator and the number, but do something different with them when the need arises.

    If it's going to be something other than a string, consider making an enumerated type for your operators, and storing them as an array or what not of those.
    Code:
    enum operators { op_empty, op_plus, op_minus, ... };
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    5
    I'm storing them as floating point numbers. Here's a bit of the code:

    Code:
    class Token
    {
      public:
        virtual float getNumber()=0;
        virtual char getOperator()=0;
    };
    
    class Operator:public Token
    {
        char op;
      public:
        Operator(char ch) {op=ch;}
        char getOperator();
        float getNumber();
    };
    
    class Number:public Token
    {
        float fnum;
      public:
        Number(float fn) {fnum=fn;}
        float getNumber();
        char getOperator();
    };

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I think you want to change
    Code:
    Token stk[SIZE];
    to
    Code:
    Token*[SIZE];
    and
    Code:
    {stk[++head] = *var;}
    to
    Code:
    {stk[++head] = var;}
    (You're not storing the data on this stack, just pointers to the data.)
    Last edited by R.Stiltskin; 02-05-2005 at 08:45 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    I think you create a base class from which Operator and Number are derived.
    Then you would use a stack of those base types.

    In C terms, you would use a union.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  4. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 09:59 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21