ivana_andreevska

Kolokviumska primer Maraton

May 12th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class Ucesnik {
  7. private:
  8.     char* ime;
  9.     bool pol;
  10.     int vozrast;
  11. public:
  12.     Ucesnik(const char* ime = "", bool pol = false, int vozrast = 0)
  13.     {
  14.         this->ime = new char[strlen(ime) + 1];
  15.         strcpy(this->ime, ime);
  16.         this->pol = pol;
  17.         this->vozrast = vozrast;
  18.     }
  19.     Ucesnik(Ucesnik& ob)
  20.     {
  21.         delete[]this->ime;
  22.         this->ime = new char[strlen(ob.ime) + 1];
  23.         strcpy(this->ime, ob.ime);
  24.         this->pol = ob.pol;
  25.         this->vozrast = ob.vozrast;
  26.     }
  27.     ~Ucesnik()
  28.     {
  29.     delete[]this->ime;
  30.     }
  31.     Ucesnik operator=(Ucesnik& ob)
  32.     {
  33.         if (this != &ob)
  34.         {
  35.             delete[]this->ime;
  36.             this->ime = new char[strlen(ob.ime) + 1];
  37.             strcpy(this->ime, ob.ime);
  38.             this->pol =ob.pol;
  39.             this->vozrast = ob.vozrast;
  40.         }
  41.         return *this;
  42.     }
  43.     int getVozrast()
  44.     {
  45.         return this->vozrast;
  46.     }
  47.  
  48.     bool operator>(Ucesnik& ob)
  49.     {
  50.         return this->vozrast > ob.vozrast;
  51.     }
  52.  
  53.     friend ostream& operator<<(ostream& out, Ucesnik& ob)
  54.     {
  55.         out << ob.ime << endl;
  56.         if (ob.pol)
  57.             out << "Maski" << endl;
  58.         else
  59.             out << "Zenski" << endl;
  60.         out << ob.vozrast << endl;
  61.         return out;
  62.     }
  63. };
  64.  
  65.  
  66. class Maraton {
  67. private:
  68.     char lokacija[100];
  69.     Ucesnik** niza;
  70.     int br_ucesnici;
  71. public:
  72.     Maraton(char* lokacija = "")
  73.     {
  74.         strcpy(this->lokacija, lokacija);
  75.         this->niza = NULL;
  76.         this->br_ucesnici = 0;
  77.     }
  78.     ~Maraton()
  79.     {
  80.         delete[]this->niza;
  81.     }
  82.     Maraton operator+=(Ucesnik&ob)
  83.     {
  84.         Ucesnik*temp=new Ucesnik[this->br_ucesnici+1];
  85.         copy(this->niza, this->niza + this->br_ucesnici, temp);
  86.         temp[this->br_ucesnici++]=ob;
  87.         delete[]this->niza;
  88.         //this->niza = temp;
  89.         return *this;
  90.     }
  91.     double proscena_vozrast()
  92.     {
  93.         int zbir = 0;
  94.         for (int i = 0; i < this->br_ucesnici; ++i)
  95.         {
  96.             zbir += this->niza[i]->getVozrast();
  97.             return zbir / ((double)(this->br_ucesnici));
  98.         }
  99.     }
  100.     void pecatiPomladi(Ucesnik& u)
  101.     {
  102.         for (int i = 0; i < this->br_ucesnici; ++i)
  103.             if (this->niza[i])
  104.                 cout << this->niza[i];
  105.     }
  106. };
  107.  
  108. int main()
  109. {
  110.     char ime[100];
  111.     bool maski;
  112.     int vozrast;
  113.     int n;
  114.  
  115.     cin>>n;
  116.  
  117.     char lokacija[100];
  118.     Maraton m(lokacija);
  119.  
  120.     Ucesnik **u=new Ucesnik*[n];
  121.     for(int i=0;i<n;i++)
  122.     {
  123.         cin>>ime>>maski>>vozrast;
  124.         u[i]=new Ucesnik(ime,maski,vozrast);
  125.  
  126.         m+=*u[i];
  127.  
  128.     }
  129.     m.pecatiPomladi(*u[n-1]);
  130.     cout<<m.proscena_vozrast()<<endl;
  131.     for(int i=0;i<n;i++)
  132.     {
  133.         delete u[i];
  134.     }
  135.     delete []u;
  136.     return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment