Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Drula Alex Marian problema 28
- #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 Modul {
- private:
- string nume, stare;
- int idModul, valoare;
- int tipDerivat;
- public:
- Modul(int tipDerivat, string nume, int valoare, string stare) {
- static int idCurent = 0;
- idCurent++;
- idModul = idCurent;
- this->tipDerivat = tipDerivat;
- this->nume = nume;
- this->valoare = valoare;
- this->stare = stare;
- }
- virtual void Afisare() {
- cout << tipDerivat << " | " << idModul << " " << nume << " " << valoare << " " << stare << " ";
- }
- int getTip() { return tipDerivat; }
- string getNume() { return nume; }
- int getValoare() {
- return valoare;
- }
- string getStare() {
- return stare;
- }
- void setVal(int val) { valoare = val; }
- virtual void Modificat() = 0;
- };
- class Releu :public Modul {
- private:
- string tip;
- int histereza;
- static int nrMod;
- public:
- Releu(string nume, int valoare, string stare, string tip, int histereza) :Modul(0, nume, valoare, stare) {
- this->tip = tip;
- this->histereza = histereza;
- }
- void Afisare() {
- Modul::Afisare();
- cout << tip << " " << histereza << endl;
- }
- void Modificat() {
- nrMod++;
- }
- static void AfMod() { cout << nrMod; }
- };
- int Releu:: nrMod;
- istream & operator>>(istream &in, Releu *&r) {
- string nume, stare;
- int valoare;
- string tip;
- int histereza;
- cout << "Nume: "; in >> nume;
- cout << "Valoare : "; in >> valoare;
- cout << "Stare : "; in >> stare;
- cout << "Tip releu[static/ contact] : "; in >> tip;
- cout << "Histereza : "; in >> histereza;
- r = new Releu(nume, valoare, stare, tip, histereza);
- return in;
- }
- class Dimmer :public Modul {
- private:
- int gama, putere, valPrag;
- static int nrMod;
- public:
- Dimmer(string nume, int valoare, string stare, int gama, int putere, int valPrag) :Modul(1, nume, valoare, stare) {
- this->gama = gama;
- this->putere = putere;
- this->valPrag = valPrag;
- }
- void Afisare() {
- Modul::Afisare();
- cout << gama << " " << putere << " " << valPrag << endl;
- }
- void Modificat() {
- nrMod++;
- }
- static void AfMod() { cout << nrMod; }
- };
- int Dimmer:: nrMod;
- istream & operator>>(istream &in, Dimmer *&d) {
- string nume, stare;
- int valoare;
- int gama, putere, valPrag;
- bool ok;
- cout << "Nume: "; in >> nume;
- cout << "Stare : "; in >> stare;
- cout << "Valoare: "; in >> valoare;
- do {
- cout << "Gama : "; in >> gama;
- try {
- if (gama < 0 || gama > 100)
- throw MyException("out_limit");
- ok = true;
- }
- catch (MyException &e) {
- cerr << e.what();
- ok = false;
- }
- } while (ok == false);
- cout << "Putere: "; in >> putere;
- cout << " Valare de Prag: "; in >> valPrag;
- d = new Dimmer(nume, valoare, stare, gama, putere, valPrag);
- return in;
- }
- class Persoana {
- private:
- string nume;
- string adresa;
- list<Modul* > ListaModul;
- public:
- Persoana(string nume, string adresa) {
- this->nume = nume;
- this->adresa = adresa;
- }
- void Afisare() {
- cout << "Nume: " << nume << endl;
- cout << "Adresa: " << adresa << endl;
- for (auto it = ListaModul.begin(); it != ListaModul.end(); it++)(*it)->Afisare();
- }
- bool Cautare(string nume) {
- for (auto it = ListaModul.begin(); it != ListaModul.end(); it++)
- if ((*it)->getNume() == nume) {
- (*it)->Afisare();
- return true;
- }
- return false;
- }
- void AfisareDefecte() {
- for (auto it = ListaModul.begin(); it != ListaModul.end(); it++)
- if ((*it)->getStare() == "defect") (*it)->Afisare();
- }
- void adaugareModul() {
- int tip;
- Releu *r = NULL;
- Dimmer *d = NULL;
- Modul *m = NULL;
- cout << "Tip de modul [releu- 0 , dimmer - 1]: "; cin >> tip;
- if (tip) {
- cin >> r;
- m = r;
- }
- else
- {
- cin >> d;
- m = d;
- }
- list<Modul *>::iterator it = ListaModul.begin();
- while (it != ListaModul.end() && (*it)->getValoare() < m->getValoare())
- it++;
- ListaModul.emplace(it, m);
- }
- void Sterge(int tip, string nume) {
- if (ListaModul.empty()) return;
- for (auto it = ListaModul.begin(); it != ListaModul.end(); it++) {
- if ((*it)->getTip() == tip && (*it)->getNume() == nume) {
- ListaModul.erase(it);
- it = ListaModul.begin();
- }
- if (ListaModul.empty()) return;
- }
- }
- string getNume() { return nume; }
- void ModVal(string nume, int val) {
- for (auto it = ListaModul.begin(); it != ListaModul.end(); it++) {
- if ((*it)->getNume() == nume) {
- (*it)->setVal(val);
- (*it)->Modificat();
- }
- }
- }
- friend fstream &operator>>(fstream &in, Persoana *&p);
- };
- istream &operator>>(istream &in, Persoana *&p) {
- string nume, adresa;
- cout << "Nume: "; in >> nume;
- cout << "Adresa: "; in >> adresa;
- p = new Persoana(nume, adresa);
- return in;
- }
- ostream &operator<<(ostream &out, Persoana *&p) {
- out << linie;
- p->Afisare();
- return out;
- }
- fstream &operator>>(fstream &in, Persoana *&p) {
- string nume, stare;
- int valoare;
- string tip;
- int histereza;
- int gama, putere, valPrag;
- string numePers;
- string adresa;
- in >> numePers;
- in >> adresa;
- p = new Persoana(numePers, adresa);
- int tipDerivat;
- Modul *m = NULL;
- if (in.eof()) return in;
- while (getline(in, nume))
- {
- getline(in, nume);
- if (in.eof() || nume == "\n" || nume == "") return in;
- in >> valoare;
- in >> stare;
- in >> tipDerivat;
- if (tipDerivat) {
- in >> gama >> putere >> valPrag;
- m = new Dimmer(nume, valoare, stare, gama, putere, valPrag);
- }
- else
- {
- in >> tip >> histereza;
- m = new Releu(nume, valoare, stare, tip, histereza);
- }
- list<Modul *>::iterator it = p->ListaModul.begin();
- while (it != p->ListaModul.end() && (*it)->getValoare() < m->getValoare())
- it++;
- p->ListaModul.emplace(it, m);
- }
- return in;
- }
- int main() {
- int opt, nr, val;
- list<Persoana *> listaPersoana;
- list<Persoana *>::iterator it;
- Persoana *p = 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. Afisare elemente defecte\n";
- cout << "5. Modificare Valore\n";
- cout << "6. Afisare Modificar\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 >> p;
- }
- else {
- cout << "Nume: "; cin >> nume;
- p = NULL;
- for (it = listaPersoana.begin(); it != listaPersoana.end(); it++) {
- if ((*it)->getNume() == nume) {
- p = *it;
- break;
- }
- }
- if (p == NULL) { cout << "nu a fost gasit!\n"; break; }
- }
- cout << "Numar de module: "; cin >> nr;
- for (int i = 1; i <= nr; i++) {
- cout << "Modulul " << i << endl;
- p->adaugareModul();
- system("cls");
- }
- if (gasit == 1) {
- it = listaPersoana.begin();
- while (it != listaPersoana.end() && (*it)->getNume() < p->getNume()) it++;
- listaPersoana.emplace(it, p);
- }
- gasit = 0;
- break;
- case 2:
- for (it = listaPersoana.begin(); it != listaPersoana.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 >> p;
- if (p->getNume() == "") {
- delete p;
- break;
- }
- it = listaPersoana.begin();
- while (it != listaPersoana.end() && (*it)->getNume() < p->getNume()) it++;
- listaPersoana.emplace(it, p);
- p->Afisare();
- cout << endl;
- }
- f.close();
- break;
- case 4:
- for (it = listaPersoana.begin(); it != listaPersoana.end(); it++) (*it)->AfisareDefecte();
- break;
- case 5:
- cout << "Nume: "; cin >> nume;
- cout << "Valoare Noua: "; cin >> val;
- for (it = listaPersoana.begin(); it != listaPersoana.end(); it++) (*it)->ModVal(nume, val);
- break;
- case 6:
- cout << "\nReleu: ";
- Releu::AfMod();
- cout << "\nDimmer: ";
- Dimmer::AfMod();
- cout << endl;
- break;
- }
- } while (1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement