Posted By

DrPepper on 09/09/11


Tagged


Versions (?)

sequence2.h


 / Published in: C++
 

  1. // NAME : Ryutaro Matsuda
  2. // FILE: sequence1.h
  3. // CLASS PROVIDED: sequence (part of the namespace CISP430_A2)
  4. // There is no implementation file provided for this class since it is
  5. // an exercise from Section 3.2 of "Data Structures and Other Objects Using C++"
  6. //
  7. // TYPEDEFS and MEMBER CONSTANTS for the sequence class:
  8. // typedef ____ value_type
  9. // sequence::value_type is the data type of the items in the sequence. It
  10. // may be any of the C++ built-in types (int, char, etc.), or a class with a
  11. // default constructor, an assignment operator, and a copy constructor.
  12. //
  13. // typedef ____ size_type
  14. // sequence::size_type is the data type of any variable that keeps track of
  15. // how many items are in a sequence.
  16. //
  17. // enum { CAPACITY = 30 };
  18. // CAPACITY is the maximum number of items that a sequence can hold.
  19. //
  20. // CONSTRUCTOR for the sequence class:
  21. // sequence(size_type entry=CAPACITY );
  22. // Postcondition: The sequence has been dynamic allocate memories.
  23. //
  24. //COPY CONSTRUCTOR
  25. // sequence(const sequence& entry)
  26. // Postcondition: The sequence has been created by copying from an existing sequence.
  27. //
  28. // MODIFICATION MEMBER FUNCTIONS for the sequence class:
  29. // void start( )
  30. // Postcondition: The first item on the sequence becomes the current item
  31. // (but if the sequence is empty, then there is no current item).
  32. //
  33. // void advance( )
  34. // Precondition: is_item returns true.
  35. // Postcondition: If the current item was already the last item in the
  36. // sequence, then there is no longer any current item. Otherwise, the new
  37. // current item is the item immediately after the original current item.
  38. //
  39. // void insert(const value_type& entry)
  40. // Precondition: size( ) < CAPACITY. if this is not true then increase the capacity by 10%
  41. // Postcondition: A new copy of entry has been inserted in the sequence
  42. // before the current item. If there was no current item, then the new entry
  43. // has been inserted at the front of the sequence. In either case, the newly
  44. // inserted item is now the current item of the sequence.
  45. //
  46. // void attach(const value_type& entry)
  47. // Precondition: size( ) < CAPACITY.if this is not true then increase the capacity by 10%
  48. // Postcondition: A new copy of entry has been inserted in the sequence after
  49. // the current item. If there was no current item, then the new entry has
  50. // been attached to the end of the sequence. In either case, the newly
  51. // inserted item is now the current item of the sequence.
  52. //
  53. // void remove_current( )
  54. // Precondition: is_item returns true.
  55. // Postcondition: The current item has been removed from the sequence, and the
  56. // item after this (if there is one) is now the new current item.
  57. //
  58. //Operator= overloading
  59. // void sequence::operator =(const sequence&);
  60. // Postcondition: The l-value's sequence object is copy to the r-value
  61. //
  62. // CONSTANT MEMBER FUNCTIONS for the sequence class:
  63. // size_type size( ) const
  64. // Postcondition: The return value is the number of items in the sequence.
  65. //
  66. // bool is_item( ) const
  67. // Postcondition: A true return value indicates that there is a valid
  68. // "current" item that may be retrieved by activating the current
  69. // member function (listed below). A false return value indicates that
  70. // there is no valid current item.
  71. //
  72. // value_type current( ) const
  73. // Precondition: is_item( ) returns true.
  74. // Postcondition: The item returned is the current item in the sequence.
  75. // Destructor
  76. // ~sequence()
  77. // Postcondition: release the memory to the heap
  78. //
  79. // VALUE SEMANTICS for the sequence class:
  80. // Assignments and the copy constructor may be used with sequence objects.
  81. //
  82. //void resize(size_type new_capacity )
  83. //Precondition: new_capacity > used
  84. // Postcondition: new space allocated and old space released
  85.  
  86.  
  87. #ifndef SEQUENCE_H
  88. #define SEQUENCE_H
  89. #include <cstdlib> // Provides size_t
  90.  
  91. namespace CISP430_A2
  92. {
  93. class sequence
  94. {
  95. public:
  96. // TYPEDEFS and MEMBER CONSTANTS
  97. typedef double value_type;
  98. typedef size_t size_type;
  99. enum { CAPACITY = 30 };
  100. // CONSTRUCTOR
  101. sequence(size_type entry=CAPACITY );
  102. // COPY CONSTRUCTOR
  103. sequence(const sequence& entry) ;
  104. // Library facilities used: cstdlib
  105. // MODIFICATION MEMBER FUNCTIONS
  106. void start( );
  107. void advance( );
  108. void insert(const value_type& entry);
  109. void attach(const value_type& entry);
  110. void remove_current( );
  111. void resize(size_type );
  112. void sequence::operator =(const sequence&);
  113. // CONSTANT MEMBER FUNCTIONS
  114. size_type size( ) const;
  115. bool is_item( ) const;
  116. value_type current( ) const;
  117. //Destructor
  118. ~sequence() ;
  119. private:
  120. value_type *data;
  121. size_type used;
  122. size_type capacity;
  123. size_type current_index;
  124. };
  125. }
  126.  
  127. #endif

Report this snippet  

You need to login to post a comment.