Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <exception>
  4.  
  5. using namespace std;
  6.  
  7. class exceptionDivide : public exception{
  8.     virtual const char* what() const throw(){
  9.         return "ERROR: DIVIDE-IMPOSSIBLE";
  10.     }
  11. };
  12.  
  13. template <typename Type>
  14. class Array {
  15.     Type** List; // lista cu pointeri la obiecte de tipul T*
  16.  
  17.     int Capacity; // dimensiunea listei de pointeri
  18.  
  19.     int Size; // cate elemente sunt in lista
  20.  
  21. public:
  22.     Array(); // Lista nu e alocata, Capacity si Size = 0
  23.  
  24.     void Print();
  25.  
  26.     Type& operator[] (int index); // arunca exceptie daca index este out of range
  27.  
  28.     const Array<Type>& Insert(int index, Type &newElem); // adauga un element pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
  29.  
  30.     void Sort(bool(*compare)(const Type, const Type)); // sorteaza folosind o functie de comparatie
  31.  
  32.     int Find(const Type& elem); // cauta un element in Array
  33.  
  34.     int BinarySearch(Type & elem); // cauta un element folosind binary search in Array
  35.  
  36.     int GetSize();
  37.     int GetCapacity();
  38. };
  39.  
  40. template <typename Type>
  41. bool isLesser(const Type arg1,const Type arg2) {
  42.     return arg1 < arg2;
  43. }
  44.  
  45. int main() {
  46.     Array<int> arr;
  47.     int x = 3;
  48.     int y = 5;
  49.     int z = 4;
  50.     int t = 6;
  51.     try {
  52.         arr.Insert(0, x);
  53.         arr.Insert(1, y);
  54.         arr.Insert(2, x);
  55.         arr.Insert(1, y);
  56.         arr.Insert(2, x);
  57.         arr.Insert(2, t);
  58.         arr.Insert(3, x);
  59.         arr.Insert(95, x);
  60.     }
  61.     catch(exception e){
  62.         cout << e.what();
  63.     }
  64.     arr.Print();
  65.     cout << arr.Find(y) << "\n";
  66.  
  67.     arr.Sort(isLesser);
  68.     arr.Print();
  69.     cout << arr.BinarySearch(y) << "\n";
  70.     _getch();
  71. }
  72.  
  73. template<typename Type>
  74. Array<Type>::Array(){
  75.     List = nullptr;
  76.     Capacity = 0;
  77.     Size = 0;
  78. }
  79.  
  80. template<typename Type>
  81. void Array<Type>::Print(){
  82.     for (int i = 0; i < Size; i++) {
  83.         cout << *List[i] << " ";
  84.     }
  85.     cout << "\n";
  86. }
  87.  
  88. template<typename Type>
  89. Type & Array<Type>::operator[](int index){
  90.     if (index < 0 or index >= Size) {
  91.         throw exception("Signal 11");
  92.     }
  93.     return List[index];
  94. }
  95.  
  96. template<typename Type>
  97. const Array<Type>& Array<Type>::Insert(int index, Type & newElem){
  98.     if (Capacity == 0) {
  99.         List = new Type*[100];
  100.         Capacity = 100;
  101.         Size = 0;
  102.     }
  103.    
  104.     if (index > Size or index < 0) {
  105.         throw exception("Signal 11\n");
  106.     }
  107.  
  108.     if (index != Size) {
  109.         for (int i = Size; i >= index; i--) {
  110.             List[i + 1] = List[i];
  111.         }
  112.     }
  113.  
  114.     List[index] = &newElem;
  115.     Size++;
  116.  
  117.     return *this;
  118. }
  119.  
  120. template<typename Type>
  121. void Array<Type>::Sort(bool(*compare)(const Type, const Type)){
  122.     for (int i = 0; i < Size; i++) {
  123.         for (int j = i + 1; j < Size; j++) {
  124.             if (!compare(*List[i], *List[j])) {
  125.                 auto aux = List[i];
  126.                 List[i] = List[j];
  127.                 List[j] = aux;
  128.             }
  129.         }
  130.     }
  131. }
  132.  
  133. template<typename Type>
  134. int Array<Type>::Find(const Type & elem){
  135.     for (int i = 0; i < Size; i++) {
  136.         if (&elem == List[i]) {
  137.             return i;
  138.         }
  139.     }
  140.     return Size;
  141. }
  142.  
  143. template<typename Type>
  144. int Array<Type>::BinarySearch(Type & elem){
  145.     int left = 0;
  146.     int right = Size - 1;
  147.     int middle = (left + right - 1) / 2;
  148.  
  149.     while (left != right) {
  150.         if (List[middle] == &elem)
  151.             return middle;
  152.         if (*List[middle] > elem)
  153.             right = middle;
  154.         else
  155.             left = middle;
  156.         middle = (left + right - 1) / 2;
  157.     }
  158.     return -1;
  159. }
  160.  
  161. template<typename Type>
  162. int Array<Type>::GetSize(){
  163.     return Size;
  164. }
  165.  
  166. template<typename Type>
  167. int Array<Type>::GetCapacity(){
  168.     return Capacity;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement