Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- class Localitate {
- private:
- string nume;
- int populatie;
- int suprafata;
- public:
- Localitate(string nume, int populatie = 0, int suprafata = 0);
- string getNume() { return nume; }
- int getPopulatie() { return populatie; }
- int getSuprafata() { return suprafata; }
- void setNume(string nume) { this->nume = nume; }
- void setPopulatie(int populatie) { this->populatie = populatie; }
- void setSuprafata(int suprafata) { this->suprafata = suprafata; }
- };
- Localitate::Localitate(string nume, int populatie, int suprafata) : nume(nume),
- populatie(populatie), suprafata(suprafata) {}
- class Judet {
- private:
- string nume;
- string prefix;
- int populatie;
- int maxSize;
- Localitate * localitati;
- int size;
- string makePrefix();
- void expand();
- Localitate* findLocalitate(string nume) const;
- public:
- Judet(string nume, int populatie = 0);
- ~Judet();
- string getNume() { return nume; }
- string getPrefix() { return prefix; }
- int getPopulatie() { return populatie; }
- void setNume(string nume);
- void setPopulatie(int populatie) { this->populatie = populatie; }
- void adaugaLocalitate(Localitate & localitate);
- void stergeLocalitate(string nume);
- void combina(string nume, string localitate1, string localitate2);
- void afiseaza();
- Localitate* operator[](string nume) const;
- Localitate* operator[](int index) const;
- };
- Judet::Judet(string nume, int populatie) : nume(nume), populatie(populatie),
- maxSize(1), size(0) {
- localitati = (Localitate *) malloc(maxSize * sizeof(Localitate));
- if (nume.size() > 2) {
- this->makePrefix();
- } else {
- this->prefix = "";
- }
- }
- Judet::~Judet() {
- free(localitati);
- }
- void Judet::setNume(string nume) {
- this->nume = nume;
- if (nume.size() > 2) {
- this->makePrefix();
- } else {
- this->prefix = "";
- }
- }
- string Judet::makePrefix() {
- stringstream s;
- s << toupper(nume[0]) << toupper(nume[1]);
- return s.str();
- }
- void Judet::expand() {
- maxSize += 10;
- Localitate * tmp = (Localitate *) realloc(localitati, maxSize * sizeof(Localitate));
- if(tmp) {
- localitati = tmp;
- } else {
- perror("Memorie insuficienta");
- }
- }
- void Judet::adaugaLocalitate(Localitate & localitate) {
- if (size != maxSize) {
- this->localitati[size++] = localitate;
- this->populatie += localitate.getPopulatie();
- } else {
- this->expand();
- adaugaLocalitate(localitate);
- }
- }
- Localitate* Judet::findLocalitate(string nume) const {
- for (int i = 0; i < this->size; i++) {
- if (this->localitati[i].getNume() == nume ) {
- return &this->localitati[i];
- }
- }
- return nullptr;
- }
- Localitate * Judet::operator[](string nume) const {
- return this->findLocalitate(nume);
- }
- Localitate * Judet::operator[](int index) const {
- if (index >= 0 && index < size) {
- return &this->localitati[index];
- }
- return nullptr;
- }
- void Judet::stergeLocalitate(string nume) {
- int index = 0;
- Localitate * localitati = (Localitate *) malloc(maxSize * sizeof(Localitate));
- for (int i = 0; i < size; i++) {
- if (this->localitati[i].getNume() == nume) {
- this->populatie -= this->localitati[i].getPopulatie();
- continue;
- }
- localitati[index++] = this->localitati[i];
- }
- free(this->localitati);
- this->localitati = localitati;
- --size;
- }
- void Judet::afiseaza() {
- cout << "Localitati: " << endl;
- for (int i = 0; i < size; i++) {
- cout << "-" <<this->localitati[i].getNume() << endl;
- }
- cout << "Populatie totala: " << this->getPopulatie() << endl;
- }
- void Judet::combina(string nume, string localitate1, string localitate2) {
- Localitate * locPtr1 = this->findLocalitate(localitate1);
- Localitate * locPtr2 = this->findLocalitate(localitate2);
- if (locPtr1 && locPtr2) {
- int populatie = locPtr1->getPopulatie() + locPtr2->getPopulatie();
- int suprafata = locPtr1->getSuprafata() + locPtr2->getSuprafata();
- Localitate * new_loc = new Localitate(nume, populatie, suprafata);
- this->adaugaLocalitate(*new_loc);
- this->stergeLocalitate(localitate1);
- this->stergeLocalitate(localitate2);
- }
- }
- int main() {
- Judet arad("Arad", 0);
- Localitate moneasa("Moneasa", 1000, 250), sebis("Sebis", 5000, 700);
- arad.adaugaLocalitate(moneasa);
- arad.adaugaLocalitate(sebis);
- arad.afiseaza();
- arad.stergeLocalitate("Moneasa");
- arad.adaugaLocalitate(*(new Localitate("Carand", 500, 200)));
- arad.adaugaLocalitate(*(new Localitate("Minead", 1500, 600)));
- arad.adaugaLocalitate(*(new Localitate("Ineu", 2500, 400)));
- arad.afiseaza();
- arad.combina("Camid", "Carand", "Minead");
- cout << arad["Camid"]->getPopulatie() << endl;
- arad.afiseaza();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement