Advertisement
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>
- #include<exception>
- #include<fstream>
- using namespace std;
- #define _CRT_SECURE_NO_WARNING
- 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;
- }
- const char* what() const throw() {
- switch (error)
- {
- case 0: break;
- case 1: return " CNP are prea putine cifre(CNP tebuie sa contina 13 cifre)\n";
- case 2: return " CNP are prea multe cifre(CNP tebuie sa contina 13 cifre)\n";
- case 3: return " CNP este deja in baza de date\n";
- case 4: return " Prima cifra din CNP este gresita\n";
- case 5: return " A patra cifra din CNP este gresita\n";
- case 6: return " A cincea cifra a CNP este gresita\n";
- case 7: return " Luna nasteri gresita\n";
- case 8: return " A 6 cifra din CNP este gresita\n";
- case 9: return " A 7 cfra din CNP este gresita\n";
- case 10: return " Ziua nasteri din CNP este gresita\n";
- default: return " Au fost introduse date invalide\n";
- break;
- }
- if (mesajEroare == "") return "Nu au fost intruse date";
- if (mesajEroare == "nume invalid") return "Numele trebuie sa contine doar litere ale alfabetului englez,\n cel putin o litera si singurul carcater permies este '-'\n";
- if (mesajEroare == "denumire invalida") return "Denumirea aleasa contine date invalide \n";
- if (mesajEroare == "CNP_not_found") return "CNP introdus nu se gaseste in baza de date\n";
- if (mesajEroare == "lista_goala") return "Lista este goala\n";
- if (mesajEroare == "nume_negasit") return "Numele cautat nu se afla in baza de date\n";
- return "Eroare nespcificata";
- }
- };
- 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);
- Cadou() {}
- virtual void Afisare() = 0;
- string get_nume() {
- return nume;
- }
- string get_tip() {
- return tip;
- }
- };
- 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, addedByFile;
- 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;
- this->addedByFile = false;
- }
- void CitireCadou();
- char *get_CNP() {
- return CNP;
- }
- string get_nume() {
- return nume;
- }
- void Afisare(bool afisareCadouri) {
- cout << "------------------------------------------------------------------------------------------\n";
- cout << nume << " " << CNP;
- 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;
- if (afisareCadouri == false) return;
- 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;
- }
- bool CautCadou(string tip) {
- for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++)
- if ((*it)->get_tip() == tip) return true;
- return false;
- }
- void DublareCadou();
- void StergeCadou();
- void ClearList() { listaCadou.clear(); }
- void AddedByFile(bool added) {
- this->addedByFile = added;
- }
- void CitireCadouriFiser(ifstream &f);
- };
- 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 Persoana::DublareCadou() {
- string nume;
- list <Cadou *>::iterator it;
- getchar();
- CitireNumeCadou:
- cout << "Introduceti nume cadou: "; getline(cin, nume);
- for (it = listaCadou.begin(); it != listaCadou.end(); it++)
- if ((*it)->get_nume() == nume)break;
- try {
- if (it == listaCadou.end()) throw Exception("nume_negasit");
- }
- catch (Exception &e) {
- if (nume == "0") return;
- cerr << e.what();
- goto CitireNumeCadou;
- }
- listaCadou.emplace(it, *it);
- }
- void Persoana::StergeCadou() {
- list <Cadou *>::iterator it1;
- string nume;
- CitireNumeCadou:
- cout << "Introduceti nume cadou: "; getline(cin, nume);
- for (it1 = listaCadou.begin(); it1 != listaCadou.end(); it1++)
- if ((*it1)->get_nume() == nume)break;
- try {
- if (it1 == listaCadou.end()) throw Exception("nume_negasit");
- }
- catch (Exception &e) {
- if (nume == "0") return;
- cerr << e.what();
- goto CitireNumeCadou;
- }
- listaCadou.erase(it1, it1);
- }
- void Persoana::CitireCadouriFiser(ifstream &f) {
- char temp;
- string nume, tip;
- double pret;
- list <Cadou *>::iterator it;
- getline(f, nume, '\n');
- while (!f.eof()) {
- f >> temp;
- if (temp == '\n' || temp == ' ')return;
- if (f.eof()) return;
- getline(f, nume, '\n');
- getline(f, nume, '\n');
- getline(f, tip, '\n');
- f >> pret;
- if (temp == 'D') {
- string ingrediente;
- double calori, gramaj;
- getline(f, ingrediente, '\n');
- getline(f, ingrediente, '\n');
- f >> calori >> 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));
- }
- }
- else if (temp == 'P') {
- bool pentruBarbati;
- unsigned short nrProd;
- f >> pentruBarbati;
- f >> 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));
- }
- }
- else if (temp == 'J') {
- string marca;
- bool bateriNecesare;
- unsigned short varstaRecomandata;
- getline(f, marca, '\n');
- getline(f, marca, '\n');
- f >> bateriNecesare;
- f >> 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;
- cerr << 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 (strcmp((*it)->get_CNP(), CNP) == 0)
- throw Exception(3);
- }
- catch (exception &e)
- {
- if (strcmp(CNP, "0") == 0) return;
- cerr << 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) { cerr << 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 (strcmp((*it)->get_CNP(), CNP2)) throw Exception("CNP_not_found");
- }
- catch (exception &e)
- {
- if (strcmp(CNP2, "0") == 0) return;
- cerr << e.what();
- goto CitireCNP;
- }
- CNP = CNP2;
- }
- int nrCadouri;
- (*it)->Afisare(true);
- 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;
- cerr << e.what();
- goto CitireCNP;
- }
- (*it)->Afisare(true);
- (*it)->AfisareCheltuitTotal();
- }
- void CautareTipCadou() {
- string tip;
- bool gasitPersoana = 0;
- getchar();
- cout << "Introduceti tipul: "; getline(cin, tip);
- for (it = listaPersone.begin(); it != listaPersone.end(); it++) {
- if ((*it)->CautCadou(tip)) {
- (*it)->Afisare(false);
- cout << "--------------------------------------------------------------------------";
- gasitPersoana = 1;
- }
- }
- if (gasitPersoana == false)cout << "Nu s-a gasit nici-o persoana care sa aiba un cadou de tipul " << tip << endl;
- }
- void DublareCadou() throw() {
- 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;
- cerr << e.what();
- goto CitireCNP;
- }
- (*it)->DublareCadou();
- }
- void StergereDinlista() {
- bool option;
- cout << "Persoana = 0, Cadou = 1"; cin >> option;
- 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;
- cerr << e.what();
- goto CitireCNP;
- }
- if (option) (*it)->StergeCadou();
- else {
- (*it)->ClearList();
- listaPersone.erase(it, it);
- }
- }
- void CitirePersoaneFiser() {
- char CNP[20];
- string nume;
- struct adresa adresa;
- int nrEroriCitrePersoana = 0;
- Persoana *q = NULL;
- ifstream f("in.txt");
- bool ok = 0, ok2;
- if (f.eof()) return; ;
- while (!f.eof()) {
- f >> CNP;
- try {
- ok2 = 1;
- VerificareCNP(CNP);
- for (it = listaPersone.begin(); it != listaPersone.end(); it++)
- if (strcmp((*it)->get_CNP(), CNP) == 0) {
- ok = 0;
- ok2 = 0;
- break;
- }// daca introducem un cnp deja aflat in baza de date inseamna ca dorims a mai adaugam caoduri
- }
- catch (...) {
- if (ok) {
- nrEroriCitrePersoana++;
- ok = 0;
- }
- continue;
- }
- if (ok2 == 0) continue;
- getline(f, nume, '\n');
- getline(f, nume, '\n');
- try {
- VerificareNume(nume);
- }
- catch (...) {
- if (ok) {
- nrEroriCitrePersoana++;
- ok = 0;
- }
- continue;
- }
- getline(f, adresa.localitate, '\n');
- getline(f, adresa.judet, '\n');
- getline(f, adresa.strada, '\n');
- getline(f, adresa.codPostal, '\n');
- f >> adresa.nr;
- q = new Persoana(nume, CNP, adresa);
- q->AddedByFile(true);
- 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);
- }
- q->CitireCadouriFiser(f);
- ok = true;
- }
- f.close();
- }
- 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 << "5. Cautarea dupa un tip de cadou si afisarea persoanel0r\n";
- cout << "6. Dublare cadou pentru o persoana\n";
- cout << "7. Citire date din fiser in.txt\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) { cerr << e.what(); }
- for (it = listaPersone.begin(); it != listaPersone.end(); it++) (*it)->Afisare(true);
- break;
- case 3:
- CitireCadou(NULL); break;
- case 4:
- CautarePersoana(); break;
- case 5:
- CautareTipCadou(); break;
- case 6:
- DublareCadou(); break;
- case 7: CitirePersoaneFiser(); break;
- case 0:
- return 0;
- }
- } while (1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement