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;
- class Exception: public exception {
- private:
- int error;
- string mesajEroare;
- public:
- Exception(int error) {
- this->error = error;
- mesajEroare = "";
- }
- Exception(string mesajEroare) {
- this->error = 0;
- this->mesajEroare = mesajEroare;
- }
- virtual void what() {
- switch (error)
- {
- case 0: break;
- case 1: cout << " CNP are prea putine cifre(CNP tebuie sa contina 13 cifre)\n"; break;
- case 2: cout << " CNP are prea multe cifre(CNP tebuie sa contina 13 cifre)\n"; break;
- case 3: cout << " CNP este deja in baza de date\n"; break;
- case 4: cout << " Prima cifra din CNP este gresita\n"; break;
- case 5: cout << " A patra cifra din CNP este gresita\n"; break;
- case 6: cout << " A cincea cifra a CNP este gresita\n"; break;
- case 7: cout << " Luna nasteri gresita\n"; break;
- case 8: cout << " A 6 cifra din CNP este gresita\n"; break;
- case 9: cout << " A 7 cfra din CNP este gresita\n"; break;
- case 10: cout << " Ziua nasteri din CNP este gresita\n"; break;
- default: cout << " Au fost introduse date invalide\n";
- break;
- }
- if (mesajEroare == "") return;
- if (mesajEroare == "nume invalid") { cout << "Numele trebuie sa contine doar litere ale alfabetului englez,\n cel putin o litera si singurul carcater permies este '-'\n"; return; }
- if (mesajEroare == "denumire invalida") { cout << "Denumirea aleasa contine date invalide \n"; return; }
- if (mesajEroare == "CNP_not_found") { cout << "CNP introdus nu se gaseste in baza de date\n"; return; }
- if (mesajEroare == "lista_goala") { cout << "Lista este goala\n"; return; }
- }
- };
- 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() override {
- 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() override {
- 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() final override {
- 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 << " ";
- if (dataNastere.zi < 10)cout << '0';
- cout << dataNastere.zi << "/";
- if(dataNastere.luna < 10) cout << '0';
- cout << dataNastere.luna << "/" << dataNastere.an << endl;
- cout << adresa.localitate << " " << adresa.judet <<" "<< adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
- cout << "\nLista de cadouri:\n";
- cout << "=============================" << endl;
- try
- {
- if (listaCadou.empty()) throw Exception("lista_goala");
- }
- catch (Exception &e)
- {
- e.what();
- cout << "\n=============================" << endl;
- return;
- }
- for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) (*it)->Afisare();
- cout << "\n=============================" << endl;
- }
- void AfisareCheltuitTotal() {
- cout << "Buget cheltuit = " << cheltuitCadouri << " ron" << 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;
- getchar();
- cout << "Nume: "; getline(cin, nume);
- cout << "Pret: "; cin >> pret;
- getchar();
- cout << "Tip: "; getline(cin, tip);
- cheltuitCadouri += pret;
- if (t == 'D') {
- string ingrediente;
- double calori, gramaj;
- cout << "Ingerdiente: "; getline(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: "; getline(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));
- }
- }
- }
- void VerificareCNP(char *CNP){
- if (strlen(CNP) < 13) throw Exception(1);
- if (strlen(CNP) > 13) throw Exception(2);
- if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw Exception(4);
- if (CNP[3] > '1') throw Exception(5);
- if (CNP[3] == '1' && CNP[4] > '2') throw Exception(6);
- if (CNP[3] == '0' && CNP[4] == '0') throw Exception(7);
- if (CNP[5] > '3') throw Exception(8);
- if (CNP[5] == '3' && CNP[6] > '1') throw Exception(9);
- if (CNP[5] == '0' && CNP[6] == '0')throw Exception(10);
- for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw Exception(99999);
- }
- void VerificareNume(string &nume) {
- while (nume[0] == ' ' || nume[0] == '-')
- {
- while (nume[0] == ' ') nume.erase(0, 1);
- while (nume[0] == '-') nume.erase(0, 1);
- }
- if (nume == "")throw Exception("nume invalid");
- for (int i = 0, n = nume.size(); i < n; i++)
- if (nume[i] == ' ' || nume[i] == '-') continue;
- else if (nume[i] >= 'a' && nume[i] <= 'z') continue;
- else if (nume[i] >= 'A' && nume[i] <= 'Z') continue;
- else throw Exception("nume invalid");
- }
- void AdaugaPersoana() {
- string nume;
- char CNP[20];
- Persoana *q;
- struct adresa adresa;
- getchar();
- CitireNume:
- try {
- cout << "Nume persoana( valoarea 0 pentru a reveni la meniu): "; getline(cin, nume);
- VerificareNume(nume);
- }
- catch (Exception &e) {
- if (nume == "0") return;
- e.what();
- goto CitireNume;
- }
- CitireCNP:
- try
- {
- cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP;
- VerificareCNP(CNP);
- for (it = listaPersone.begin(); it != listaPersone.end(); it++)
- if ((*it)->get_CNP() == CNP)
- throw Exception(3);
- }
- catch (Exception &e)
- {
- if (strcmp(CNP, "0") == 0) return;
- e.what();
- goto CitireCNP;
- }
- 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();
- }
- }
- void CitireCadou(char *CNP) {
- try { if (listaPersone.empty()) throw Exception("lista_goala"); }
- catch (Exception &e) { e.what(); }
- char CNP2[20];
- if (CNP == NULL) {
- CitireCNP:
- try
- {
- cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP2;
- VerificareCNP(CNP2);
- for (it = listaPersone.begin(); it != listaPersone.end(); it++)
- if (strcmp((*it)->get_CNP(), CNP2) == 0) break;
- if (it == listaPersone.end()) throw Exception("CNP_not_found");
- }
- catch (Exception &e)
- {
- if (strcmp(CNP2, "0") == 0) return;
- e.what();
- goto CitireCNP;
- }
- CNP = CNP2;
- }
- int nrCadouri;
- (*it)->Afisare();
- cout << "Numar de cadouri: "; cin >> nrCadouri;
- for (int i = 0; i < nrCadouri; i++) {
- system("cls");
- cout << "Cadoul " << i + 1 << endl;
- (*it)->CitireCadou();
- }
- }
- void CautarePersoana() {
- char CNP[20];
- CitireCNP:
- try
- {
- cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP;
- VerificareCNP(CNP);
- for (it = listaPersone.begin(); it != listaPersone.end(); it++)
- if (strcmp((*it)->get_CNP(), CNP) == 0) break;
- if (it == listaPersone.end()) throw Exception("CNP_not_found");
- }
- catch (Exception &e)
- {
- if (strcmp(CNP, "0") == 0) return;
- e.what();
- goto CitireCNP;
- }
- (*it)->Afisare();
- (*it)->AfisareCheltuitTotal();
- }
- int main() {
- int opt;
- do {
- cout << "\n\n";
- cout << "1. Adaugare persoana si lista de cadouri\n";
- cout << "2. Afisare persoane si lista cadouri sub forma tabelara\n";
- cout << "3. Adaugare cadou pentru o persoana oarecare\n";
- cout << "4. Cautarea unei persoane, afisarea listei de cadouri si pretul total\n";
- cout << "0. Iesire \n";
- cout << "Dati optiunea dvs: ";
- cin >> opt;
- system("cls");
- switch (opt) {
- case 1:
- AdaugaPersoana();
- break;
- case 2:
- try { if (listaPersone.empty()) throw Exception("lista_goala"); }
- catch (Exception &e) { e.what(); }
- for (it = listaPersone.begin(); it != listaPersone.end(); it++) (*it)->Afisare();
- break;
- case 3:
- CitireCadou(NULL); break;
- case 4:
- CautarePersoana(); break;
- case 0:
- return 0;
- }
- } while (1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment