Advertisement
Guest User

kolosPK3

a guest
Jan 21st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. class platnosc
  2. {
  3.     char* nazwa;
  4.     double kwota;
  5.     int dzien;
  6.     int miesiac;
  7.     int rok;
  8. public:
  9.     platnosc(char*, double, int, int, int);
  10.     bool operator<(const platnosc& p);
  11.     friend  std::ostream& operator <<(std::ostream& os, platnosc& p);
  12.  
  13. };
  14.  
  15. struct lista_platnosci
  16. {
  17.     platnosc p;
  18.     lista_platnosci* next;
  19.     lista_platnosci(const platnosc& q) : p(q), next(nullptr) {};
  20. };
  21.  
  22. class stos_platnosci
  23. {
  24.     int pojemnosc;
  25. protected:
  26.     lista_platnosci* head;
  27.  
  28. public:
  29.     stos_platnosci(int p) : head(nullptr), pojemnosc(p) {};
  30.     stos_platnosci & operator --(int)
  31.     {
  32.         if (!head) throw "pusty stos";
  33.         else
  34.         {
  35.  
  36.             lista_platnosci* q = head, * r = head->next;
  37.             head = r;
  38.             delete q;
  39.             return *this;
  40.         }
  41.     }
  42.     stos_platnosci put(const platnosc & p_)
  43.     {
  44.         if(!head)
  45.         {
  46.             lista_platnosci* head = new lista_platnosci(p_,nullptr);
  47.             return *this;
  48.         }
  49.         else
  50.         {  
  51.             int length = 0;
  52.             lista_platnosci* tmp = head;
  53.             while(tmp)
  54.             {
  55.                 length++;
  56.                 tmp = tmp->next;
  57.             }
  58.  
  59.             if (length > pojemnosc)
  60.             {
  61.                 lista_platnosci* q = new lista_platnosci(p_);
  62.                 q->p = p_;
  63.                 q->next = head;
  64.                 head = q;
  65.                 return *this;
  66.             }
  67.             else return *this;
  68.         }
  69.     }
  70.     void empty()
  71.     {
  72.         while(head)
  73.         {
  74.             lista_platnosci* p = head;
  75.             delete p;
  76.             head = head->next;
  77.         }
  78.     }
  79.     ~stos_platnosci() { empty(); };
  80. };
  81.  
  82. class kolejka_wydatkow : public stos_platnosci
  83. {
  84. public:
  85.     kolejka_wydatkow(platnosc & p_, int )
  86.     {
  87.         lista_platnosci* q = new lista_platnosci(p_);
  88.         if((!head) || (head->p < p_))
  89.         {
  90.             q->next = head;
  91.             head = q;
  92.         }
  93.         else
  94.         {
  95.             lista_platnosci* r = head;
  96.             while (r->next && (r->next->p < p_))
  97.                 r = r->next;
  98.  
  99.             q->next = r->next;
  100.             r->next = q;
  101.         }
  102.  
  103.     }
  104.     friend std::ostream& operator<<(std::ostream& os,kolejka_wydatkow &k)
  105.     {
  106.         lista_platnosci* tmp = k.head;
  107.         while(tmp)
  108.         {
  109.             os << tmp->p;
  110.             os << "->";
  111.             tmp = tmp->next;
  112.         }
  113.         return os;
  114.     }
  115.     kolejka_wydatkow operator =(const kolejka_wydatkow& k_)
  116.     {
  117.         if(&k_ == this)
  118.         {
  119.             return *this;
  120.         }
  121.         empty();
  122.  
  123.         lista_platnosci* q = k_.head;
  124.         while(q)
  125.         {
  126.             put(q->p);
  127.             q = q->next;
  128.         }
  129.         return *this;
  130.     };
  131.     kolejka_wydatkow operator=(kolejka_wydatkow& k_)
  132.     {
  133.         std::swap(head, k_.head);
  134.         return *this;
  135.     }
  136.     kolejka_wydatkow insert(char * nazwa, double kwota, int dzien, int miesiac, int rok)
  137.     {
  138.         platnosc p_(nazwa, kwota, dzien, miesiac, rok);
  139.  
  140.         lista_platnosci* q = new lista_platnosci(p_);
  141.         if((!head) || q->p < p_)
  142.         {
  143.             q->next = head;
  144.             head = q;
  145.         }
  146.         else
  147.         {
  148.             lista_platnosci* r = head;
  149.                 while((r->next) && r->next->p <  p_)
  150.                 {
  151.                     r = r->next;
  152.                 }
  153.  
  154.                 q->next = r->next;
  155.                 r->next = q;
  156.         }
  157.  
  158.     }
  159.  
  160.     ~kolejka_wydatkow() { empty(); };
  161. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement