Advertisement
Alx09

Untitled

Dec 11th, 2020
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.17 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string>
  5. #include<time.h>
  6. #include<list>
  7. #include<iterator>
  8. #define _CRT_SECURE_NO_WARNING
  9. using namespace std;
  10.  
  11. typedef struct data {
  12.     unsigned short zi, luna, an;
  13. }data;
  14.  
  15. typedef struct adresa {
  16.     string localitate, judet, strada, codPostal;
  17.     unsigned short nr;
  18. }adresa;
  19. unsigned short GetAge(struct data &dataNastere) {
  20.     unsigned short age;
  21.     time_t t = time(NULL);
  22.     struct tm tm = *localtime(&t);
  23.     age = tm.tm_year + 1900 - dataNastere.an;
  24.     if (tm.tm_mon + 1 > dataNastere.luna) age--;
  25.     else if (tm.tm_mon + 1 == dataNastere.luna && tm.tm_mday > dataNastere.zi)age--;
  26.     return age;
  27. }
  28. unsigned short GetYear(unsigned short year) {
  29.     time_t t = time(NULL);
  30.     struct tm tm = *localtime(&t);
  31.     if (tm.tm_year < year) return 1900 + year;
  32.     else
  33.         return tm.tm_year + 1900 - year;
  34. }
  35. class Persoana;
  36. class Cadou
  37. {
  38. protected:
  39.     string nume, tip;
  40.     double pret;
  41. public:
  42.     Cadou(string nume, string tip, double pret);
  43.     virtual void Afisare() = 0;
  44.     string get_nume() {
  45.         return nume;
  46.     }
  47. };
  48. class Jucarii : public Cadou {
  49. private:
  50.     string marca;
  51.     bool bateriNecesare;
  52.     unsigned short varstaRecomandata;
  53. public:
  54.     Jucarii(string nume, string tip, double pret, string marca, bool bateriNecesare, unsigned short varstaRecomandata) :Cadou(nume, tip, pret) {
  55.         this->marca = marca;
  56.         this->bateriNecesare = bateriNecesare;
  57.         this->varstaRecomandata = varstaRecomandata;
  58.     }
  59.     void Afisare() {
  60.         cout << "\n" << nume << " " << marca << " " << tip;
  61.         if (bateriNecesare) cout << " bateri necesare ";
  62.         else cout << " nu sunt necesare bateri ";
  63.         cout << varstaRecomandata << " " << pret;
  64.     }
  65. };
  66. class PachetCadou : public Cadou {
  67. private:
  68.  
  69.     bool pentruBarbati;
  70.     unsigned short nrProd;
  71. public:
  72.     PachetCadou(string nume, string tip, double pret, bool pentruBarbati, unsigned short nrProd) :Cadou(nume, tip, pret) {
  73.         this->pentruBarbati = pentruBarbati;
  74.         this->nrProd = nrProd;
  75.     }
  76.     void Afisare() {
  77.         cout << "\n" << nume << " " << " " << tip << " " << nrProd;
  78.         if (pentruBarbati) cout << " de barbati ";
  79.         else cout << " de femei ";
  80.         cout << pret;
  81.     }
  82. };
  83. class Dulciuri : public Cadou {
  84. private:
  85.  
  86.     string ingrediente;
  87.     double calori, gramaj;
  88. public:
  89.     Dulciuri(string nume, string tip, double pret, string ingrediente, double calori, double gramaj) :Cadou(nume, tip, pret) {
  90.         this->ingrediente = ingrediente;
  91.         this->calori = calori;
  92.         this->gramaj = gramaj;
  93.     }
  94.     void Afisare() {
  95.         cout << "\n" << nume << " " << " " << tip << " " << gramaj << " " << round(calori * 100 / gramaj) << " " << ingrediente << " " << pret;
  96.  
  97.     }
  98. };
  99.  
  100. class Persoana {
  101. private:
  102.    
  103.     string nume;
  104.     char CNP[13];
  105.     unsigned short varsta;
  106.     bool isMan;
  107.     struct data dataNastere;
  108.     struct adresa adresa;
  109.     list <Cadou *> listaCadou;
  110. public:
  111.     Persoana(string nume, char *CNP, struct adresa &adresa) {
  112.         this->nume = nume;
  113.         strcpy(this->CNP, CNP);
  114.         if (CNP[0] == '1' || '5') isMan = true;
  115.         else isMan = false;
  116.         dataNastere.an = GetYear((CNP[1] - 48) * 10 + (CNP[2] - 48));
  117.         dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
  118.         dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
  119.         this->adresa.codPostal = adresa.codPostal;
  120.         this->adresa.judet = adresa.judet;
  121.         this->adresa.localitate = adresa.localitate;
  122.         this->adresa.nr = adresa.nr;
  123.         this->adresa.strada = adresa.strada;
  124.  
  125.     }
  126.     void CitireCadou();
  127. };
  128.  
  129.  
  130. Cadou::Cadou(string nume, string tip, double pret)
  131. {
  132.     this->nume = nume;
  133.     this->pret = pret;
  134.     this->tip = tip;
  135.  
  136. }
  137.  
  138. void Persoana::CitireCadou()
  139. {
  140.     list <Cadou *>::iterator it;
  141.     string nume, tip;
  142.     double pret;
  143.     char t;
  144.     cout << "\nCe fel de cadou D- Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
  145.     cout << "Nume: "; cin >> nume;
  146.     cout << "Pret: "; cin >> pret;
  147.     cout << "Tip: "; cin >> tip;
  148.    
  149.     if (t == 'P') {
  150.         string ingrediente;
  151.         double calori, gramaj;
  152.         cout << "Ingerdiente: ";   cin >> ingrediente;
  153.         cout << "Calori: ";     cin >>  calori;
  154.         cout << "Gramaj: "; cin >> gramaj;
  155.         if (listaCadou.empty())
  156.             listaCadou.push_back(new Dulciuri(nume, tip, pret,ingrediente, calori, gramaj));
  157.         else {
  158.             it = listaCadou.begin();
  159.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  160.                 advance(it, 1);
  161.             listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  162.         }
  163.     }
  164.     if (t == 'P') {
  165.    
  166.         bool pentruBarbati;
  167.         unsigned short nrProd;
  168.         cout << "Este pentru femei - 0, barbati - 1: ";   cin >> pentruBarbati;
  169.         cout << "Numar de produse in pachet: "; cin >> nrProd;
  170.         if (listaCadou.empty())
  171.             listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  172.         else {
  173.             it = listaCadou.begin();
  174.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  175.                 advance(it, 1);
  176.             listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  177.         }
  178.     }
  179.     if (t == 'J') {
  180.  
  181.         string marca;
  182.         bool bateriNecesare;
  183.         unsigned short varstaRecomandata;
  184.         cout << "Marca: ";   cin >> marca;
  185.         cout << "Are nevoie de bateri Nu - 0, Da - 1: ";   cin >> bateriNecesare;
  186.         cout << "Varsta recomandata: "; cin >> varstaRecomandata;
  187.         if (listaCadou.empty())
  188.             listaCadou.push_back(new Jucarii(nume, tip, pret, marca,  bateriNecesare, varstaRecomandata));
  189.         else {
  190.             it = listaCadou.begin();
  191.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  192.                 advance(it, 1);
  193.             listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  194.         }
  195.     }
  196.    
  197.    
  198. }
  199.  
  200. int main() {
  201.     string nume;
  202.     char CNP[20];
  203.     unsigned short varsta;
  204.     bool isMan;
  205.     struct data dataNastere;
  206.     struct adresa adresa;
  207.  
  208.     int opt;
  209.     do {
  210.         cout << "\n\n";
  211.         cout << "1.Adaugare Persoana si lista de cadouri\n";
  212.         cout << "2.Afisare  persoane si lista caduri sub forma tabelara\n";
  213.         cout << "0.Iesire \n";
  214.         cout << "Dati optiunea dvs: ";
  215.         cin >> opt;
  216.         system("cls");
  217.         switch (opt) {
  218.         case 1:
  219.             cout << "Nume persoana: "; cin >> nume;
  220.             cout << "CNP: "; cin >> CNP;
  221.             try
  222.             {
  223.                 if (strlen(CNP) < 12) throw 1;
  224.  
  225.             }
  226.             catch (int error)
  227.             {
  228.                 if (error == 1) cout << "CNP are prea putine cifre";
  229.  
  230.             }
  231.  
  232.             break;
  233.         case 2:
  234.  
  235.             break;
  236.  
  237.         case 0:
  238.             break;
  239.  
  240.         }
  241.     } while (opt != 0);
  242.     return 0;
  243. }
  244.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement