Advertisement
Huntersazzad

shorted

Feb 29th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1.  
  2. (cpp file)
  3. #include"sortedtype.h"
  4.  
  5.  
  6. template <class ItemType>
  7. SortedType<ItemType>::SortedType()
  8. {
  9.     length=0;
  10.     currentPos=-1;
  11. }
  12. template <class ItemType>
  13. void SortedType<ItemType>::MakeEmpty()
  14. {
  15.     length=0;
  16. }
  17. template <class ItemType>
  18. bool SortedType<ItemType>::IsFull()
  19. {
  20.     return(length==MAX_ITEMS);
  21.  
  22. }
  23. template <class ItemType>
  24. int SortedType<ItemType>::LengthIs()
  25. {
  26.     return length;
  27. }
  28. template <class ItemType>
  29. void SortedType<ItemType>::ResetList()
  30. {
  31.     currentPos=-1;
  32. }
  33. template <class ItemType>
  34. void SortedType<ItemType>::GetNextItem(ItemType& item)
  35. {
  36.     currentPos++;
  37.     item=info[currentPos];
  38. }
  39. template <class ItemType>
  40. void SortedType<ItemType>::InsertItem(ItemType item)
  41. {
  42.     int location =0;
  43.     bool moreToSearch=(location<length);
  44.     while(moreToSearch)
  45.     {
  46.         if(item>info[location])
  47.         {
  48.             location++;
  49.             moreToSearch=(location<length);
  50.         }else if(item>info[location])
  51.         moreToSearch=false;
  52.     }
  53.     for(int index=length;index>location;index--)
  54.         info[index]=info[index-1];
  55.     info[location]=item;
  56.     length++;
  57.  
  58. }
  59. template <class ItemType>
  60. void SortedType<ItemType>::DeleteItem(ItemType item)
  61. {
  62.     int location=0;
  63.     while (item!=info[location])
  64.         location++;
  65.     for(int index=location+1;index<length;index++)
  66.         info[index-1]=info[index];
  67.     length--;
  68. }
  69. template <class ItemType>
  70. void SortedType<ItemType>::RetriveItem(ItemType&item,bool&found)
  71. {
  72.     int midPoint , first=0,last=length-1;
  73.     bool moreToSearch=(first<=last);
  74.     found=false;
  75.     while(moreToSearch&& !found)
  76.     {
  77.         midPoint=(first+last)/2;
  78.         if(item<info[midPoint])
  79.         {
  80.             last =midPoint-1;
  81.             moreToSearch=(first<=last);
  82.         }else if(item>info[midPoint])
  83.         {
  84.             first =midPoint+1;
  85.             moreToSearch=(first<=last);
  86.         }else
  87.         {
  88.             found=true;
  89.             item=info[midPoint];
  90.         }
  91.     }
  92.  
  93. }
  94.  
  95. HEDER FILE
  96.  
  97. #ifndef SORTEDTYPE_H_INCLUDED
  98. #define SORTEDTYPE_H_INCLUDED
  99. const int MAX_ITEMS=5;
  100. template <class ItemType>
  101. class SortedType
  102. {
  103. public :
  104.     SortedType();
  105.     void MakeEmpty();
  106.      bool IsFull();
  107.     int  LengthIs();
  108.     void InsertItem(ItemType);
  109.     void DeleteItem(ItemType);
  110.     void RetriveItem(ItemType&,bool&);
  111.     void ResetList();
  112.     void GetNextItem(ItemType&);
  113. private:
  114.     int length;
  115.     ItemType info[MAX_ITEMS];
  116.     int currentPos;
  117. };
  118.  
  119.  
  120. #endif // SORTEDTYPE_H_INCLUDED
  121.  
  122.  
  123.  
  124.  
  125. main
  126. #include <iostream>
  127. #include"sortedtype.h"
  128. #include"sortedtype.cpp"
  129. using namespace std;
  130.  
  131. int main()
  132. {
  133.     SortedType<int> saz;
  134.     cout << "length"<<saz.LengthIs()<< endl;
  135.  int n,a,j;
  136.     cin >> n;
  137.     while(n--)
  138.     {
  139.  
  140.         cin >> a;
  141.         saz.InsertItem(a);
  142.  
  143.     }
  144.  
  145. for( int j=0; j<saz.LengthIs(); j++)
  146.     {
  147.  
  148.  
  149.         saz.GetNextItem(a);
  150.         cout << a ;
  151.     }
  152.  
  153.  
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement