Reliable Software Logo

C++ In Action: Language

Exercise Solutions

  1. Top
    int IStack::Top () const
    {
        assert (_top > 0);
        return _arr [_top - 1];
    }
    
    int IStack::Count () const
    {    return _top;
    }
    						
  2. This example calls Pop on an empty stack, in violation of the contract.
    int main ()
    {
        IStack stack;
        stack.Pop (); 
    }
  3. This is the interface of the stack of characters:
    class CharStack
    {
    public:
        CharStack () :_top (0) {}
        void Push (char c);
        char Pop ();
        char Top () const;
        int Count () const;
    private:
        char _arr [maxStack];
        int  _top;
    };
    This is how you reverse a string using a stack of characters:
    int main ()
    {
        CharStack stack;
        char str [] = "esreveR";
        for (int i = 0; str [i] != '\0'; ++i)
            stack.Push (str [i]);
        while (stack.Count () > 0)
            std::cout << stack.Pop ();
        std::cout << std::endl;
    }
  4. Queue
    #include <iostream>
    #include <cassert>
    
    const int maxPuts = 8;
    
    class Queue
    {
    public:
        Queue ();
        double Get ();
        void Put (double x);
    private:
        double    _arr [maxPuts];
        int        _putIdx;
        int        _getIdx;
    };
    
    Queue::Queue ()
    : _putIdx (0),
    _getIdx (0)
    {}
    
    double Queue::Get ()
    {
        assert (_getIdx < _putIdx);
        ++_getIdx;
        return _arr [_getIdx - 1];
    }
    
    void Queue::Put (double x)
    {
        assert (_putIdx < maxPuts);
        _arr [_putIdx] = x;
        ++_putIdx;
    }
    
    int main ()
    {
        Queue queue;
        queue.Put (0.1);
        queue.Put (0.2);
        std::cout << "Getting: " << queue.Get () << ", ";
        std::cout << queue.Get () << std::endl;
        queue.Put (0.3);
        std::cout << "Getting more: " << queue.Get () << std::endl; 
    }
    						
  5. Array of double
    #include <iostream>
    #include <cassert>
    
    const int maxCells = 16;
    
    class DblArray
    {
    public:
        DblArray ();
        void Set (int i, double val);
        double Get (int i) const;
        bool IsSet (int i) const;
    private:
        double    _arr [maxCells];
        bool    _isSet [maxCells];
    };
    
    DblArray::DblArray ()
    {
        for (int i = 0; i < maxCells; ++i)
            _isSet [i] = false;
    }
    
    void DblArray::Set (int i, double val)
    {
        assert (i < maxCells);
        assert (!IsSet (i));
        _arr [i] = val;
        _isSet [i] = true;
    }
    
    double DblArray::Get (int i) const
    {
        assert (i < maxCells);
        assert (_isSet [i]);
        return _arr [i];
    }
    
    bool DblArray::IsSet (int i) const
    {
        assert (i < maxCells);
        return _isSet [i];
    }
    
    int main ()
    {
        DblArray arr;
        arr.Set (2, 0.2);
        arr.Set (4, 0.4);
        std::cout << "IsSet (0) returns " << arr.IsSet (0) << std::endl;
        std::cout << "IsSet (2) returns " << arr.IsSet (2) << std::endl;
        std::cout << "Get (2) returns " << arr.Get (2) << std::endl;
        std::cout << "Get (4) returns " << arr.Get (4) << std::endl;
    }