Advertisement
Guest User

POO exam

a guest
Jan 28th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.43 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. typedef enum { activ = 0, inactiv = 1, defect = 2 }stare;
  8. typedef enum { V5, V12, V14, V220 }tensiune_comanda;
  9.  
  10. class Lista;
  11.  
  12. class Modul
  13. {  
  14.     int tip;
  15.     int id_modul = rand() % 4000;
  16.     string denumire;
  17.  
  18. public:
  19.     int valoare;
  20.     Modul *urm;
  21.  
  22.     Modul(int tip, int id_modul, string denumire, int valoare)
  23.     {
  24.         this->tip = tip;
  25.         this->id_modul = id_modul;
  26.         this->denumire = denumire;
  27.         this->valoare = valoare;
  28.     }
  29.  
  30.     virtual void afisare()
  31.     {
  32.         if (tip == 1)
  33.             cout << "Element executie: " << endl;
  34.         if (tip == 2)
  35.             cout << "Senzor " << endl;
  36.  
  37.         cout << "ID_modul: " << id_modul << endl;
  38.         cout << "Denumire: " << denumire << endl;
  39.     }
  40.     friend class Lista;
  41. };
  42.  
  43. class Element_executie :public Modul
  44. {
  45.     tensiune_comanda t;
  46.     stare s;
  47.  
  48. public:
  49.     Element_executie(int tip, int id_modul, string denumire, int valoare, tensiune_comanda t, stare s) :Modul(tip, id_modul, denumire, valoare)
  50.     {
  51.         this->t = t;
  52.         this->s = s;
  53.     }
  54.  
  55.     void afisare()
  56.     {
  57.         Modul::afisare();
  58.  
  59.         if (s == 0) cout << "Activ";
  60.         if (s == 1) cout << "Inactiv";
  61.         if (s == 2) cout << "Defect";
  62.         cout << endl;
  63.         if (t == 0) cout << "5 Volti";
  64.         if (t == 1) cout << "12 Volti";
  65.         if (t == 2) cout << "24 Volti";
  66.         if (t == 3) cout << "220 Volti";
  67.         cout << endl;
  68.         cout << endl;
  69.     }
  70.     friend class Lista;
  71. };
  72.  
  73. class Senzor :public Modul
  74. {
  75.     float factor_calibrare;
  76.     float factor_translatie;
  77.     float val_reala;
  78.     int tip;
  79.  
  80. public:
  81.     Senzor(int tip, int id_modul, string denumire, int valoare, float factor_calibrare, float factor_translatie, float val_reala) :Modul(tip, id_modul, denumire, valoare)
  82.     {
  83.         this->factor_calibrare = factor_calibrare;
  84.         this->factor_translatie = factor_translatie;
  85.         this->val_reala = val_reala;
  86.     }
  87.  
  88.     void afisare()
  89.     {
  90.         Modul::afisare();
  91.         cout << "Factor calibrare: " << factor_calibrare << endl;
  92.         cout << "Factor translatie: " << factor_translatie << endl;
  93.         cout << "Valoare reala: " << val_reala<<endl<<endl;
  94.     }
  95.     friend class Lista;
  96. };
  97.  
  98. class Lista
  99. {
  100. public:
  101.     Modul *head;
  102.     void adaugare(Modul *m);
  103.     void afisare_lista();
  104.     void stergere();
  105.     void cautare();
  106. };
  107.  
  108. class MyException
  109. {
  110. public:
  111.     string s;
  112.     int i;
  113.  
  114.     MyException(string s, int i)
  115.     {
  116.         s = this->s;
  117.         i = this->i;
  118.     }
  119. };
  120.  
  121. void Lista::adaugare(Modul *a)
  122. {
  123.     Modul *p;
  124.     p = head;
  125.     if (!p)
  126.     {
  127.         head = a;
  128.     }
  129.     else
  130.     {
  131.         if (a->denumire.compare(p->denumire) < 0)
  132.         {
  133.             a->urm = head;
  134.             head = a;
  135.         }
  136.         else
  137.         {
  138.             while (p->urm && p->urm->denumire.compare(a->denumire) > 0)
  139.                 p = p->urm;
  140.             a->urm = p->urm;
  141.             p->urm = a;
  142.         }
  143.     }
  144. }
  145.  
  146. void introd(Lista &m, int x)
  147. {
  148.     Modul *mod;
  149.     int id_modul = rand() % 4000;
  150.     string denumire;
  151.     int valoare;
  152.     int star;
  153.     int tensiune;
  154.     float factor_calibrare, factor_translatie;
  155.     float val_reala;
  156.     cout << "Dati denumirea: ";
  157.     cin >> denumire;
  158.  
  159.     try
  160.     {
  161.         cout << "Dati valoarea: ";
  162.         cin >> valoare;
  163.         if (valoare < 0)
  164.             throw MyException("Valoare negativa ", valoare);
  165.     }
  166.     catch (MyException y)
  167.     {
  168.         do {
  169.             cout << "Introdu val pozitiva ";
  170.             cin >> valoare;
  171.         } while (valoare < 0);
  172.     }
  173.  
  174.     if (x == 1)
  175.     {
  176.         Element_executie *el;
  177.  
  178.         cout << "dati starea : 0-activ 1-inactiv 2-defect :";
  179.         cin >> star;
  180.         stare st = static_cast<stare>(star);
  181.  
  182.         cout << "dati tensiunea de comanda : 0-5v, 1-12v 2-24v,3-220v";
  183.         cin >> tensiune;
  184.         tensiune_comanda t = static_cast<tensiune_comanda>(tensiune);
  185.  
  186.         el = new Element_executie(1, id_modul, denumire, valoare, t, st);
  187.         mod = el;
  188.         m.adaugare(mod);
  189.     }
  190.     if (x == 2)
  191.     {
  192.         Senzor *sen;
  193.         cout << "dati factorul de calibrare";
  194.         cin >> factor_calibrare;
  195.         cout << "dati factorul de translatie";
  196.         cin >> factor_translatie;
  197.         val_reala = valoare*factor_calibrare + factor_translatie;
  198.         sen = new Senzor(2, id_modul, denumire, valoare, factor_calibrare, factor_translatie, val_reala);
  199.         mod = sen;
  200.         m.adaugare(mod);
  201.     }
  202. }
  203.  
  204. void Lista::afisare_lista()
  205. {
  206.     Modul *p;
  207.     p = head;
  208.     if (!p)
  209.         cout << "Lista vida!";
  210.     else
  211.     {
  212.         while (p != NULL)
  213.         {
  214.             p->afisare();
  215.             p = p->urm;
  216.             _getch();
  217.         }
  218.     }
  219. }
  220.  
  221. void Lista::stergere()
  222. {
  223.     Modul *p,*q;
  224.     p = head;
  225.     q = head;
  226.     string nume;
  227.     cout << "Denumire de sters: ";
  228.     cin >> nume;
  229.  
  230.     if (p)
  231.     {
  232.         while (p && p->denumire.compare(nume))
  233.         {
  234.             q = p;
  235.             p = p->urm;
  236.         }
  237.         if (p)
  238.         {
  239.             if (q != p)
  240.             {
  241.                 q->urm = p->urm;
  242.                 delete p;
  243.             }
  244.             else
  245.             {
  246.                 head = p->urm;
  247.                 delete p;
  248.             }
  249.         }
  250.         else cout << "Negasit" << endl;
  251.     }
  252.  
  253. }
  254.  
  255. void Lista::cautare()
  256. {
  257.     Modul *p;
  258.     p = head;
  259.     if (!p)
  260.         cout << "Lista vida!";
  261.     else
  262.     {
  263.         while (p != NULL)
  264.         {
  265.             if (p->tip == 1)
  266.             {
  267.                 Element_executie *el;
  268.                 el = (Element_executie *)p;
  269.            
  270.                 if (el->s == 2)
  271.                     el->afisare();
  272.             }
  273.             p = p->urm;
  274.         }
  275.     }
  276. }
  277.  
  278. int main()
  279. {
  280.     int opt;
  281.     string nume1;
  282.     Lista l;
  283.     l.head = NULL;
  284.     do
  285.     {
  286.         cout << endl << "1.Adaugare element de executie" << endl;
  287.         cout << "2.adaugare senzor" << endl;
  288.         cout << "3.afisare modul" << endl;
  289.         cout << "4.sterge modul" << endl;
  290.         cout << "5.Cautare elemente de executie defecte" << endl;
  291.         cout << "introduceti optiunea" << endl;
  292.         cin >> opt;
  293.         switch (opt)
  294.         {
  295.         case 1:
  296.             introd(l, 1);
  297.             break;
  298.         case 2:
  299.             introd(l, 2);
  300.             break;
  301.         case 3:
  302.             l.afisare_lista();
  303.             break;
  304.         case 4:
  305.             l.stergere();
  306.             l.afisare_lista();
  307.             break;
  308.         case 5:
  309.             l.cautare();
  310.             break;
  311.         }
  312.     } while (opt != 0);
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement