Advertisement
Guest User

implementation file

a guest
Feb 11th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include "sequence2.h"
  2.  
  3. using namespace CISP430_A2;
  4. using namespace std;
  5.  
  6. typedef double value_type;
  7. typedef size_t size_type;
  8.  
  9. // COPY CONSTRUCTOR
  10. sequence::sequence(const sequence& entry)
  11. {
  12.     for(int i=0;i<capacity;i++)
  13.         data[i]=entry.data[i];
  14.     used=entry.used;
  15.     capacity=entry.capacity;
  16.     current_index=entry.current_index;
  17. }
  18. // Library facilities used: cstdlib
  19. // MODIFICATION MEMBER FUNCTIONS
  20. void sequence::start()
  21. {
  22.     current_index = 0;
  23. }
  24. void sequence::advance()
  25. {
  26.     if (is_item()==true)
  27.         current_index += 1;
  28. }
  29. void sequence::insert(const value_type& entry)
  30. {
  31.     if (current_index==0)
  32.     {
  33.         for(int i=capacity;i>=0;i--)
  34.             data[i]=data[i-1];
  35.     }
  36.  
  37.     if (size() == 1)
  38.         data[current_index]= entry;
  39.     else if(size() < CAPACITY)
  40.         data[current_index-1] = entry;
  41.     else
  42.         capacity*=1.1;
  43.     ++used;
  44. }
  45. void sequence::attach(const value_type& entry)
  46. {
  47.     if (size() == 1)
  48.         data[current_index]= entry;
  49.     else if(size() < CAPACITY)
  50.         data[current_index+1] = entry;
  51.     else
  52.         capacity*=1.1;
  53.     ++used;
  54. }
  55. void sequence::remove_current()
  56. {
  57.     if (is_item()==true)
  58.     {
  59.         for (int i=current_index+1;i<used;++i)
  60.             data[i-1]=data[i];
  61.         --used;
  62.     }
  63. }
  64. void sequence::resize(size_type new_capacity)
  65. {
  66.     if (new_capacity > size())
  67.         capacity = new_capacity;
  68. }
  69. void sequence::operator =(const sequence& entry)
  70. {
  71.     for(int i=0;i<capacity;i++)
  72.         data[i]=entry.data[i];
  73.     used=entry.used;
  74.     capacity=entry.capacity;
  75.     current_index=entry.current_index;
  76. }
  77. // CONSTANT MEMBER FUNCTIONS
  78. size_type sequence::size() const
  79. {
  80.     return used;
  81. }
  82. bool sequence::is_item() const
  83. {
  84.     if (current_index >= 0 && current_index <= capacity)
  85.         return true;
  86.     else
  87.         return false;
  88. }
  89. value_type sequence::current() const
  90. {
  91.     if (is_item()== true)
  92.         return data[current_index];
  93.     else
  94.         return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement