Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T>
  5. class Median{
  6. private:
  7.     T* array;
  8.     unsigned numElements;
  9. public:
  10.     Median();
  11.     unsigned size();
  12.     void empty();
  13.     void print();
  14.     void insert(T& item);
  15.     T median();
  16. };
  17. template<class T>
  18. Median<T>::Median() {
  19.     numElements = 0;
  20.     array = new T[numElements];
  21. }
  22.  
  23. template<class T>
  24. unsigned Median<T>::size() {
  25.     return numElements;
  26. }
  27.  
  28. template<class T>
  29. void Median<T>::empty() {
  30.     if(array != NULL){
  31.         delete[] array;
  32.     }
  33.     numElements = 0;
  34.     array = new T[0];
  35. }
  36.  
  37. template<class T>
  38. void Median<T>::print() {
  39.     for(unsigned i=0; i<Median::numElements; i++){
  40.         cout<<array[i]<<"\t";
  41.     }
  42. }
  43.  
  44. template<class T>
  45. void Median<T>::insert(T &item) {
  46.  
  47.     //create a copy temp array before increasing the size
  48.     T* temp = new T[numElements];
  49.     for(unsigned i=0; i<Median::numElements; i++) {
  50.         temp[i] = array[i];
  51.     }
  52.  
  53.     if(array != NULL){
  54.         delete[] array;
  55.     }
  56.     numElements++;
  57.     array = new T[numElements];
  58.  
  59.     //restore the available items
  60.     if(numElements>2) {
  61.         unsigned posToInsert = 0;
  62.         bool isFound = false;
  63.         if(item<=temp[0]){
  64.             isFound = true;
  65.         }else {
  66.             for (unsigned i = 0; i < Median::numElements - 2; i++) {
  67.                 if (temp[i] <= item && temp[i + 1] >= item) {
  68.                     posToInsert = i + 1;
  69.                     isFound = true;
  70.                 }
  71.             }
  72.         }
  73.         if(!isFound){
  74.             posToInsert=numElements-1;
  75.         }
  76.  
  77.         unsigned posNow = 0;
  78.         bool  isInserted = false;
  79.         for (unsigned i = 0; i < Median::numElements-1; i++) {
  80.             if(i!=posToInsert){
  81.                 array[posNow++] = temp[i];
  82.             }else{
  83.                 array[posNow++] = item;
  84.                 array[posNow++] = temp[i];
  85.                 isInserted = true;
  86.             }
  87.         }
  88.  
  89.         if(!isInserted){
  90.             array[posNow] = item;
  91.         }
  92.  
  93.     }else{
  94.         if(numElements==1) {
  95.             array[0] = item;
  96.         } else{
  97.             if(item<temp[0]){
  98.                 array[0] = item;
  99.                 array[1] = temp[0];
  100.             }else{
  101.                 array[0] = temp[0];
  102.                 array[1] = item;
  103.             }
  104.         }
  105.     }
  106.  
  107. }
  108.  
  109. template<class T>
  110. T Median<T>::median() {
  111.     return nullptr;
  112. }
  113.  
  114.  
  115.  
  116. int main() {
  117.     Median<int> median;
  118.  
  119.     int num = 8;
  120.     median.insert(num);
  121.  
  122.     int num2 = 4;
  123.     median.insert(num2);
  124.  
  125.  
  126.     num = 9;
  127.     median.insert(num);
  128.  
  129.     num = 5;
  130.     median.insert(num);
  131.  
  132.  
  133.  
  134.     num = 2;
  135.     median.insert(num);
  136.  
  137.  
  138.     num = 3;
  139.     median.insert(num);
  140.  
  141.     num = 1;
  142.     median.insert(num);
  143.  
  144.  
  145.     num = 13;
  146.     median.insert(num);
  147.     median.print();
  148.  
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement