Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Lista;
  7.  
  8. class Cadou
  9. {
  10.     int tip;
  11.     string persoana;
  12.     int pret;
  13.     Cadou *urm;
  14.  
  15. public:
  16.     Cadou(int tip, string persoana, int pret)
  17.     {
  18.         this->tip = tip;
  19.         this->persoana = persoana;
  20.         this->pret = pret;
  21.     }
  22.  
  23.     virtual void afisare()
  24.     {
  25.         cout << "Persoana: " << persoana<<endl;
  26.         cout << "Pret: " << pret<<endl;
  27.     }
  28.  
  29.     friend class Lista;
  30. };
  31.  
  32. class Obiect : public Cadou
  33. {
  34.     string model;
  35.     string marime;
  36. public:
  37.     Obiect(int tip, string persoana, int pret, string model, string marime) :Cadou(tip, persoana, pret)
  38.     {
  39.         this->model = model;
  40.         this->marime = marime;
  41.     }
  42.     void afisare()
  43.     {
  44.         Cadou::afisare();
  45.         cout << "Model: " << model << endl;
  46.         cout << "Marime: " << marime << endl<<endl;
  47.     }
  48.     friend class Lista;
  49. };
  50.  
  51. class Dulciuri : public Cadou
  52. {
  53.     string aroma;
  54.     string firma;
  55. public:
  56.     Dulciuri(int tip, string persoana, int pret, string aroma, string firma) :Cadou(tip, persoana, pret)
  57.     {
  58.         this->aroma = aroma;
  59.         this->firma = firma;
  60.     }
  61.     void afisare()
  62.     {
  63.         Cadou::afisare();
  64.         cout << "Aroma: " << aroma << endl;
  65.         cout << "Firma: " << firma << endl<<endl;
  66.     }
  67.     friend class Lista;
  68. };
  69.  
  70. class Lista {
  71.  
  72. public:
  73.     Cadou *head;
  74.     void adaugare(Cadou *a);
  75.     void afisare();
  76.     void cautare();
  77.     void stergere();
  78.  
  79. };
  80.  
  81. void Lista::adaugare(Cadou *a)
  82. {
  83.     Cadou *p;
  84.     p = head;
  85.    
  86.     if (!p)
  87.     {
  88.         head = a;
  89.     }
  90.     else
  91.     {
  92.         if (a->persoana.compare(p->persoana) < 0)
  93.         {
  94.             a->urm = head;
  95.             head = a;
  96.         }
  97.         else
  98.         {
  99.             while (p->urm && p->urm->persoana.compare(a->persoana))
  100.                 p = p->urm;
  101.             a->urm = p->urm;
  102.             p->urm = a;
  103.         }
  104.  
  105.     }
  106. }
  107.  
  108. void Lista::afisare()
  109. {
  110.     Cadou *p;
  111.     p = head;
  112.  
  113.     if (!p)
  114.         cout << "Lista vida" << endl;
  115.     else
  116.     {
  117.         while (p)
  118.         {
  119.             p->afisare();
  120.             p = p->urm;
  121.             _getch();
  122.         }
  123.     }
  124. }
  125.  
  126. void Lista::cautare()
  127. {
  128.     Cadou *p;
  129.     p = head;
  130.     string arom;
  131.     cout << "Aroma cautata: "; cin >> arom;
  132.  
  133.     if (!p)
  134.         cout << "Lista vida" << endl;
  135.     else
  136.     {
  137.         while (p)
  138.         {
  139.             if (p->tip == 2)
  140.             {
  141.                 Dulciuri *dul;
  142.                 dul = (Dulciuri*)p;
  143.                 if (dul->aroma.compare(arom) == 0)
  144.                 {
  145.                     p->afisare();
  146.                     _getch();
  147.                 }
  148.             }
  149.         }
  150.     }
  151.     cout << "Produs negasit" << endl;
  152. }
  153.  
  154. void Lista::stergere()
  155. {
  156.     Cadou *p,*q;
  157.     p = q = head;
  158.     string pers;
  159.     cout << "Persoana cautata: "; cin >> pers;
  160.  
  161.     if (!p)
  162.         cout << "Lista vida" << endl;
  163.     else
  164.     {
  165.         while (p && p->persoana.compare(pers))
  166.         {
  167.             q = p;
  168.             p = p->urm;
  169.         }
  170.         if (p)
  171.         {
  172.             if (p != q)
  173.             {
  174.                 q->urm = p->urm;
  175.                 delete p;
  176.             }
  177.             else
  178.             {
  179.                 head = p->urm;
  180.                 delete p;
  181.             }
  182.         }
  183.         else cout<<"Negasit"<<endl;
  184.     }
  185.    
  186. }
  187.  
  188. void introd(Lista &m, int x)
  189. {
  190.     string persoana;
  191.     int pret;
  192.     string model;
  193.     string marime;
  194.     string aroma;
  195.     string firma;
  196.     Cadou *cad;
  197.  
  198.     cout << "Persoana: "; cin >> persoana;
  199.     cout << "Pret: "; cin >> pret;
  200.  
  201.     if (x == 1)
  202.     {
  203.         Obiect *ob;
  204.         cout << "Model: "; cin >> model;
  205.         cout << "Marime: "; cin >> marime;
  206.         ob = new Obiect(1, persoana, pret, model, marime);
  207.         cad = ob;
  208.         m.adaugare(cad);
  209.         return;
  210.     }
  211.  
  212.     if (x == 2)
  213.     {
  214.         Dulciuri *dul;
  215.         cout << "Aroma: "; cin >> aroma;
  216.         cout << "Firma: "; cin >> firma;
  217.         dul = new Dulciuri(2, persoana, pret, aroma, firma);
  218.         cad = dul;
  219.         m.adaugare(cad);
  220.         return;
  221.     }
  222.  
  223. }
  224.  
  225.  
  226. int main()
  227. {
  228.     int opt;
  229.     Lista m;
  230.     m.head=NULL;
  231.  
  232.     do {
  233.         cout << endl;
  234.         cout << "1. Adaugare obiect" << endl;
  235.         cout << "2. Adaugare dulciuri" << endl;
  236.         cout << "3. Afisare" << endl;
  237.         cout << "4. Cautare in functie de aroma" << endl;
  238.         cout << "5. Stergere in functie de persoana" << endl;
  239.         cout << "0. Exit" << endl;
  240.  
  241.         cin >> opt;
  242.  
  243.         switch (opt)
  244.         {
  245.             case 1:
  246.                 introd(m, 1);
  247.                 break;
  248.  
  249.             case 2:
  250.                 introd(m, 2);
  251.                 break;
  252.  
  253.             case 3:
  254.                 m.afisare();
  255.                 break;
  256.  
  257.             case 4:
  258.                 m.cautare();
  259.                 break;
  260.  
  261.             case 5:
  262.                 m.stergere();
  263.                 break;
  264.  
  265.             case 0:
  266.                 break;
  267.  
  268.             default: cout << "Varianta gresita" << endl;
  269.         }
  270.  
  271.     } while (opt);
  272.  
  273.     _getch();
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement