11

 avatar
gorazd
c_cpp
a year ago
3.0 kB
8
Indexable
#include <iostream>
#include <cstring>
#include  <iomanip>
using namespace std;

class Ucesnik {
    char *name;
    bool gender;
    int age;
public:
    Ucesnik(const char *_name = "", bool _gender = true, int _age = 0) {
        age = _age;
        gender = _gender;
        name = new char[strlen(_name)+1];
        strcpy(name, _name);
    }
    ~Ucesnik() {
        delete[] name;
    }
    Ucesnik(const Ucesnik &s) {
        age = s.age;
        gender = s.gender;
        name = new char[strlen(s.name)];
        strcpy(name, s.name);
    }
    Ucesnik &operator=(const Ucesnik &other) {
        if (this==&other)
            return *this;
        delete[] name;
        name = new char[strlen(other.name)+1];
        strcpy(name,other.name);
        gender = other.gender;
        age =other.age;
        return *this;
    }
    int getAge(){return age;}
    bool operator>(const Ucesnik &other) {
        return this->age > other.age;
    }
    friend ostream &operator<<(ostream &os, const Ucesnik &u);
};
ostream &operator<<(ostream &os, const Ucesnik &u) {
    os << u.name << endl;
    if (u.gender)
        os << "mashki" << endl;
    else
        os << "zhenski" << endl;
    os << u.age << endl;
    return os;
}

class Marathon {
    char location[100];
    int numberOf;
    Ucesnik *ucesnici;
public:
    Marathon(const char *_location = "", int _numberOf = 0, const Ucesnik *_ucesnici = nullptr) {
        strcpy(location, _location);
        numberOf = _numberOf;
        if (_ucesnici!=nullptr and numberOf>0) {
            ucesnici = new Ucesnik[numberOf];
            for (int i = 0; i<numberOf; i++) {
                ucesnici[i] = _ucesnici[i];
            }
        }
        else
            ucesnici = nullptr;
    }
    ~Marathon() {
        delete[] ucesnici;
    }
    Marathon &operator+=(const Ucesnik &u) {
        Ucesnik *temp = new Ucesnik[numberOf+1];
        for (int i = 0; i<numberOf; i++)
            temp[i] = ucesnici[i];
        temp[numberOf] = u;
        delete[] ucesnici;
        ucesnici = temp;
        numberOf++;
        return *this;
    }
    float prosecnoVozrast() {
        float age = 0.0;
        for (int i = 0; i<numberOf; i++)
            age += ucesnici[i].getAge();
        return age/static_cast<float>(numberOf);
    }
    void pecatiPomladi(Ucesnik &u) {
        for (int i = 0; i<numberOf; i++) {
            if (u>ucesnici[i])
                cout << ucesnici[i];
        }
    }
};

int main() {
    char ime[100];
    bool maski;
    int vozrast, n;
    cin >> n;
    char lokacija[100];
    cin >> lokacija;
    Marathon m(lokacija);
    Ucesnik **u = new Ucesnik*[n];
    for(int i = 0; i < n; ++i) {
        cin >> ime >> maski >> vozrast;
        u[i] = new Ucesnik(ime, maski, vozrast);
        m += *u[i];
    }
    m.pecatiPomladi(*u[n - 1]);
    cout << m.prosecnoVozrast() << endl;
    for(int i = 0; i < n; ++i) {
        delete u[i];
    }
    delete [] u;
    return 0;
}
Editor is loading...
Leave a Comment