Alx09

Untitled

Feb 8th, 2021
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <iterator>
  4. #include <conio.h>
  5. #include <string>
  6. #include <exception>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. class MyException : public exception {
  23. private:
  24.     string mesajEroare;
  25. public:
  26.  
  27.     MyException(string mesajEroare) {
  28.  
  29.         this->mesajEroare = mesajEroare;
  30.     }
  31.     const char* what() const throw() {
  32.         if (mesajEroare == "out_limit") return "Valoarea este inafara intervalului";
  33.         if (mesajEroare == "lista_goala") return "lista este goala";
  34.         return "Eroare neprevazuta";
  35.     }
  36. };
  37. ostream & linie(ostream &out) {
  38.     out << "------------------------\n";
  39.     return out;
  40. }
  41.  
  42. class C1 {
  43. private:
  44.     string nume;
  45.     int tipDerivat;
  46. public :
  47.     C1(int tipDerivat, string nume) {
  48.         this->tipDerivat = tipDerivat;
  49.         this->nume = nume;
  50.     }
  51.     virtual void Afisare() {
  52.         cout << tipDerivat   << " | " << nume   << " "<<   << " " <<   << " "<<   << " ";}
  53.     int getTip() { return tipDerivat; }
  54.     string getNume() { return  nume; }
  55. };
  56.  
  57.  
  58. class D1 :public  C1 {
  59. private:
  60.  
  61. public:
  62.     D1(string nume) :C1(0, nume) {
  63.        
  64.     }
  65.     void Afisare() {
  66.         C1::Afisare();
  67.         cout <<  << " "<<   << " " <<   << " "<<   << " "<<endl;}
  68.  
  69. };
  70. istream & operator>>(istream &in, D1 *&d1) {
  71.     string nume;
  72.    
  73.     bool ok;
  74.     cout << "Nume: "; in >> nume ;
  75.     cout << " : "; in >> ;
  76.     cout << " : "; in >>  ;
  77.     do {
  78.         try {
  79.              
  80.             throw MyException("out_limit");
  81.             ok = true;
  82.         }
  83.         catch (MyException &e) {
  84.             cerr << e.what() << endl;
  85.             ok = false;
  86.         }
  87.     } while (ok == false);
  88.     d1 =  new D1(nume);
  89.     return in;
  90. }
  91. class D2 :public C1 {
  92. private:
  93.    
  94. public:
  95.     D2(string nume) :C1(1, nume) {
  96.  
  97.     }
  98.     void Afisare() {
  99.         C1::Afisare();
  100.         cout <<  << " "<<   << " " <<   << " "<<   << " " << endl;
  101.     }
  102. };
  103. istream & operator>>(istream &in, D2 *&d2) {
  104.     string nume;
  105.    
  106.     bool ok;
  107.     cout << "Nume: "; in >> nume;
  108.     cout << " : "; in >> ;
  109.     cout << " : "; in >> ;
  110.     do {
  111.         try {
  112.              throw MyException("out_limit");
  113.             ok = true;
  114.         }
  115.         catch (MyException &e) {
  116.             cerr << e.what();
  117.             ok = false;
  118.         }
  119.     } while (ok == false);
  120.  
  121.  
  122.     d2 = new D2(nume);
  123.     return in;
  124. }
  125. class C2 {
  126. private:
  127.     string nume;
  128.     list<C1* > l1;
  129. public:
  130.     C2(string nume) {
  131.         this->nume = nume;
  132.     }
  133.     void Afisare() {
  134.         cout << "Nume: " << nume << endl;
  135.         cout << " : " <<  << endl;
  136.        
  137.         for (auto it = l1.begin(); it != l1.end(); it++)(*it)->Afisare();
  138.     }
  139.     bool Cautare(string nume) {
  140.         for (auto it = l1.begin(); it != l1.end(); it++)
  141.             if ((*it)->getNume() ==nume) {
  142.                 (*it)->Afisare();
  143.                 return true;
  144.             }
  145.         return false;
  146.  
  147.     }
  148.     void adaugare_cazare() {
  149.         int tip;
  150.         D1 *d1 = NULL;
  151.         D2 *d2 = NULL;
  152.         C1 *c1 = NULL;
  153.         cout << "Tip de cazare [- 0 ,  - 1]: "; cin >> tip;
  154.         if (tip) {
  155.             cin >> d1;
  156.             c1 = d1;
  157.         }
  158.         else
  159.         {
  160.             cin >> d2;
  161.             c1 = d2;
  162.         }
  163.  
  164.         list<C1* >::iterator it = l1.begin();
  165.         while (it != l1.end() && (*it)->getNume() < c1->getNume()) it++;
  166.         l1.emplace(it, c1);
  167.     }
  168.     void Sterge(int tip, string nume) {
  169.         if (l1.empty()) return;
  170.         for (auto it = l1.begin(); it != l1.end(); it++) {
  171.             if ((*it)->getTip() == tip && (*it)->getNume() == nume)
  172.                 l1.erase(it);
  173.             if (l1.empty()) return;
  174.             if (it == l1.end()) return;
  175.         }
  176.     }
  177.     string getNume() { return nume; }
  178.  
  179.     friend fstream &operator>>(fstream &in, C2 *&c2);
  180. };
  181. istream &operator>>(istream &in, C2 *&c2) {
  182.     string nume;
  183.     cout << "Denumire: "; in >> nume;
  184.    
  185.     c2 = new C2(nume);
  186.     return in;
  187. }
  188. ostream &operator<<(ostream &out, C2 *&c2) {
  189.     out << linie;
  190.     c2->Afisare();
  191.     return out;
  192. }
  193.  
  194. fstream &operator>>(fstream &in, C2 *&c2) {
  195.     string denumire;
  196.    
  197.     in >> ;
  198.     in >> ;
  199.     in >> ;
  200.     c2 = new C2(denumire);
  201.     string nume;
  202.    
  203.     int tipDerivat;
  204.     C1 *c1 = NULL;
  205.     if (in.eof()) return in;
  206.     while (getline(in, nume))
  207.     {
  208.         getline(in, nume);
  209.         if (in.eof() || nume == "\n" || nume == "") return in;
  210.  
  211.         in >> ;
  212.         in >> ;
  213.         in >> tipDerivat;
  214.         if (tipDerivat) {
  215.            
  216.            
  217.             c1 = new D2(nume);
  218.         }
  219.         else
  220.         {
  221.            
  222.             c1 = new D1(nume);
  223.         }
  224.         list<C1 *>::iterator it = c2->l1.begin();
  225.         while (it != c2->l1.end() && (*it)->getNume() < c2->getNume()) it++;
  226.         c2->l1.emplace(it, c1);
  227.  
  228.     }
  229.     return in;
  230. }
  231.  
  232.  
  233. int main(){
  234.     int opt, nr;
  235.     list<C2 *> l2;
  236.     list<C2 *>::iterator it;
  237.     C2 *c2 = NULL;
  238.     fstream f;
  239.     string a, nume;
  240.     bool gasit = 0;
  241.    
  242.     do {
  243.         cout << "0. Iesire\n";
  244.         cout << "1. Adaugare tastatura \n";
  245.         cout << "2. Afisare Consola \n";
  246.         cout << "3. Citire Fiser \n";
  247.         cout << "4. Cautare  \n";
  248.         cout << "5. Stergere\n";
  249.         cout << "Optiunea Aleasa "; cin >> opt;
  250.         system("cls");
  251.         switch (opt)
  252.         {
  253.         case 0: exit(0);
  254.         case 1:
  255.             cout << "Oferta noua -1, sau adaugare de modul - 0 "; cin >> gasit;
  256.             if (gasit) {
  257.                 cin >> c2;
  258.             }
  259.             else {
  260.                 cout << "Nume: "; cin >> nume;
  261.                 c2 = NULL;
  262.                 for (it = l2.begin(); it != l2.end(); it++) {
  263.                     if ((*it)->getNume() == nume) {
  264.                         c2 = *it;
  265.  
  266.                         break;
  267.                     }
  268.                 }
  269.                 if (c2 == NULL) { cout << "nu a fost gasit!\n"; break; }
  270.             }
  271.             cout << "Numar de module: "; cin >> nr;
  272.             for (int i = 1; i <= nr; i++) {
  273.                 cout << "Cazarea " << i << endl;
  274.                 c2->adaugare_cazare();
  275.                 system("cls");
  276.             }
  277.             it = l2.begin();
  278.             while (it != l2.end() && (*it)->getNume() < c2->getNume()) it++;
  279.             l2.emplace(it, c2);
  280.             break;
  281.         case 2:
  282.             for (it = l2.begin(); it != l2.end(); it++)cout << (*it);
  283.             cout << linie;
  284.             break;
  285.         case 3:
  286.             f.open("in.txt", ios::in);
  287.             if (f.is_open() == false) break;
  288.             cout << "Datele citie din fiserul in.txt sunt: " << endl;
  289.             while (!f.eof()) {
  290.                 f >> c2;
  291.                 if (c2->getNume() == "") {
  292.                     delete c2;
  293.                     break;
  294.                 }
  295.                 it = l2.begin();
  296.                 while (it != l2.end() && (*it)->getNume() < c2->getNume()) it++;
  297.                 l2.emplace(it, c2);
  298.                 c2->Afisare();
  299.                 cout << endl;
  300.             }
  301.             f.close();
  302.             break;
  303.  
  304.         case 4:
  305.  
  306.             cout << "Nume: "; cin >> nume;
  307.             for (it = l2.begin(); it != l2.end(); it++)
  308.                 if ((*it)->Cautare(nume)) {
  309.                     gasit = 1;
  310.                     break;
  311.                 }
  312.             if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
  313.             gasit = 0;
  314.             break;
  315.         case 5:
  316.             cout << "Stergere dupa  [ - 0,  -1]: "; cin >> gasit;
  317.  
  318.             if (gasit) {
  319.                 cin >> nume;
  320.                 for (it = l2.begin(); it != l2.end(); it++) (*it)->Sterge(1, nume);
  321.             }
  322.             else {
  323.                 cin >> nume;
  324.                 for (it = l2.begin(); it != l2.end(); it++) (*it)->Sterge(0, nume);
  325.             }
  326.             gasit = 0;
  327.             break;
  328.         }
  329.     } while (1);
  330.    
  331.     return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment