Advertisement
Alx09

Untitled

Nov 25th, 2020 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.96 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<list>
  5. #include<iterator>
  6. #include<string>
  7. #include<fstream>
  8. #define _CRT_SECURE_NO_WARNING
  9.  
  10. using namespace std;
  11.  
  12. class Cadou {
  13. private:
  14.     string nume;
  15.     double pret;
  16. public:
  17.     Cadou(string nume, double pret) {
  18.         this->nume = nume;
  19.         this->pret = pret;
  20.     }
  21.    
  22.     virtual void afisare() {
  23.         cout  << nume <<" " << pret << " ";
  24.     }
  25.     string get_nume() { return nume; }
  26.     double get_pret() { return pret; }
  27.    
  28. };
  29.  
  30. class Haina : public Cadou {
  31. private:
  32.     int marime;
  33.     string culoare;
  34. public:
  35.     Haina(string nume, double pret, string culoare, int marime) : Cadou(nume, pret) {
  36.         this->culoare = culoare;
  37.         this->marime = marime;
  38.     }
  39.     void afisare() {
  40.         Cadou::afisare();
  41.         cout << culoare << " " << marime << endl;
  42.     }
  43.  
  44. };
  45.  
  46. class Bijuteri :public Cadou {
  47. private:
  48.     string material;
  49.  
  50. public:
  51.     Bijuteri(string nume, double pret, string material) : Cadou(nume, pret) {
  52.         this->material = material;
  53.  
  54.     }
  55.     void afisare() {
  56.         Cadou::afisare();
  57.         cout << material << endl;
  58.     }
  59.    
  60.  
  61.  
  62. };
  63.  
  64. class Jucarie: public Cadou
  65. {
  66. protected:
  67.     string brand;
  68. public:
  69.     Jucarie(string nume, double pret, string brand) : Cadou(nume, pret) {
  70.         this->brand = brand;
  71.     }
  72.     void afisare() {
  73.         Cadou::afisare();
  74.         cout << brand << endl;
  75.     }
  76. };
  77.  
  78. list <Cadou *> listaCadou;
  79. list <Cadou *>::iterator it;
  80.  
  81. void init()
  82. {
  83.     int marime;
  84.     double pret;
  85.     string nume, brand, material, culoare;
  86.     char tip;
  87.     cout << "\nCe fel de cadou H- haina, B- Bijutire, -J -Jucarie: "; cin >> tip;
  88.     cout << "Nume: "; cin >> nume;
  89.     cout << "Pret: "; cin >> pret;
  90.         if (tip == 'H') {
  91.  
  92.             cout << "Culoare: ";    cin >> culoare;
  93.             cout << "Marime: "; cin  >> marime;
  94.             if (listaCadou.empty())
  95.                 listaCadou.push_back(new Haina(nume, pret, culoare, marime));
  96.             else {
  97.                 it = listaCadou.begin();
  98.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  99.                     advance(it, 1);
  100.                 listaCadou.emplace(it, new Haina(nume, pret, culoare, marime));
  101.             }
  102.         }
  103.         if (tip == 'B') {
  104.             cout << "Material: "; cin >> material;
  105.             if (listaCadou.empty())
  106.                 listaCadou.push_back(new Bijuteri(nume, pret, material));
  107.             else {
  108.                 it = listaCadou.begin();
  109.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  110.                     advance(it, 1);
  111.                 listaCadou.emplace(it, new Bijuteri(nume, pret, material));
  112.             }
  113.         }
  114.         if (tip == 'J') {
  115.             cout << "brand: ";  cin >> brand;
  116.  
  117.             if (listaCadou.empty())
  118.                 listaCadou.push_back(new Jucarie(nume, pret, brand));
  119.             else {
  120.                 it = listaCadou.begin();
  121.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  122.                     advance(it, 1);
  123.                 listaCadou.emplace(it, new Jucarie(nume, pret, brand));
  124.             }
  125.        
  126.         }
  127.  
  128.  
  129. }
  130.  
  131. void stergere() {
  132.     string nume;
  133.     cout << "\nNume cadou: ";
  134.     cin >> nume;
  135.     for (it = listaCadou.begin(); it != listaCadou.end(); it++)
  136.         if ((*it)->get_nume() == nume) {
  137.             listaCadou.erase(it);
  138.             cout << " Cadoul " << nume << " a fost sters!" << endl;
  139.             return;
  140.         }
  141.     cout << " Cadoul" << nume << " nu se afla in baza de date" << endl;
  142.  
  143. }
  144.  
  145. void calcul_pret_cadouri() {
  146.      double suma = 0.f;
  147.     for (it = listaCadou.begin(); it != listaCadou.end(); it++) suma = suma + (*it)->get_pret();           
  148. }
  149.  
  150. void cautareCadou() {
  151.      bool ok = 0;
  152.      string nume;
  153.     cout << " Care este denumirea cadoului?: ";
  154.     cin >> nume;
  155.     for (it = listaCadou.begin(); it != listaCadou.end(); it++)
  156.         if ((*it)->get_nume() == nume) {
  157.             (*it)->afisare();
  158.             return;
  159.         }
  160.     cout << "\n Caodul nu se afla pe lista";
  161.  
  162. }
  163.  
  164. void citire_fisier(string numeFiser)
  165. {
  166.     ifstream f(numeFiser);
  167.     int marime;
  168.     double pret;
  169.     string nume, brand, material, culoare;
  170.     char tip;
  171.     if (!f) {
  172.         cout << "\n Nu exista fiserul " <<  numeFiser;
  173.         return;
  174.     }
  175.     while (!f.eof()) {
  176.         f >> tip;
  177.         f >> nume;
  178.         f >> pret;
  179.         if (tip == 'H') {
  180.             f >> culoare;
  181.             f >> marime;
  182.             if (listaCadou.empty())
  183.                 listaCadou.push_back(new Haina(nume, pret, culoare, marime));
  184.             else {
  185.                 it = listaCadou.begin();
  186.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  187.                     advance(it, 1);
  188.                 listaCadou.emplace(it, new  Haina(nume, pret, culoare, marime));
  189.             }
  190.         }
  191.         if (tip == 'B') {
  192.             f >> material;
  193.             if (listaCadou.empty())
  194.                 listaCadou.push_back(new Bijuteri(nume, pret, material));
  195.             else {
  196.                 it = listaCadou.begin();
  197.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  198.                     advance(it, 1);
  199.                 listaCadou.emplace(it, new Bijuteri(nume, pret, material));
  200.             }
  201.         }
  202.         if (tip == 'J') {
  203.             f >> brand;
  204.            
  205.             if (listaCadou.empty())
  206.                 listaCadou.push_back(new Jucarie(nume, pret,  brand));
  207.             else {
  208.                 it = listaCadou.begin();
  209.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  210.                     advance(it, 1);
  211.                 listaCadou.emplace(it, new Jucarie(nume, pret, brand));
  212.             }
  213.         }
  214.     }
  215.     cout << "\n Datele au fost citite cu succes!";
  216.     f.close();
  217.     //else throw "Eroare la citirea din fisier";
  218. }
  219.  
  220. void show() {
  221.     cout << "=============================" << endl;
  222.     for (it = listaCadou.begin(); it != listaCadou.end(); it++) {
  223.         (*it)->afisare();
  224.         cout << "----------------------------- \n";
  225.     }
  226.     cout << "=============================" << endl;
  227. }
  228.  
  229.  
  230. int main() {
  231.     int opt;
  232.     do {
  233.         cout << "\n\n";
  234.         cout << "1.Citire cadouuri din fiser\n";
  235.         cout << "2.Afis cadouri \n";
  236.         cout << "3.Afisare date despre un cadou \n";
  237.         cout << "4.Calculul facutri pentru cadouri\n";
  238.         cout << "5.Stergerea unui cadou de pe lista\n";
  239.         cout << "6. Adaugare cadou de la tastautra\n";
  240.         cout << "0.Iesire \n";
  241.         cout << "Dati optiunea dvs: ";
  242.         cin >> opt;
  243.         system("cls");
  244.        
  245.         switch (opt) {
  246.         case 1:
  247.            
  248.             citire_fisier("cadou.txt");
  249.             break;
  250.         case 2:
  251.             show();
  252.             break;
  253.         case 3:
  254.             cautareCadou();
  255.             break;
  256.         case 4:
  257.             calcul_pret_cadouri();
  258.             break;
  259.         case 5:
  260.             stergere();
  261.             break;
  262.         case 6:
  263.             init();
  264.             break;
  265.         case 0:
  266.             return 0;
  267.             break;
  268.         default:
  269.             cout << "\nOptiune Invalida";
  270.             break;
  271.  
  272.         }
  273.     } while (opt != 0);
  274.     return 0;
  275. }
  276.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement