Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Localitate {
  8. private:
  9.   string nume;
  10.   int populatie;
  11.   int suprafata;
  12. public:
  13.   Localitate(string nume, int populatie = 0, int suprafata = 0);
  14.   string getNume() { return nume; }
  15.   int getPopulatie() { return populatie; }
  16.   int getSuprafata() { return suprafata; }
  17.   void setNume(string nume) { this->nume = nume; }
  18.   void setPopulatie(int populatie) { this->populatie = populatie; }
  19.   void setSuprafata(int suprafata) { this->suprafata = suprafata; }
  20. };
  21.  
  22. Localitate::Localitate(string nume, int populatie, int suprafata) : nume(nume),
  23. populatie(populatie), suprafata(suprafata) {}
  24.  
  25. class Judet {
  26. private:
  27.   string nume;
  28.   string prefix;
  29.   int populatie;
  30.   int maxSize;
  31.   Localitate * localitati;
  32.   int size;
  33.   string makePrefix();
  34.   void expand();
  35.   Localitate* findLocalitate(string nume) const;
  36. public:
  37.   Judet(string nume, int populatie = 0);
  38.   ~Judet();
  39.   string getNume() { return nume; }
  40.   string getPrefix() { return prefix; }
  41.   int getPopulatie() { return populatie; }
  42.   void setNume(string nume);
  43.   void setPopulatie(int populatie) { this->populatie = populatie; }
  44.   void adaugaLocalitate(Localitate & localitate);
  45.   void stergeLocalitate(string nume);
  46.   void combina(string nume, string localitate1, string localitate2);
  47.   void afiseaza();
  48.   Localitate* operator[](string nume) const;
  49.   Localitate* operator[](int index) const;
  50. };
  51.  
  52. Judet::Judet(string nume, int populatie) : nume(nume), populatie(populatie),
  53. maxSize(1), size(0) {
  54.   localitati = (Localitate *) malloc(maxSize * sizeof(Localitate));
  55.   if (nume.size() > 2) {
  56.     this->makePrefix();
  57.   } else {
  58.     this->prefix = "";
  59.   }
  60. }
  61.  
  62. Judet::~Judet() {
  63.   free(localitati);
  64. }
  65.  
  66. void Judet::setNume(string nume) {
  67.   this->nume = nume;
  68.   if (nume.size() > 2) {
  69.     this->makePrefix();
  70.   } else {
  71.     this->prefix = "";
  72.   }
  73. }
  74.  
  75. string Judet::makePrefix() {
  76.   stringstream s;
  77.   s << toupper(nume[0]) << toupper(nume[1]);
  78.   return s.str();
  79. }
  80.  
  81. void Judet::expand() {
  82.   maxSize += 10;
  83.   Localitate * tmp = (Localitate *) realloc(localitati, maxSize * sizeof(Localitate));
  84.   if(tmp) {
  85.     localitati = tmp;
  86.   } else {
  87.     perror("Memorie insuficienta");
  88.   }
  89. }
  90.  
  91. void Judet::adaugaLocalitate(Localitate & localitate) {
  92.   if (size != maxSize) {
  93.     this->localitati[size++] = localitate;
  94.     this->populatie += localitate.getPopulatie();
  95.   } else {
  96.     this->expand();
  97.     adaugaLocalitate(localitate);
  98.   }
  99. }
  100.  
  101. Localitate* Judet::findLocalitate(string nume) const {
  102.   for (int i = 0; i < this->size; i++) {
  103.     if (this->localitati[i].getNume() == nume ) {
  104.       return &this->localitati[i];
  105.     }
  106.   }
  107.   return nullptr;
  108. }
  109.  
  110. Localitate * Judet::operator[](string nume) const {
  111.   return this->findLocalitate(nume);
  112. }
  113. Localitate * Judet::operator[](int index) const {
  114.   if (index >= 0 && index < size) {
  115.     return &this->localitati[index];
  116.   }
  117.   return nullptr;
  118. }
  119.  
  120. void Judet::stergeLocalitate(string nume) {
  121.   int index = 0;
  122.   Localitate * localitati = (Localitate *) malloc(maxSize * sizeof(Localitate));
  123.   for (int i = 0; i < size; i++) {
  124.     if (this->localitati[i].getNume() == nume) {
  125.       this->populatie -= this->localitati[i].getPopulatie();
  126.       continue;
  127.     }
  128.     localitati[index++] = this->localitati[i];
  129.   }
  130.   free(this->localitati);
  131.   this->localitati = localitati;
  132.   --size;
  133. }
  134.  
  135. void Judet::afiseaza() {
  136.   cout << "Localitati: " << endl;
  137.   for (int i = 0; i < size; i++) {
  138.     cout << "-" <<this->localitati[i].getNume() << endl;
  139.   }
  140.   cout << "Populatie totala: " << this->getPopulatie() << endl;
  141. }
  142.  
  143. void Judet::combina(string nume, string localitate1, string localitate2) {
  144.   Localitate * locPtr1 = this->findLocalitate(localitate1);
  145.   Localitate * locPtr2 = this->findLocalitate(localitate2);
  146.   if (locPtr1 && locPtr2) {
  147.     int populatie = locPtr1->getPopulatie() + locPtr2->getPopulatie();
  148.     int suprafata = locPtr1->getSuprafata() + locPtr2->getSuprafata();
  149.     Localitate * new_loc = new Localitate(nume, populatie, suprafata);
  150.     this->adaugaLocalitate(*new_loc);
  151.     this->stergeLocalitate(localitate1);
  152.     this->stergeLocalitate(localitate2);
  153.   }
  154. }
  155.  
  156. int main() {
  157.   Judet arad("Arad", 0);
  158.   Localitate moneasa("Moneasa", 1000, 250), sebis("Sebis", 5000, 700);
  159.   arad.adaugaLocalitate(moneasa);
  160.   arad.adaugaLocalitate(sebis);
  161.   arad.afiseaza();
  162.  
  163.   arad.stergeLocalitate("Moneasa");
  164.  
  165.   arad.adaugaLocalitate(*(new Localitate("Carand", 500, 200)));
  166.   arad.adaugaLocalitate(*(new Localitate("Minead", 1500, 600)));
  167.   arad.adaugaLocalitate(*(new Localitate("Ineu", 2500, 400)));
  168.   arad.afiseaza();
  169.   arad.combina("Camid", "Carand", "Minead");
  170.   cout << arad["Camid"]->getPopulatie() << endl;
  171.   arad.afiseaza();
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement