Advertisement
Alx09

Untitled

Dec 8th, 2020
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.48 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string>
  5. #include<time.h>
  6. #define _CRT_SECURE_NO_WARNING
  7. using namespace std;
  8.  
  9.  typedef struct data{
  10.     unsigned short zi, luna, an;
  11. }data;
  12.  
  13.  typedef struct adresa {
  14.      string localitate, judet, strada, codPostal;
  15.      unsigned short nr;
  16.  }adresa;
  17.  unsigned short GetAge(struct data &dataNastere) {
  18.      unsigned short age;
  19.      time_t t = time(NULL);
  20.      struct tm tm = *localtime(&t);
  21.      age = tm.tm_year + 1900  - dataNastere.an;
  22.      if (tm.tm_mon + 1 > dataNastere.luna) age--;
  23.      else if (tm.tm_mon + 1 == dataNastere.luna && tm.tm_mday > dataNastere.zi)age--;
  24.      return age;
  25.  }
  26.  unsigned short GetYear(unsigned short year) {
  27.      time_t t = time(NULL);
  28.      struct tm tm = *localtime(&t);
  29.      if (tm.tm_year  < year) return 1900 + year;
  30.      else
  31.          return tm.tm_year + 1900 - year;
  32.  }
  33.  class Persoana;
  34.  class Cadou
  35.  {
  36.  protected:
  37.      static Cadou *headCadou;
  38.      Cadou *next;
  39.      string nume, tip;
  40.      double pret;
  41.  public:
  42.      Cadou(string nume, string tip, double pret);
  43.      static void AdaugaCadou(Persoana *q, char*CNP);
  44.      virtual void Afisare() = 0;
  45.      
  46.  };
  47.  Cadou * Cadou::headCadou = NULL;
  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.     static Persoana *head;
  103.     Persoana *next;
  104.     Cadou *headCadou;
  105.     string nume;
  106.     char CNP[13];
  107.     unsigned short varsta;
  108.     bool isMan;
  109.     struct data dataNastere;
  110.     struct adresa adresa;
  111. public:
  112.     Persoana(string nume, char *CNP, struct adresa &adresa) {
  113.         this->nume = nume;
  114.         strcpy(this->CNP, CNP);
  115.         if (CNP[0] == '1' || '5') isMan = true;
  116.         else isMan = false;
  117.         dataNastere.an = GetYear((CNP[1]-48) * 10 + (CNP[2]- 48));
  118.         dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
  119.         dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
  120.         this->adresa.codPostal = adresa.codPostal;
  121.         this->adresa.judet = adresa.judet;
  122.         this->adresa.localitate = adresa.localitate;
  123.         this->adresa.nr = adresa.nr;
  124.         this->adresa.strada = adresa.strada;
  125.         this->next = NULL;
  126.         this->headCadou = NULL;
  127.         if (head == NULL) {
  128.             head = this;
  129.             return;
  130.         }
  131.         if (head->nume > this->nume) {
  132.             this->next = head;
  133.             head = this;
  134.             return;
  135.         }
  136.         Persoana *q = head;
  137.         while (q->next && q->next->nume < this->nume) q = q->next;
  138.         this->next = q->next;
  139.         q->next = this;
  140.     }
  141.     static void Adauga_cadou(char *CNP) {
  142.         Persoana *q = head;
  143.         while (q && strcmp(q->CNP, CNP))
  144.             q = q->next;
  145.         if (!q) {
  146.             cout << "\nPersoana cu CNP: " << CNP << " nu se afla in baza de date";
  147.             return;
  148.         }
  149.        
  150.     }
  151.     friend static void Cadou::AdaugaCadou(Persoana *q, char*CNP);
  152. };
  153.  
  154. Persoana * Persoana::head = NULL;
  155.  
  156. Cadou::Cadou(string nume, string tip, double pret)
  157. {
  158.     this->nume = nume;
  159.     this->pret = pret;
  160.     this->tip = tip;
  161.     this->next = NULL;
  162.    
  163.     Cadou *q = headCadou;
  164.     if (headCadou == NULL) {
  165.         headCadou = this;
  166.         return;
  167.     }
  168.     if (headCadou->nume > this->nume) {
  169.         this->next = headCadou;
  170.         headCadou = this;
  171.         return;
  172.     }
  173.  
  174.     while (q->next && q->next->nume < this->nume) q = q->next;
  175.     this->next = q->next;
  176.     q->next = this;
  177.  
  178. }
  179.  
  180. void Cadou::AdaugaCadou(Persoana *q, char *CNP) {
  181.     if (q == NULL) {
  182.         q = Persoana::head;
  183.         while (q && strcmp(q->CNP, CNP)) q = q->next;
  184.         try
  185.         {
  186.             if (Persoana::head == NULL) throw 1;
  187.             if (q == NULL) throw 2;
  188.            
  189.  
  190.         }
  191.         catch (int error)
  192.         {
  193.             switch (error)
  194.             {
  195.             case 1:     cout << "\nLista este goala"; break;
  196.             case 2: cout << "\nNu exista vreo persoana cu CNP: " << CNP << " in baza de date";
  197.             default:
  198.                 break;
  199.             }
  200.            
  201.             return;
  202.  
  203.         }
  204.  
  205.        
  206.     }
  207.     Dulciuri *dulce;
  208.     Jucarii *jucarie;
  209.     PachetCadou *pachetcadou;
  210.     int opt;
  211. }
  212.  
  213.  
  214.  
  215. int main() {
  216.    
  217.     bool help;
  218.     int opt;
  219.     do {
  220.         cout << "\n\n";
  221.         cout << "1.Adaugare Persoana\n";
  222.         cout << "2.Afis  persoane \n";
  223.         cout << "0.Iesire \n";
  224.         cout << "Dati optiunea dvs: ";
  225.         cin >> opt;
  226.         system("cls");
  227.         switch (opt) {
  228.         case 1:
  229.        
  230.             break;
  231.         case 2:
  232.        
  233.             break;
  234.  
  235.         case 0:
  236.             break;
  237.  
  238.         }
  239.     } while (opt != 0);
  240.     return 0;
  241. }
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement