Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <iterator>
- #include <conio.h>
- #include <string>
- #include <exception>
- #include <fstream>
- using namespace std;
- class MyException : public exception {
- private:
- string mesajEroare;
- public:
- MyException(string mesajEroare) {
- this->mesajEroare = mesajEroare;
- }
- const char* what() const throw() {
- if (mesajEroare == "out_limit") return "Valoarea este inafara intervalului";
- if (mesajEroare == "lista_goala") return "lista este goala";
- return "Eroare neprevazuta";
- }
- };
- ostream & linie(ostream &out) {
- out << "------------------------\n";
- return out;
- }
- class C1 {
- private:
- string nume;
- int tipDerivat;
- public :
- C1(int tipDerivat, string nume) {
- this->tipDerivat = tipDerivat;
- this->nume = nume;
- }
- virtual void Afisare() {
- cout << tipDerivat << " | " << nume << " "<< << " " << << " "<< << " ";}
- int getTip() { return tipDerivat; }
- string getNume() { return nume; }
- };
- class D1 :public C1 {
- private:
- public:
- D1(string nume) :C1(0, nume) {
- }
- void Afisare() {
- C1::Afisare();
- cout << << " "<< << " " << << " "<< << " "<<endl;}
- };
- istream & operator>>(istream &in, D1 *&d1) {
- string nume;
- bool ok;
- cout << "Nume: "; in >> nume ;
- cout << " : "; in >> ;
- cout << " : "; in >> ;
- do {
- try {
- throw MyException("out_limit");
- ok = true;
- }
- catch (MyException &e) {
- cerr << e.what() << endl;
- ok = false;
- }
- } while (ok == false);
- d1 = new D1(nume);
- return in;
- }
- class D2 :public C1 {
- private:
- public:
- D2(string nume) :C1(1, nume) {
- }
- void Afisare() {
- C1::Afisare();
- cout << << " "<< << " " << << " "<< << " " << endl;
- }
- };
- istream & operator>>(istream &in, D2 *&d2) {
- string nume;
- bool ok;
- cout << "Nume: "; in >> nume;
- cout << " : "; in >> ;
- cout << " : "; in >> ;
- do {
- try {
- throw MyException("out_limit");
- ok = true;
- }
- catch (MyException &e) {
- cerr << e.what();
- ok = false;
- }
- } while (ok == false);
- d2 = new D2(nume);
- return in;
- }
- class C2 {
- private:
- string nume;
- list<C1* > l1;
- public:
- C2(string nume) {
- this->nume = nume;
- }
- void Afisare() {
- cout << "Nume: " << nume << endl;
- cout << " : " << << endl;
- for (auto it = l1.begin(); it != l1.end(); it++)(*it)->Afisare();
- }
- bool Cautare(string nume) {
- for (auto it = l1.begin(); it != l1.end(); it++)
- if ((*it)->getNume() ==nume) {
- (*it)->Afisare();
- return true;
- }
- return false;
- }
- void adaugare_cazare() {
- int tip;
- D1 *d1 = NULL;
- D2 *d2 = NULL;
- C1 *c1 = NULL;
- cout << "Tip de cazare [- 0 , - 1]: "; cin >> tip;
- if (tip) {
- cin >> d1;
- c1 = d1;
- }
- else
- {
- cin >> d2;
- c1 = d2;
- }
- list<C1* >::iterator it = l1.begin();
- while (it != l1.end() && (*it)->getNume() < c1->getNume()) it++;
- l1.emplace(it, c1);
- }
- void Sterge(int tip, string nume) {
- if (l1.empty()) return;
- for (auto it = l1.begin(); it != l1.end(); it++) {
- if ((*it)->getTip() == tip && (*it)->getNume() == nume)
- l1.erase(it);
- if (l1.empty()) return;
- if (it == l1.end()) return;
- }
- }
- string getNume() { return nume; }
- friend fstream &operator>>(fstream &in, C2 *&c2);
- };
- istream &operator>>(istream &in, C2 *&c2) {
- string nume;
- cout << "Denumire: "; in >> nume;
- c2 = new C2(nume);
- return in;
- }
- ostream &operator<<(ostream &out, C2 *&c2) {
- out << linie;
- c2->Afisare();
- return out;
- }
- fstream &operator>>(fstream &in, C2 *&c2) {
- string denumire;
- in >> ;
- in >> ;
- in >> ;
- c2 = new C2(denumire);
- string nume;
- int tipDerivat;
- C1 *c1 = NULL;
- if (in.eof()) return in;
- while (getline(in, nume))
- {
- getline(in, nume);
- if (in.eof() || nume == "\n" || nume == "") return in;
- in >> ;
- in >> ;
- in >> tipDerivat;
- if (tipDerivat) {
- c1 = new D2(nume);
- }
- else
- {
- c1 = new D1(nume);
- }
- list<C1 *>::iterator it = c2->l1.begin();
- while (it != c2->l1.end() && (*it)->getNume() < c2->getNume()) it++;
- c2->l1.emplace(it, c1);
- }
- return in;
- }
- int main(){
- int opt, nr;
- list<C2 *> l2;
- list<C2 *>::iterator it;
- C2 *c2 = NULL;
- fstream f;
- string a, nume;
- bool gasit = 0;
- do {
- cout << "0. Iesire\n";
- cout << "1. Adaugare tastatura \n";
- cout << "2. Afisare Consola \n";
- cout << "3. Citire Fiser \n";
- cout << "4. Cautare \n";
- cout << "5. Stergere\n";
- cout << "Optiunea Aleasa "; cin >> opt;
- system("cls");
- switch (opt)
- {
- case 0: exit(0);
- case 1:
- cout << "Oferta noua -1, sau adaugare de modul - 0 "; cin >> gasit;
- if (gasit) {
- cin >> c2;
- }
- else {
- cout << "Nume: "; cin >> nume;
- c2 = NULL;
- for (it = l2.begin(); it != l2.end(); it++) {
- if ((*it)->getNume() == nume) {
- c2 = *it;
- break;
- }
- }
- if (c2 == NULL) { cout << "nu a fost gasit!\n"; break; }
- }
- cout << "Numar de module: "; cin >> nr;
- for (int i = 1; i <= nr; i++) {
- cout << "Cazarea " << i << endl;
- c2->adaugare_cazare();
- system("cls");
- }
- it = l2.begin();
- while (it != l2.end() && (*it)->getNume() < c2->getNume()) it++;
- l2.emplace(it, c2);
- break;
- case 2:
- for (it = l2.begin(); it != l2.end(); it++)cout << (*it);
- cout << linie;
- break;
- case 3:
- f.open("in.txt", ios::in);
- if (f.is_open() == false) break;
- cout << "Datele citie din fiserul in.txt sunt: " << endl;
- while (!f.eof()) {
- f >> c2;
- if (c2->getNume() == "") {
- delete c2;
- break;
- }
- it = l2.begin();
- while (it != l2.end() && (*it)->getNume() < c2->getNume()) it++;
- l2.emplace(it, c2);
- c2->Afisare();
- cout << endl;
- }
- f.close();
- break;
- case 4:
- cout << "Nume: "; cin >> nume;
- for (it = l2.begin(); it != l2.end(); it++)
- if ((*it)->Cautare(nume)) {
- gasit = 1;
- break;
- }
- if (gasit == false)cout << "Nu a fost gasit in lista" << endl;
- gasit = 0;
- break;
- case 5:
- cout << "Stergere dupa [ - 0, -1]: "; cin >> gasit;
- if (gasit) {
- cin >> nume;
- for (it = l2.begin(); it != l2.end(); it++) (*it)->Sterge(1, nume);
- }
- else {
- cin >> nume;
- for (it = l2.begin(); it != l2.end(); it++) (*it)->Sterge(0, nume);
- }
- gasit = 0;
- break;
- }
- } while (1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment