LegoDrifter

Maraton bez patiki

May 19th, 2020
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. // vashiot kod ovde
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class Ucesnik{
  7. private:
  8.     char *ime;
  9.     bool pol;
  10.     int age;
  11. public:
  12.  
  13.     Ucesnik(char *ime="",bool pol=false,int age=0)
  14.     {
  15.         this->pol=pol;
  16.         this->age=age;
  17.         this->ime = new char [strlen(ime)+1];  // Alocirame niza od charovi
  18.         strcpy(this->ime,ime); //Otkako napravivme alokacija soodvetno ja prefrlame vrednosta
  19.     }
  20.     Ucesnik(Ucesnik &u)
  21.     {
  22.         this->pol=u.pol;
  23.         this->age=u.age;
  24.         this->ime = new char [strlen(u.ime)+1];
  25.         strcpy(this->ime,u.ime);
  26.     }
  27.     Ucesnik &operator=(Ucesnik &u)
  28.     {
  29.         if(this!=&u)
  30.         {
  31.         delete [] ime;
  32.         this->pol=u.pol;
  33.         this->age=u.age;
  34.         this->ime = new char [strlen(u.ime)+1];
  35.         strcpy(this->ime,u.ime);
  36.        
  37.         }
  38.         return *this;
  39.     }
  40.     friend bool operator>(Ucesnik &u,Ucesnik &u1);
  41.  
  42.     friend ostream &operator<<(ostream &out,Ucesnik &u)
  43.     {
  44.        
  45.         if(u.pol==0)
  46.         out<<u.ime<<endl<<"zhenski"<<endl;
  47.         else
  48.             out<<u.ime<<endl<<"mashki"<<endl;
  49.         out<<u.age<<endl;
  50.         return out;
  51.  
  52.     }
  53.     int get_age()
  54.     {
  55.         return age;
  56.     }
  57.     ~Ucesnik()
  58.     {
  59.     //delete [] ime;
  60.     }
  61. };
  62. bool operator >(Ucesnik &u,Ucesnik &u1)
  63. {
  64.     if(u.get_age()>u1.get_age())
  65.         return true;
  66.     else return false;
  67. }
  68. class Maraton{
  69.     char lokacija[100];
  70.     Ucesnik *ucesnici;
  71.     int broj;
  72. public:
  73.     Maraton(char * lokacija="",Ucesnik *ucesnici=0,int broj=0)
  74.     {
  75.         strcpy(this->lokacija,lokacija);
  76.         this->broj=broj;
  77.         this->ucesnici = new Ucesnik[broj];
  78.         for (int i=0;i<broj;i++)
  79.         {
  80.             this->ucesnici[i]=ucesnici[i];
  81.         }
  82.     }
  83.     Maraton(Maraton &m)
  84.     {
  85.         strcpy(this->lokacija,m.lokacija);
  86.         this->broj=m.broj;
  87.         this->ucesnici = new Ucesnik[m.broj];
  88.         for (int i=0;i<broj;i++)
  89.         {
  90.             this->ucesnici[i]=m.ucesnici[i];
  91.         }
  92.     }
  93.     Maraton &operator=(Maraton &m)
  94.     {
  95.         if(this!=&m)
  96.         {
  97.          strcpy(this->lokacija,m.lokacija);
  98.         this->broj=m.broj;
  99.         this->ucesnici = new Ucesnik[m.broj];
  100.             delete [] ucesnici;
  101.         for (int i=0;i<broj;i++)
  102.         {
  103.             this->ucesnici[i]=m.ucesnici[i];
  104.         }
  105.         }
  106.         return *this;
  107.     }
  108.     Maraton &operator +=(Ucesnik &u)
  109.     {
  110.         Ucesnik *tmp = new Ucesnik[broj+1];
  111.         for(int i=0;i<broj;i++)
  112.         {
  113.             tmp[i]=ucesnici[i];
  114.         }
  115.         tmp[broj++]=u;
  116.         delete [] ucesnici;
  117.         ucesnici=tmp;
  118.         return *this;
  119.     }
  120.     float prosecnoVozrast()
  121.     {
  122.         float prosek;
  123.         for(int i=0;i<broj;i++)
  124.         {
  125.             prosek+=ucesnici[i].get_age();
  126.         }
  127.         return prosek/broj;
  128.     }
  129.     void pecatiPomladi(Ucesnik &u)
  130.     {
  131.         int brojac=0;
  132.         for(int i=0;i<broj;i++)
  133.         {
  134.             if(u.get_age()>ucesnici[i].get_age())
  135.                  cout<<ucesnici[i];
  136.  
  137.         }
  138.        
  139.     }
  140.     ~Maraton()
  141.     {
  142.         //delete [] ucesnici;
  143.     }
  144.  
  145.  
  146. };
  147. int main() {
  148.     char ime[100];
  149.     bool maski;
  150.     int vozrast, n;
  151.     cin >> n;
  152.     char lokacija[100];
  153.     cin >> lokacija;
  154.     Maraton m(lokacija);
  155.     Ucesnik **u = new Ucesnik*[n];
  156.     for(int i = 0; i < n; ++i) {
  157.         cin >> ime >> maski >> vozrast;
  158.         u[i] = new Ucesnik(ime, maski, vozrast);
  159.         m += *u[i];
  160.     }
  161.     m.pecatiPomladi(*u[n - 1]);
  162.     cout << m.prosecnoVozrast() << endl;
  163.     for(int i = 0; i < n; ++i) {
  164.         delete u[i];
  165.     }
  166.     delete [] u;
  167.     return 0;
  168. }
Add Comment
Please, Sign In to add comment