Advertisement
Komandor_Astrii

Kolzad3

Jun 23rd, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class ListException
  7. {
  8. public:
  9.     virtual string message()=0;
  10. };
  11.  
  12. class IndexOutOfBoundException : public ListException
  13. {
  14. private:
  15.     int indeks;
  16. public:
  17.     IndexOutOfBoundException(int indeks)
  18.     {
  19.         this->indeks=indeks;
  20.     }
  21.  
  22.     string message()
  23.     {
  24.         string ms = "Probowano dostac sie do indeksu, ktory wykracza poza wektor!";
  25.         return ms;
  26.     }
  27. };
  28.  
  29. class FreeIndexException : public ListException
  30. {
  31. private:
  32.     int indeks;
  33. public:
  34.     FreeIndexException(int indeks)
  35.     {
  36.         this->indeks=indeks;
  37.     }
  38.  
  39.     string message()
  40.     {
  41.         string ms = "Probowano dostac sie do indeksu pod ktorym jest wolne pole!";
  42.         return ms;
  43.     }
  44. };
  45.  
  46. class VectorList
  47. {
  48. private:
  49.     vector< pair<bool, int> > _list; // true-zajêta, false-wolna
  50. public:
  51.     void add(int v)
  52.     {
  53.        if(_list.empty()==true)
  54.        {
  55.             _list.push_back(make_pair(true,v));
  56.        }else
  57.        {
  58.            bool wszystkieZajete=true;
  59.  
  60.            for(unsigned int i = 0 ; i < _list.size(); i++)
  61.            {
  62.                if(_list[i].first==false)
  63.                {
  64.                    _list.insert(_list.begin()+i, make_pair(true, v));
  65.                    wszystkieZajete=false;
  66.                    break;
  67.                }
  68.            }
  69.  
  70.            if(wszystkieZajete==true)    _list.push_back(make_pair(true,v));
  71.        }
  72.     }
  73.  
  74.     void del(int i)
  75.     {
  76.         if(i<0||i>_list.size())
  77.         {
  78.             throw IndexOutOfBoundException(i);
  79.         }else if(_list[i].first==false)
  80.         {
  81.             throw FreeIndexException(i);
  82.         }else
  83.         {
  84.             _list[i].first = false;
  85.         }
  86.     }
  87.  
  88.     int get(int i)
  89.     {
  90.         int wartosc;
  91.         if(i<0||i>_list.size())
  92.         {
  93.             throw IndexOutOfBoundException(i);
  94.         }else if(_list[i].first==false)
  95.         {
  96.             throw FreeIndexException(i);
  97.         }else
  98.         {
  99.             wartosc = _list[i].second;
  100.         }
  101.         return wartosc;
  102.     }
  103. };
  104.  
  105. int main()
  106. {
  107.     VectorList wektor;
  108.  
  109.     try
  110.     {
  111.         wektor.add(6);
  112.         wektor.add(8);
  113.         wektor.add(3);
  114.         wektor.del(1);
  115.  
  116.         cout<<wektor.get(0)<<endl;
  117.         wektor.add(7);
  118.  
  119.         cout<<wektor.get(1)<<endl;
  120.  
  121.         wektor.add(11);
  122.  
  123.         wektor.del(-1);
  124.  
  125.     }catch(ListException& le)
  126.     {
  127.         cout<<le.message()<<endl;
  128.     }
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement