1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
// FILE: sequence1.h
// CLASS PROVIDED: sequence (part of the namespace main_savitch_3)
// There is no implementation file provided for this class since it is
// an exercise from Section 3.2 of "Data Structures and Other Objects Using C++"
//
// TYPEDEFS and MEMBER CONSTANTS for the sequence class:
// typedef ____ value_type
// sequence::value_type is the data type of the items in the sequence. It
// may be any of the C++ built-in types (int, char, etc.), or a class with a
// default constructor, an assignment operator, and a copy constructor.
//
// typedef ____ size_type
// sequence::size_type is the data type of any variable that keeps track of
// how many items are in a sequence.
//
// static const size_type CAPACITY = _____
// sequence::CAPACITY is the maximum number of items that a sequence can hold.
//
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib> // Provides size_t
#include <cassert> //for assert function
#include <iostream> //for test function
using namespace std;
namespace main_savitch_3
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
sequence();
// MODIFICATION MEMBER FUNCTIONS
void start();
void advance();
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current();
// CONSTANT MEMBER FUNCTIONS
size_type size() const { return used; }
bool is_item() const;
value_type current() const;
void test() const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
sequence::sequence()
{
used = 0;
current_index = 0;
}
// void insert(const value_type& entry)
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of entry has been inserted in the sequence
// before the current item. If there was no current item, then the new entry
// has been inserted at the front of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
void sequence::insert(const value_type& entry)
{
assert(size() < CAPACITY);
for (size_type i = used; i > current_index; i--)
{
data[i] = data[i - 1];
}
data[current_index] = entry;
used++;
}
// void start( )
// Postcondition: The first item on the sequence becomes the current item
// (but if the sequence is empty, then there is no current item).
void sequence::start()
{
current_index = 0;
}
// value_type current( ) const
// Precondition: is_item( ) returns true.
// Postcondition: The item returned is the current item in the sequence.
sequence::value_type sequence::current() const
{
assert(is_item());
return data[current_index];
}
// void advance( )
// Precondition: is_item returns true.
// Postcondition: If the current item was already the last item in the
// sequence, then there is no longer any current item. Otherwise, the new
// current item is the item immediately after the original current item.
void sequence::advance()
{
assert(is_item());
current_index++;
}
//bool is_item() const
// Postcondition: A true return value indicates that there is a valid "current" item that
// may be retrieved by the current member function (listed below). A false return value
// indicated that there is no current item.
bool sequence::is_item() const
{
bool found = false;
if (current_index < used)
found = true;
return found;
//The below code is an alternative
//return (current_index < used);
}
// void attach(const value_type& entry)
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of entry has been inserted in the sequence after
// the current item. If there was no current item, then the new entry has
// been attached to the end of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
void sequence::attach(const value_type& entry)
{
assert(size() < CAPACITY);
if (!is_item())
current_index = used - 1;
else
{
for (size_type i = used; i > current_index + 1; --i)
data[i] = data[i - 1];
}
current_index++;
data[current_index] = entry;
used++;
}
// void remove_current( )
// Precondition: is_item returns true.
// Postcondition: The current item has been removed from the sequence, and the
// item after this (if there is one) is now the new current item.
void sequence::remove_current()
{
assert(is_item());
for (size_type i = current_index + 1; i < used; ++i)
data[i - 1] = data[i];
used--;
if(used==0)
current_index=used;
}
void sequence::test() const
{
cout << "The current index is: " << current_index << endl;
cout << "{ ";
for (size_t i = 0; i < used; i++) //used is of type size_t which the computer reads as an unsigned integer
cout << data[i] << ", ";
cout << "} used = " << used << endl;
}
}
#endif
|