Example:
#include <string> #include <iostream> #include <list> struct Human{ enum class Gender{ Male = 1, Female = 2 }; Human(int age = 0, Gender gender = Gender::Male, const std::string& lastName = {}): age{age}, gender{gender}, lastName{lastName} {} Human(const Human& other): age{other.age}, gender{other.gender}, lastName{other.lastName} {} Human& operator=(const Human& other){ if (this != &other){ age = other.age; gender = other.gender; lastName = other.lastName; } return *this; } int age; Gender gender; std::string lastName; }; Human readFromUser(){ int age; int gender; std::string lastName; std::cout << "Enter gender of human:\n" << "1 - Male\n" << "2 - Female" << std::endl; std::cin >> gender; std::cout << "Enter age of human:\n"; std::cin >> age; std::cout << "Enter lastName of human:"; std::cin >> lastName; return Human(age, static_cast<Human::Gender>(gender), lastName); } int main() { std::cout << "Enter number of humans:"; int n; std::cin >> n; std::list<Human> humans; for (int i = 0; i < n; i++){ humans.push_back(readFromUser()); } double ageSum = 0.0; int maleCount = 0; int femaleCount = 0; //ΡΡΠΈΡΠ°Π΅ΠΌ ΡΡΠΌΠΌΡ Π²ΠΎΠ·ΡΠ°ΡΡΠΎΠ² ΠΌΡΠΆΡΠΈΠ½, //Π° Π·Π°ΠΎΠ΄Π½ΠΎ ΠΈ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΆΠ΅Π½ΡΠΈΠ½ ΠΈ ΠΌΡΠΆΡΠΈΠ½ for (const auto& h: humans){ switch (h.gender) { case Human::Gender::Female: //ΠΆΠ΅Π½ΡΠΈΠ½Ρ femaleCount++; break; case Human::Gender::Male: ageSum += static_cast<double>(h.age); maleCount++; break; default: break; } } double ageAvg = 0.0; if (maleCount != 0){ ageAvg = ageSum / static_cast<double>(maleCount); } std::cout << "male count: " << maleCount << std::endl; std::cout << "female count: " << femaleCount << std::endl; std::cout << "ageAvg:" << ageAvg << std::endl; std::cout << "list of lastnames:" << std::endl; for (const auto& h: humans){ if (h.gender == Human::Gender::Male){ if (static_cast<double>(h.age) < ageAvg){ std::cout << "age: " << h.age << " lastname:" << h.lastName << std::endl; } } } }
Example of use:
Enter number of humans:10 Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 45 Enter lastName of human:ivanov Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 25 Enter lastName of human:Egorov Enter gender of human: 1 - Male 2 - Female 2 Enter age of human: 19 Enter lastName of human:Smirnova Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 38 Enter lastName of human:Sidorov Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 25 Enter lastName of human:Petrov Enter gender of human: 1 - Male 2 - Female 2 Enter age of human: 36 Enter lastName of human:Barabash Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 26 Enter lastName of human:Skladanov Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 27 Enter lastName of human:Karlov Enter gender of human: 1 - Male 2 - Female 2 Enter age of human: 59 Enter lastName of human:Poroshina Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 45 Enter lastName of human:Petrov male count: 7 female count: 3 ageAvg:33 list of lastnames: age: 25 lastname:Egorov age: 25 lastname:Petrov age: 26 lastname:Skladanov age: 27 lastname:Karlov