Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string>
- #include<time.h>
- #include<list>
- #include<iterator>
- #define _CRT_SECURE_NO_WARNING
- using namespace std;
- typedef struct data {
- unsigned short zi, luna, an;
- }data;
- typedef struct adresa {
- string localitate, judet, strada, codPostal;
- unsigned short nr;
- }adresa;
- unsigned short GetAge(struct data &dataNastere) {
- unsigned short age;
- time_t t = time(NULL);
- struct tm tm = *localtime(&t);
- age = tm.tm_year + 1900 - dataNastere.an;
- if (tm.tm_mon + 1 < dataNastere.luna) age--;
- else if (tm.tm_mon + 1 == dataNastere.luna && tm.tm_mday < dataNastere.zi)age--;
- return age;
- }
- unsigned short GetYear(unsigned short year) {
- time_t t = time(NULL);
- struct tm tm = *localtime(&t);
- if (tm.tm_year % 100 < year) return 1800 + tm.tm_year + year - tm.tm_year % 100;
- else
- return tm.tm_year + 1900 - year- tm.tm_year % 100;
- }
- class Persoana;
- class Cadou
- {
- protected:
- string nume, tip;
- double pret;
- public:
- Cadou(string nume, string tip, double pret);
- virtual void Afisare() = 0;
- string get_nume() {
- return nume;
- }
- };
- class Jucarii : public Cadou {
- private:
- string marca;
- bool bateriNecesare;
- unsigned short varstaRecomandata;
- public:
- Jucarii(string nume, string tip, double pret, string marca, bool bateriNecesare, unsigned short varstaRecomandata) :Cadou(nume, tip, pret) {
- this->marca = marca;
- this->bateriNecesare = bateriNecesare;
- this->varstaRecomandata = varstaRecomandata;
- }
- void Afisare() {
- cout << "\n" << nume << " " << marca << " " << tip;
- if (bateriNecesare) cout << " bateri necesare ";
- else cout << " nu sunt necesare bateri ";
- cout << varstaRecomandata << " " << pret;
- }
- };
- class PachetCadou : public Cadou {
- private:
- bool pentruBarbati;
- unsigned short nrProd;
- public:
- PachetCadou(string nume, string tip, double pret, bool pentruBarbati, unsigned short nrProd) :Cadou(nume, tip, pret) {
- this->pentruBarbati = pentruBarbati;
- this->nrProd = nrProd;
- }
- void Afisare() {
- cout << "\n" << nume << " " << " " << tip << " " << nrProd;
- if (pentruBarbati) cout << " de barbati ";
- else cout << " de femei ";
- cout << pret;
- }
- };
- class Dulciuri : public Cadou {
- private:
- string ingrediente;
- double calori, gramaj;
- public:
- Dulciuri(string nume, string tip, double pret, string ingrediente, double calori, double gramaj) :Cadou(nume, tip, pret) {
- this->ingrediente = ingrediente;
- this->calori = calori;
- this->gramaj = gramaj;
- }
- void Afisare() {
- cout << "\n" << nume << " " << " " << tip << " " << gramaj << " " << round(calori * 100 / gramaj) << " " << ingrediente << " " << pret;
- }
- };
- class Persoana {
- private:
- string nume;
- char CNP[13];
- unsigned short varsta;
- bool isMan;
- struct data dataNastere;
- struct adresa adresa;
- list <Cadou *> listaCadou;
- double cheltuitCadouri;
- public:
- Persoana(string nume, char *CNP, struct adresa &adresa) {
- this->nume = nume;
- strcpy(this->CNP, CNP);
- if (CNP[0] == '1' || CNP[0] == '5') isMan = true;
- else isMan = false;
- dataNastere.an = GetYear((CNP[1] - 48) * 10 + (CNP[2] - 48));
- dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
- dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
- this->varsta = GetAge(dataNastere);
- this->adresa.codPostal = adresa.codPostal;
- this->adresa.judet = adresa.judet;
- this->adresa.localitate = adresa.localitate;
- this->adresa.nr = adresa.nr;
- this->adresa.strada = adresa.strada;
- this->cheltuitCadouri = 0.f;
- }
- void CitireCadou();
- char *get_CNP() {
- return CNP;
- }
- string get_nume() {
- return nume;
- }
- void Afisare() {
- cout << "------------------------------------------------------------------------------------------\n";
- cout << nume << " " << CNP<< varsta;
- if (isMan) cout << " barbat\n";
- else cout << " femeie\n";
- cout << varsta<<" "<< dataNastere.zi << "/" << dataNastere.luna << "/" << dataNastere.an<<endl;
- cout << adresa.localitate << " " << adresa.judet <<" "<< adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
- cout << "\nLista de cadouri:\n";
- cout << "=============================" << endl;
- for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) (*it)->Afisare();
- cout << "\n=============================" << endl;
- }
- };
- list <Persoana *> listaPersone;
- list <Persoana *>::iterator it;
- Cadou::Cadou(string nume, string tip, double pret)
- {
- this->nume = nume;
- this->pret = pret;
- this->tip = tip;
- }
- void Persoana::CitireCadou()
- {
- list <Cadou *>::iterator it;
- string nume, tip;
- double pret;
- char t;
- getchar();
- cout << "\nCe fel de cadou D- Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
- cout << "Nume: "; cin >> nume;
- cout << "Pret: "; cin >> pret;
- cout << "Tip: "; cin >> tip;
- cheltuitCadouri += pret;
- if (t == 'P') {
- string ingrediente;
- double calori, gramaj;
- cout << "Ingerdiente: "; cin >> ingrediente;
- cout << "Calori: "; cin >> calori;
- cout << "Gramaj: "; cin >> gramaj;
- if (listaCadou.empty())
- listaCadou.push_back(new Dulciuri(nume, tip, pret,ingrediente, calori, gramaj));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
- }
- }
- if (t == 'P') {
- bool pentruBarbati;
- unsigned short nrProd;
- cout << "Este pentru femei - 0, barbati - 1: "; cin >> pentruBarbati;
- cout << "Numar de produse in pachet: "; cin >> nrProd;
- if (listaCadou.empty())
- listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
- }
- }
- if (t == 'J') {
- string marca;
- bool bateriNecesare;
- unsigned short varstaRecomandata;
- cout << "Marca: "; cin >> marca;
- cout << "Are nevoie de bateri Nu - 0, Da - 1: "; cin >> bateriNecesare;
- cout << "Varsta recomandata: "; cin >> varstaRecomandata;
- if (listaCadou.empty())
- listaCadou.push_back(new Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
- else {
- it = listaCadou.begin();
- while (it != listaCadou.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaCadou.emplace(it, new Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
- }
- }
- }
- bool VerificareCNP(char *CNP) {
- for (it = listaPersone.begin(); it != listaPersone.end(); it++)
- if ((*it)->get_CNP() == CNP) {
- return true;
- }
- return false;
- }
- void AdaugaPersoana() {
- string nume;
- char CNP[20];
- Persoana *q;
- struct adresa adresa;
- getchar();
- cout << "Nume persoana: "; getline(cin, nume);
- cout << "CNP: "; cin >> CNP;
- try
- {
- if (strlen(CNP) < 13) throw 1;
- if (strlen(CNP) > 13) throw 2;
- for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw 7;
- if (VerificareCNP(CNP)) throw 3;
- if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw 4;
- if (CNP[3] > '1') throw 5;
- if (CNP[3] == '1' && CNP[4] > '2') throw 6;
- }
- catch (int error)
- {
- if (error == 1) cout << "\nCNP are prea putine cifre";
- if (error == 2) cout << "\nCNP are prea multe cifre";
- if (error == 3) cout << "\nCNP este deja in baza de date";
- if (error == 4) cout << "\nPrima cifra din CNP este gresita";
- if (error == 5) cout << "\nA patra cifra din CNP este gresita";
- if (error == 6) cout << "\nA cincea cifra a CNP este gresita";
- if (error == 7)cout << "\nAu fost introduse date invalide";
- return;
- }
- getchar();
- cout << "Localitatea: "; getline(cin, adresa.localitate);
- cout << "Judet: "; getline(cin, adresa.judet);
- cout << "Cod Postal: "; getline(cin, adresa.codPostal);
- cout << "Strada: "; getline(cin, adresa.strada);
- cout << "Numar casa: "; cin >> adresa.nr;
- q = new Persoana(nume, CNP, adresa);
- if (listaPersone.empty())
- listaPersone.push_back(q);
- else {
- it = listaPersone.begin();
- while (it != listaPersone.end() && (*it)->get_nume() < nume)
- advance(it, 1);
- listaPersone.emplace(it, q);
- }
- int nrCadouri;
- cout << "Numar de cadouri: "; cin >> nrCadouri;
- for (int i = 0; i < nrCadouri; i++) {
- system("cls");
- cout << "Cadoul " << i + 1 << endl;
- q->CitireCadou();
- }
- }
- int main() {
- int opt;
- do {
- cout << "\n\n";
- cout << "1.Adaugare Persoana si lista de cadouri\n";
- cout << "2.Afisare persoane si lista caduri sub forma tabelara\n";
- cout << "0.Iesire \n";
- cout << "Dati optiunea dvs: ";
- cin >> opt;
- system("cls");
- switch (opt) {
- case 1:
- AdaugaPersoana();
- break;
- case 2:
- for (it = listaPersone.begin(); it != listaPersone.end(); it++) (*it)->Afisare();
- break;
- case 0:
- return 0;
- }
- } while (opt != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment