Advertisement
Alx09

Ex 28 POO

Feb 19th, 2021
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.59 KB | None | 0 0
  1. // Drula Alex Marian problema 28
  2. #include <iostream>
  3. #include <list>
  4. #include <iterator>
  5. #include <conio.h>
  6. #include <string>
  7. #include <exception>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14.  
  15.  
  16. class MyException : public exception {
  17. private:
  18.     string mesajEroare;
  19. public:
  20.  
  21.     MyException(string mesajEroare) {
  22.  
  23.         this->mesajEroare = mesajEroare;
  24.     }
  25.     const char* what() const throw() {
  26.         if (mesajEroare == "out_limit") return "Valoarea este inafara intervalului";
  27.         if (mesajEroare == "lista_goala") return "lista este goala";
  28.         return "Eroare neprevazuta!";
  29.     }
  30. };
  31. ostream & linie(ostream &out) {
  32.     out << "------------------------\n";
  33.     return out;
  34. }
  35.  
  36. class Modul {
  37. private:
  38.     string nume, stare;
  39.     int idModul, valoare;
  40.     int tipDerivat;
  41.    
  42. public:
  43.     Modul(int tipDerivat, string nume, int valoare, string stare) {
  44.         static int idCurent = 0;
  45.         idCurent++;
  46.         idModul = idCurent;
  47.         this->tipDerivat = tipDerivat;
  48.         this->nume = nume;
  49.         this->valoare = valoare;
  50.         this->stare = stare;
  51.     }
  52.     virtual void Afisare() {
  53.         cout << tipDerivat << " | " << idModul << " " << nume << " " << valoare << " " << stare << " ";
  54.     }
  55.     int getTip() { return tipDerivat; }
  56.     string getNume() { return  nume; }
  57.     int getValoare() {
  58.         return valoare;
  59.     }
  60.     string getStare() {
  61.         return stare;
  62.     }
  63.     void setVal(int val) { valoare = val;  }
  64.     virtual void Modificat() = 0;
  65. };
  66.  
  67.  
  68. class Releu :public  Modul {
  69. private:
  70.     string tip;
  71.     int histereza;
  72.     static int nrMod;
  73. public:
  74.     Releu(string nume, int valoare, string stare, string tip, int histereza) :Modul(0, nume, valoare, stare) {
  75.         this->tip = tip;
  76.         this->histereza = histereza;
  77.     }
  78.     void Afisare() {
  79.         Modul::Afisare();
  80.         cout << tip << " " << histereza << endl;
  81.     }
  82.     void Modificat() {
  83.         nrMod++;
  84.     }
  85.     static void AfMod() { cout << nrMod; }
  86. };
  87. int Releu:: nrMod;
  88. istream & operator>>(istream &in, Releu *&r) {
  89.     string nume, stare;
  90.     int valoare;
  91.     string tip;
  92.     int histereza;
  93.     cout << "Nume: "; in >> nume;
  94.     cout << "Valoare : "; in >> valoare;
  95.     cout << "Stare : "; in >> stare;
  96.     cout << "Tip releu[static/ contact] : "; in >> tip;
  97.     cout << "Histereza : "; in >> histereza;
  98.     r = new Releu(nume, valoare, stare, tip, histereza);
  99.     return in;
  100. }
  101. class Dimmer :public Modul {
  102. private:
  103.     int gama, putere, valPrag;
  104.     static int nrMod;
  105. public:
  106.     Dimmer(string nume, int valoare, string stare, int gama, int putere, int valPrag) :Modul(1, nume, valoare, stare) {
  107.         this->gama = gama;
  108.         this->putere = putere;
  109.         this->valPrag = valPrag;
  110.     }
  111.     void Afisare() {
  112.         Modul::Afisare();
  113.         cout << gama << " " << putere << " " << valPrag << endl;
  114.     }
  115.     void Modificat() {
  116.         nrMod++;
  117.     }
  118.     static void AfMod() { cout << nrMod; }
  119. };
  120. int Dimmer:: nrMod;
  121. istream & operator>>(istream &in, Dimmer *&d) {
  122.     string nume, stare;
  123.     int valoare;
  124.     int gama, putere, valPrag;
  125.     bool ok;
  126.     cout << "Nume: "; in >> nume;
  127.     cout << "Stare : "; in >> stare;
  128.     cout << "Valoare: "; in >> valoare;
  129.     do {
  130.         cout << "Gama : "; in >> gama;
  131.  
  132.         try {
  133.             if (gama < 0 || gama > 100)
  134.                 throw MyException("out_limit");
  135.             ok = true;
  136.         }
  137.         catch (MyException &e) {
  138.             cerr << e.what();
  139.             ok = false;
  140.         }
  141.     } while (ok == false);
  142.     cout << "Putere: "; in >> putere;
  143.  
  144.     cout << " Valare de Prag: "; in >> valPrag;
  145.     d = new Dimmer(nume, valoare, stare, gama, putere, valPrag);
  146.     return in;
  147. }
  148. class Persoana {
  149. private:
  150.     string nume;
  151.     string adresa;
  152.     list<Modul* > ListaModul;
  153. public:
  154.     Persoana(string nume, string adresa) {
  155.         this->nume = nume;
  156.         this->adresa = adresa;
  157.     }
  158.     void Afisare() {
  159.         cout << "Nume: " << nume << endl;
  160.         cout << "Adresa: " << adresa << endl;
  161.  
  162.         for (auto it = ListaModul.begin(); it != ListaModul.end(); it++)(*it)->Afisare();
  163.     }
  164.     bool Cautare(string nume) {
  165.         for (auto it = ListaModul.begin(); it != ListaModul.end(); it++)
  166.             if ((*it)->getNume() == nume) {
  167.                 (*it)->Afisare();
  168.                 return true;
  169.             }
  170.         return false;
  171.  
  172.     }
  173.     void AfisareDefecte() {
  174.         for (auto it = ListaModul.begin(); it != ListaModul.end(); it++)
  175.             if ((*it)->getStare() == "defect") (*it)->Afisare();
  176.     }
  177.     void adaugareModul() {
  178.         int tip;
  179.         Releu *r = NULL;
  180.         Dimmer *d = NULL;
  181.         Modul *m = NULL;
  182.         cout << "Tip de modul [releu- 0  , dimmer - 1]: "; cin >> tip;
  183.         if (tip) {
  184.             cin >> r;
  185.             m = r;
  186.         }
  187.         else
  188.         {
  189.             cin >> d;
  190.             m = d;
  191.         }
  192.  
  193.         list<Modul *>::iterator it = ListaModul.begin();
  194.  
  195.         while (it != ListaModul.end() && (*it)->getValoare() < m->getValoare())
  196.             it++;
  197.  
  198.  
  199.  
  200.  
  201.         ListaModul.emplace(it, m);
  202.     }
  203.     void Sterge(int tip, string nume) {
  204.         if (ListaModul.empty()) return;
  205.         for (auto it = ListaModul.begin(); it != ListaModul.end(); it++) {
  206.             if ((*it)->getTip() == tip && (*it)->getNume() == nume) {
  207.                 ListaModul.erase(it);
  208.                 it = ListaModul.begin();
  209.             }
  210.             if (ListaModul.empty()) return;
  211.  
  212.         }
  213.     }
  214.     string getNume() { return nume; }
  215.     void ModVal(string nume, int val) {
  216.         for (auto it = ListaModul.begin(); it != ListaModul.end(); it++) {
  217.             if ((*it)->getNume() == nume) {
  218.                 (*it)->setVal(val);
  219.                 (*it)->Modificat();
  220.             }
  221.         }
  222.     }
  223.     friend fstream &operator>>(fstream &in, Persoana *&p);
  224. };
  225. istream &operator>>(istream &in, Persoana *&p) {
  226.     string nume, adresa;
  227.     cout << "Nume: "; in >> nume;
  228.     cout << "Adresa: "; in >> adresa;
  229.     p = new Persoana(nume, adresa);
  230.     return in;
  231. }
  232. ostream &operator<<(ostream &out, Persoana *&p) {
  233.     out << linie;
  234.     p->Afisare();
  235.     return out;
  236. }
  237.  
  238. fstream &operator>>(fstream &in, Persoana *&p) {
  239.     string nume, stare;
  240.     int valoare;
  241.     string tip;
  242.     int histereza;
  243.     int gama, putere, valPrag;
  244.     string numePers;
  245.     string adresa;
  246.     in >> numePers;
  247.     in >> adresa;
  248.  
  249.     p = new Persoana(numePers, adresa);
  250.  
  251.  
  252.     int tipDerivat;
  253.     Modul *m = NULL;
  254.     if (in.eof()) return in;
  255.     while (getline(in, nume))
  256.     {
  257.         getline(in, nume);
  258.         if (in.eof() || nume == "\n" || nume == "") return in;
  259.  
  260.         in >> valoare;
  261.         in >> stare;
  262.  
  263.         in >> tipDerivat;
  264.  
  265.         if (tipDerivat) {
  266.  
  267.             in >> gama >> putere >> valPrag;
  268.  
  269.             m = new Dimmer(nume, valoare, stare, gama, putere, valPrag);
  270.         }
  271.         else
  272.         {
  273.             in >> tip >> histereza;
  274.             m = new Releu(nume, valoare, stare, tip, histereza);
  275.         }
  276.         list<Modul *>::iterator it = p->ListaModul.begin();
  277.  
  278.         while (it != p->ListaModul.end() && (*it)->getValoare() < m->getValoare())
  279.             it++;
  280.  
  281.  
  282.  
  283.  
  284.         p->ListaModul.emplace(it, m);
  285.     }
  286.     return in;
  287. }
  288.  
  289.  
  290. int main() {
  291.     int opt, nr, val;
  292.     list<Persoana *> listaPersoana;
  293.     list<Persoana *>::iterator it;
  294.     Persoana *p = NULL;
  295.     fstream f;
  296.     string a, nume;
  297.     bool gasit = 0;
  298.  
  299.     do {
  300.         cout << "0. Iesire\n";
  301.         cout << "1. Adaugare tastatura \n";
  302.         cout << "2. Afisare Consola \n";
  303.         cout << "3. Citire Fiser \n";
  304.         cout << "4. Afisare elemente defecte\n";
  305.         cout << "5. Modificare Valore\n";
  306.         cout << "6. Afisare Modificar\n";
  307.         cout << "Optiunea Aleasa "; cin >> opt;
  308.         system("cls");
  309.         switch (opt)
  310.         {
  311.         case 0: exit(0);
  312.         case 1:
  313.             cout << "Oferta noua -1, sau adaugare de modul - 0 "; cin >> gasit;
  314.             if (gasit) {
  315.                 cin >> p;
  316.             }
  317.             else {
  318.                 cout << "Nume: "; cin >> nume;
  319.                 p = NULL;
  320.                 for (it = listaPersoana.begin(); it != listaPersoana.end(); it++) {
  321.                     if ((*it)->getNume() == nume) {
  322.                         p = *it;
  323.  
  324.                         break;
  325.                     }
  326.                 }
  327.                 if (p == NULL) { cout << "nu a fost gasit!\n"; break; }
  328.             }
  329.             cout << "Numar de module: "; cin >> nr;
  330.             for (int i = 1; i <= nr; i++) {
  331.                 cout << "Modulul " << i << endl;
  332.                 p->adaugareModul();
  333.                 system("cls");
  334.             }
  335.             if (gasit == 1) {
  336.                 it = listaPersoana.begin();
  337.                 while (it != listaPersoana.end() && (*it)->getNume() < p->getNume()) it++;
  338.                 listaPersoana.emplace(it, p);
  339.             }
  340.             gasit = 0;
  341.             break;
  342.         case 2:
  343.             for (it = listaPersoana.begin(); it != listaPersoana.end(); it++)cout << (*it);
  344.             cout << linie;
  345.             break;
  346.         case 3:
  347.             f.open("in.txt", ios::in);
  348.        
  349.             if (f.is_open() == false) break;
  350.             cout << "Datele citie din fiserul in.txt sunt: " << endl;
  351.             while (!f.eof()) {
  352.                 f >> p;
  353.                 if (p->getNume() == "") {
  354.                     delete p;
  355.                     break;
  356.                 }
  357.                 it = listaPersoana.begin();
  358.                 while (it != listaPersoana.end() && (*it)->getNume() < p->getNume()) it++;
  359.                 listaPersoana.emplace(it, p);
  360.                 p->Afisare();
  361.                 cout << endl;
  362.             }
  363.             f.close();
  364.             break;
  365.         case 4:
  366.             for (it = listaPersoana.begin(); it != listaPersoana.end(); it++) (*it)->AfisareDefecte();
  367.             break;
  368.         case 5:
  369.                 cout << "Nume: "; cin >> nume;
  370.                 cout << "Valoare Noua: "; cin >> val;
  371.                 for (it = listaPersoana.begin(); it != listaPersoana.end(); it++) (*it)->ModVal(nume, val);
  372.                 break;
  373.         case 6:
  374.             cout << "\nReleu: ";
  375.             Releu::AfMod();
  376.             cout << "\nDimmer: ";
  377.             Dimmer::AfMod();
  378.             cout << endl;
  379.             break;
  380.         }
  381.     } while (1);
  382.  
  383.     return 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement