Advertisement
Guest User

Nasled_dz

a guest
May 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. class Exception
  7. {
  8. private:
  9.     string s;
  10. public:
  11.     Exception(string mes) : s(mes) {}
  12.  
  13.     string Messahe()
  14.     {
  15.         return s;
  16.     }
  17. };
  18.  
  19. template<class T> class dList
  20. {
  21. protected:
  22.     class Element
  23.     {
  24.     public:
  25.         T content;
  26.         Element* next;
  27.         Element* prev;
  28.  
  29.         Element(Element* prev, T data, Element* next)
  30.         {
  31.             this->content = data;
  32.             this->next = next;
  33.             this->prev = prev;
  34.         }
  35.     };
  36.  
  37.     class ElementWithIndex
  38.     {
  39.     public:
  40.         Element* element;
  41.         int index;
  42.  
  43.         ElementWithIndex(Element* elem, int index)
  44.         {
  45.             this->element = elem;
  46.             this->index = index;
  47.         }
  48.  
  49.         T Content()
  50.         {
  51.             return element->content;
  52.         }
  53.  
  54.         inline Element* Next()
  55.         {
  56.             return element->next;
  57.         }
  58.  
  59.         inline Element* Prev()
  60.         {
  61.             return element->prev;
  62.         }
  63.  
  64.         inline void GoNext()
  65.         {
  66.             element = element->next;
  67.             index++;
  68.         }
  69.  
  70.         inline void GoPrev()
  71.         {
  72.             element = element->prev;
  73.             index--;
  74.         }
  75.     };
  76.  
  77.     Element* head;
  78.     Element* tail;
  79.     ElementWithIndex* cur;
  80.     int length;
  81.  
  82.     void offset_cur(int index)
  83.     {
  84.         if (!cur)
  85.         {
  86.             cur->element = head;
  87.             cur->index = 0;
  88.         }
  89.  
  90.         int lc = cur->index - index;
  91.         lc = (lc < 0) ? -lc : lc;
  92.         int lh = index;
  93.         int lt = length - index;
  94.  
  95.         if (index == 0 || (lh <= lc && lh <= lt))
  96.         {
  97.             cur->element = head;
  98.             cur->index = 0;
  99.         }
  100.  
  101.         if (index == length - 1 || (lt <= lc && lt <= lh))
  102.         {
  103.             cur->element = tail;
  104.             cur->index = length - 1;
  105.         }
  106.  
  107.         while (cur->index != index)
  108.         {
  109.             if (cur->index > index)
  110.             {
  111.                 cur->GoPrev();
  112.             }
  113.             else
  114.             {
  115.                 cur->GoNext();
  116.             }
  117.         }
  118.     }
  119. public:
  120.     dList()
  121.     {
  122.         length = 0;
  123.         head = 0;
  124.         tail = 0;
  125.         cur = new ElementWithIndex(0, 0);
  126.     }
  127.  
  128.     void Add(T data)
  129.     {
  130.         if (length)
  131.         {
  132.             tail = (tail->next = new Element(tail, data, 0));
  133.             length++;
  134.         }
  135.         else
  136.         {
  137.             cur->element = head = tail = new Element(0, data, 0);
  138.             cur->index = 0;
  139.             length++;
  140.         }
  141.     }
  142.  
  143.     int Length()
  144.     {
  145.         return length;
  146.     }
  147.  
  148.     T* GetAll()
  149.     {
  150.         T* res = new T[length];
  151.         Element* t = head;
  152.  
  153.         for (int i = 0; i < length; i++)
  154.         {
  155.             res[i] = t->content;
  156.             t = t->next;
  157.         }
  158.  
  159.         return res;
  160.     }
  161.  
  162.     T Get(int index)
  163.     {
  164.         if (index < 0 || index >= length) throw new Exception("Index out of range");
  165.         offset_cur(index);
  166.         return cur->Content();
  167.     }
  168.  
  169.     void Remove(int index)
  170.     {
  171.  
  172.         if (index < 0 || index >= length) throw new Exception("Index out of range");
  173.         offset_cur(index);
  174.  
  175.         if (!cur->Next() && !cur->Prev())
  176.         {
  177.             tail = head = 0;
  178.             delete cur->element;
  179.             cur->element = 0;
  180.             length--;
  181.             cur->index = 0;
  182.             return;
  183.         }
  184.  
  185.         if (!cur->Next())
  186.         {
  187.             tail = tail->prev;
  188.             delete cur->element;
  189.             cur->element = tail;
  190.             length--;
  191.             cur->index--;
  192.             return;
  193.         }
  194.  
  195.         if (!cur->Prev())
  196.         {
  197.             head = head->next;
  198.             delete cur->element;
  199.             cur->element = head;
  200.             length--;
  201.             cur->index++;
  202.             return;
  203.         }
  204.  
  205.         cur->Prev()->next = cur->Next();
  206.         cur->Next()->prev = cur->Prev();
  207.         Element* t = cur->element;
  208.         cur->element = t->next;
  209.         cur->index++;
  210.         length--;
  211.         delete t;
  212.     }
  213.  
  214.     int FindObject(bool(*inspect)(T), int index = 0)
  215.     {
  216.         offset_cur(index);
  217.  
  218.         for (int i = index; i < length && cur; i++)
  219.         {
  220.             if (inspect(cur->Content())) return i;
  221.  
  222.             cur->GoNext();
  223.         }
  224.  
  225.         return -1;
  226.     }
  227.  
  228.     T* FindAll(bool(*inspect)(T), int& res_length)
  229.     {
  230.         offset_cur(0);
  231.         int cnt = 0;
  232.  
  233.         for (int i = 0; i < length && cur; i++)
  234.         {
  235.             if (inspect(cur->Content())) cnt++;
  236.  
  237.             cur->GoNext();
  238.         }
  239.  
  240.         res_length = cnt;
  241.         if (!cnt) return 0;
  242.  
  243.         T* res = new T[cnt];
  244.         offset_cur(0);
  245.  
  246.         for (int i = 0, t = 0; i < length && cur && t < cnt; i++)
  247.         {
  248.             if (inspect(cur->Content()))
  249.             {
  250.                 res[t] = cur->Content();
  251.                 t++;
  252.             }
  253.  
  254.             cur->GoNext();
  255.         }
  256.         return res;
  257.     }
  258.  
  259.     void ClearList()
  260.     {
  261.         for (int i = length - 1; i >= 0; i--) Remove(i);
  262.     }
  263.  
  264.     ~dList()
  265.     {
  266.         ClearList();
  267.     }
  268. };
  269.  
  270. class InfoBaseClass
  271. {
  272. protected:
  273.     string name;
  274.     string info;
  275.  
  276.     void SetName(string val)
  277.     {
  278.         name = val;
  279.     }
  280.  
  281.     void  SetInfo(string val)
  282.     {
  283.         info = val;
  284.     }
  285. public:
  286.     InfoBaseClass(string name, string info) : name(name), info(info) {}
  287.  
  288.     string GetName()
  289.     {
  290.         return name;
  291.     }
  292.  
  293.     string GetInfo()
  294.     {
  295.         return info;
  296.     }
  297. };
  298.  
  299. class Vacancy : public InfoBaseClass
  300. {
  301. public:
  302.     Vacancy(string name, string specialty) : InfoBaseClass(name, specialty) {}
  303. };
  304.  
  305. class Company : public InfoBaseClass, private dList<Vacancy*>
  306. {
  307. public:
  308.     Company(string name) : InfoBaseClass(name, "Company"), dList<Vacancy*>() {}
  309.  
  310.     void AddVacancy(string name, string specialty)
  311.     {
  312.         this->Add(new Vacancy(name, specialty));
  313.     }
  314.  
  315.     Vacancy** GetAllVacancy()
  316.     {
  317.         return this->GetAll();
  318.     }
  319.  
  320.     Vacancy* GetVacancy(int index)
  321.     {
  322.         return this->Get(index);
  323.     }
  324.  
  325.     void Remove(int index)
  326.     {
  327.         this->Remove(index);
  328.     }
  329.  
  330.     Vacancy* FindVacancy_name(string name)
  331.     {
  332.         offset_cur(0);
  333.  
  334.         for (int i = 0; i < length && cur; i++)
  335.         {
  336.             if (cur->Content()->GetName() == name) return cur->Content();
  337.  
  338.             cur->GoNext();
  339.         }
  340.  
  341.         return 0;
  342.     }
  343.  
  344.     Vacancy* FindVacancy_specialty(string info)
  345.     {
  346.         offset_cur(0);
  347.  
  348.         for (int i = 0; i < length && cur; i++)
  349.         {
  350.             if (cur->Content()->GetInfo() == info) return cur->Content();
  351.  
  352.             cur->GoNext();
  353.         }
  354.  
  355.         return 0;
  356.     }
  357.  
  358.     Vacancy** FindAllVacancy_name(string name, int& res_length)
  359.     {
  360.         offset_cur(0);
  361.         int cnt = 0;
  362.  
  363.         for (int i = 0; i < length && cur; i++)
  364.         {
  365.             if (cur->Content()->GetName() == name) cnt++;
  366.  
  367.             cur->GoNext();
  368.         }
  369.  
  370.         res_length = cnt;
  371.         if (!cnt) return 0;
  372.  
  373.         Vacancy** res = new Vacancy * [cnt];
  374.         offset_cur(0);
  375.  
  376.         for (int i = 0, t = 0; i < length && cur && t < cnt; i++)
  377.         {
  378.             if (cur->Content()->GetName() == name)
  379.             {
  380.                 res[t] = cur->Content();
  381.                 t++;
  382.             }
  383.  
  384.             cur->GoNext();
  385.         }
  386.         return res;
  387.     }
  388.  
  389.     Vacancy** FindAllVacancy_specialty(string info, int& res_length)
  390.     {
  391.         offset_cur(0);
  392.         int cnt = 0;
  393.  
  394.         for (int i = 0; i < length && cur; i++)
  395.         {
  396.             if (cur->Content()->GetInfo() == info) cnt++;
  397.  
  398.             cur->GoNext();
  399.         }
  400.  
  401.         res_length = cnt;
  402.         if (!cnt) return 0;
  403.  
  404.         Vacancy** res = new Vacancy * [cnt];
  405.         offset_cur(0);
  406.  
  407.         for (int i = 0, t = 0; i < length && cur && t < cnt; i++)
  408.         {
  409.             if (cur->Content()->GetInfo() == info)
  410.             {
  411.                 res[t] = cur->Content();
  412.                 t++;
  413.             }
  414.  
  415.             cur->GoNext();
  416.         }
  417.         return res;
  418.     }
  419.  
  420.     void Clear()
  421.     {
  422.         this->ClearList();
  423.     }
  424.  
  425.     int GetVacancyCount()
  426.     {
  427.         return length;
  428.     }
  429. };
  430.  
  431. int main()
  432. {
  433.  
  434.     //getc(stdin);
  435.     return 0;
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement