Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class ListException
- {
- public:
- virtual string message()=0;
- };
- class IndexOutOfBoundException : public ListException
- {
- private:
- int indeks;
- public:
- IndexOutOfBoundException(int indeks)
- {
- this->indeks=indeks;
- }
- string message()
- {
- string ms = "Probowano dostac sie do indeksu, ktory wykracza poza wektor!";
- return ms;
- }
- };
- class FreeIndexException : public ListException
- {
- private:
- int indeks;
- public:
- FreeIndexException(int indeks)
- {
- this->indeks=indeks;
- }
- string message()
- {
- string ms = "Probowano dostac sie do indeksu pod ktorym jest wolne pole!";
- return ms;
- }
- };
- class VectorList
- {
- private:
- vector< pair<bool, int> > _list; // true-zajêta, false-wolna
- public:
- void add(int v)
- {
- if(_list.empty()==true)
- {
- _list.push_back(make_pair(true,v));
- }else
- {
- bool wszystkieZajete=true;
- for(unsigned int i = 0 ; i < _list.size(); i++)
- {
- if(_list[i].first==false)
- {
- _list.insert(_list.begin()+i, make_pair(true, v));
- wszystkieZajete=false;
- break;
- }
- }
- if(wszystkieZajete==true) _list.push_back(make_pair(true,v));
- }
- }
- void del(int i)
- {
- if(i<0||i>_list.size())
- {
- throw IndexOutOfBoundException(i);
- }else if(_list[i].first==false)
- {
- throw FreeIndexException(i);
- }else
- {
- _list[i].first = false;
- }
- }
- int get(int i)
- {
- int wartosc;
- if(i<0||i>_list.size())
- {
- throw IndexOutOfBoundException(i);
- }else if(_list[i].first==false)
- {
- throw FreeIndexException(i);
- }else
- {
- wartosc = _list[i].second;
- }
- return wartosc;
- }
- };
- int main()
- {
- VectorList wektor;
- try
- {
- wektor.add(6);
- wektor.add(8);
- wektor.add(3);
- wektor.del(1);
- cout<<wektor.get(0)<<endl;
- wektor.add(7);
- cout<<wektor.get(1)<<endl;
- wektor.add(11);
- wektor.del(-1);
- }catch(ListException& le)
- {
- cout<<le.message()<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement