Advertisement
JStefan

[Vezbi] Maraton

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