Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string>
- using namespace std;
- class Lista;
- class Cadou
- {
- int tip;
- string persoana;
- int pret;
- Cadou *urm;
- public:
- Cadou(int tip, string persoana, int pret)
- {
- this->tip = tip;
- this->persoana = persoana;
- this->pret = pret;
- }
- virtual void afisare()
- {
- cout << "Persoana: " << persoana<<endl;
- cout << "Pret: " << pret<<endl;
- }
- friend class Lista;
- };
- class Obiect : public Cadou
- {
- string model;
- string marime;
- public:
- Obiect(int tip, string persoana, int pret, string model, string marime) :Cadou(tip, persoana, pret)
- {
- this->model = model;
- this->marime = marime;
- }
- void afisare()
- {
- Cadou::afisare();
- cout << "Model: " << model << endl;
- cout << "Marime: " << marime << endl<<endl;
- }
- friend class Lista;
- };
- class Dulciuri : public Cadou
- {
- string aroma;
- string firma;
- public:
- Dulciuri(int tip, string persoana, int pret, string aroma, string firma) :Cadou(tip, persoana, pret)
- {
- this->aroma = aroma;
- this->firma = firma;
- }
- void afisare()
- {
- Cadou::afisare();
- cout << "Aroma: " << aroma << endl;
- cout << "Firma: " << firma << endl<<endl;
- }
- friend class Lista;
- };
- class Lista {
- public:
- Cadou *head;
- void adaugare(Cadou *a);
- void afisare();
- void cautare();
- void stergere();
- };
- void Lista::adaugare(Cadou *a)
- {
- Cadou *p;
- p = head;
- if (!p)
- {
- head = a;
- }
- else
- {
- if (a->persoana.compare(p->persoana) < 0)
- {
- a->urm = head;
- head = a;
- }
- else
- {
- while (p->urm && p->urm->persoana.compare(a->persoana))
- p = p->urm;
- a->urm = p->urm;
- p->urm = a;
- }
- }
- }
- void Lista::afisare()
- {
- Cadou *p;
- p = head;
- if (!p)
- cout << "Lista vida" << endl;
- else
- {
- while (p)
- {
- p->afisare();
- p = p->urm;
- _getch();
- }
- }
- }
- void Lista::cautare()
- {
- Cadou *p;
- p = head;
- string arom;
- cout << "Aroma cautata: "; cin >> arom;
- if (!p)
- cout << "Lista vida" << endl;
- else
- {
- while (p)
- {
- if (p->tip == 2)
- {
- Dulciuri *dul;
- dul = (Dulciuri*)p;
- if (dul->aroma.compare(arom) == 0)
- {
- p->afisare();
- _getch();
- }
- }
- }
- }
- cout << "Produs negasit" << endl;
- }
- void Lista::stergere()
- {
- Cadou *p,*q;
- p = q = head;
- string pers;
- cout << "Persoana cautata: "; cin >> pers;
- if (!p)
- cout << "Lista vida" << endl;
- else
- {
- while (p && p->persoana.compare(pers))
- {
- q = p;
- p = p->urm;
- }
- if (p)
- {
- if (p != q)
- {
- q->urm = p->urm;
- delete p;
- }
- else
- {
- head = p->urm;
- delete p;
- }
- }
- else cout<<"Negasit"<<endl;
- }
- }
- void introd(Lista &m, int x)
- {
- string persoana;
- int pret;
- string model;
- string marime;
- string aroma;
- string firma;
- Cadou *cad;
- cout << "Persoana: "; cin >> persoana;
- cout << "Pret: "; cin >> pret;
- if (x == 1)
- {
- Obiect *ob;
- cout << "Model: "; cin >> model;
- cout << "Marime: "; cin >> marime;
- ob = new Obiect(1, persoana, pret, model, marime);
- cad = ob;
- m.adaugare(cad);
- return;
- }
- if (x == 2)
- {
- Dulciuri *dul;
- cout << "Aroma: "; cin >> aroma;
- cout << "Firma: "; cin >> firma;
- dul = new Dulciuri(2, persoana, pret, aroma, firma);
- cad = dul;
- m.adaugare(cad);
- return;
- }
- }
- int main()
- {
- int opt;
- Lista m;
- m.head=NULL;
- do {
- cout << endl;
- cout << "1. Adaugare obiect" << endl;
- cout << "2. Adaugare dulciuri" << endl;
- cout << "3. Afisare" << endl;
- cout << "4. Cautare in functie de aroma" << endl;
- cout << "5. Stergere in functie de persoana" << endl;
- cout << "0. Exit" << endl;
- cin >> opt;
- switch (opt)
- {
- case 1:
- introd(m, 1);
- break;
- case 2:
- introd(m, 2);
- break;
- case 3:
- m.afisare();
- break;
- case 4:
- m.cautare();
- break;
- case 5:
- m.stergere();
- break;
- case 0:
- break;
- default: cout << "Varianta gresita" << endl;
- }
- } while (opt);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement