1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
class Runner
{
private:
char* name;
bool sex;
int age;
public:
Runner(const char* _name="", bool _sex=false, int _age=0)
{
this->name=new char[strlen(_name)+1];
strcpy(this->name, _name);
this->sex=_sex;
this->age=_age;
}
Runner(Runner& ob)
{
delete[] this->name;
this->name=new char[strlen(ob.name)+1];
strcpy(this->name, ob.name);
this->sex=ob.sex;
this->age=ob.age;
}
~Runner()
{
delete[] this->name;
}
Runner& operator=(Runner& ob)
{
if (this!=&ob)
{
delete[] this->name;
this->name=new char[strlen(ob.name)+1];
strcpy(this->name, ob.name);
this->sex=ob.sex;
this->age=ob.age;
}
return *this;
}
int getAge()
{
return this->age;
}
bool operator>(Runner& ob)
{
return this->age>ob.age;
}
friend ostream& operator<<(ostream& out, Runner& ob)
{
out << ob.name << endl;
if (ob.age)
out << "Male" << endl;
else
out << "Female" << endl;
out << ob.age << endl;
return out;
}
};
class Marathon
{
private:
char location[100];
Runner* array;
int numRunners;
public:
Marathon(char* _location="")
{
strcpy(this->location, _location);
this->array=NULL;
this->numRunners=0;
}
~Marathon()
{
delete[] this->array;
}
Marathon& operator+=(Runner& ob)
{
Runner *temp=new Runner[this->numRunners+1];
copy(this->array, this->array+this->numRunners, temp);
temp[this->numRunners++]=ob;
delete[] this->array;
this->array=temp;
return *this;
}
double procentAge()
{
int sum=0;
for (int i=0; i<this->numRunners; ++i)
sum+=this->array[i].getAge();
return sum/((double)(this->numRunners));
}
void printYounger(Runner &u)
{
for (int i=0; i<this->numRunners; ++i)
if (u>this->array[i])
cout << this->array[i];
}
};
int main() {
char ime[100];
bool Male;
int age, n;
cin >> n;
char location[100];
cin >> location;
Marathon m(location);
Runner **u = new Runner*[n];
for(int i = 0; i < n; ++i) {
cin >> name >> Male >> age;
u[i] = new Runner(name, male, age);
m += *u[i];
}
m.printYounger(*u[n - 1]);
cout << m.procentAge() << endl;
for(int i = 0; i < n; ++i) {
delete u[i];
}
delete [] u;
return 0;
}
|