Advertisement
RobertDeMilo

BB5.2 Функции или методы классов?

Jun 21st, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5.  
  6. using namespace std;
  7.  
  8. enum class Gender
  9. {
  10.     FEMALE,
  11.     MALE
  12. };
  13.  
  14. struct Person
  15. {
  16.     int age;
  17.     Gender gender;
  18.     bool is_employed;
  19. };
  20.  
  21.  
  22. struct AgeStats
  23. {
  24.     int total;
  25.     int females;
  26.     int males;
  27.     int employed_females;
  28.     int unemployed_females;
  29.    
  30.     int employed_males;
  31.     int unemployed_males;
  32. };
  33.  
  34.  
  35.     template <typename InputIt>
  36.     int ComputeMedianAge(InputIt range_begin, InputIt range_end)
  37.     {
  38.        
  39.         if(range_begin==range_end)
  40.         {
  41.             return 0;
  42.         }
  43.        
  44.         vector<typename InputIt::value_type> range_copy(range_begin,range_end);
  45.        
  46.         auto middle = begin(range_copy) + range_copy.size()/2;
  47.        
  48.         nth_element(begin(range_copy),middle, end(range_copy),[](const Person&lhs, const Person& rhs)
  49.                     {return lhs.age<rhs.age;});
  50.        
  51.         return middle->age;
  52.     }
  53.  
  54.  
  55. struct StatsManager
  56. {
  57.   static vector<Person> ReadPersons(istream& in_stream = cin);
  58.   static AgeStats ComputeStats(vector<Person> persons);
  59.   static void PrintStats(const AgeStats& stats,ostream& out_stream = cout);
  60. };
  61.  
  62. vector<Person> StatsManager::ReadPersons(istream& in_stream = cin)
  63. {
  64.     int person_count;
  65.  
  66.     in_stream>>person_count;
  67.     vector<Person> persons;
  68.     persons.reserve (person_count);
  69.  
  70. for(int i=0; i<person_count; ++i)
  71. {
  72.     int age,gender, is_employed;
  73.     in_stream >> age >> gender >> is_employed;
  74.     Person person {age,static_cast<Gender>(gender),is_employed == 1};
  75.     persons.push_back(person);
  76. }
  77.     return persons;
  78. }
  79.  
  80. AgeStats StatsManager::ComputeStats(vector<Person> persons)
  81. {
  82.     auto females_end = partition(begin(persons),end(persons),[](const Person&p)
  83.                                  {return p.gender == Gender::FEMALE; });
  84.    
  85.     auto employed_females_end = partition(begin(persons),females_end,[](const Person&p)
  86.                                           {return p.is_employed; });
  87.      
  88.     auto employed_males_end = partition(females_end,end(persons),[](const Person&p)
  89.                                         {return p.is_employed; });
  90.        
  91.     return {ComputeMedianAge(begin(persons),end(persons)),
  92.        
  93.             ComputeMedianAge(begin(persons),females(end)),
  94.        
  95.             ComputeMedianAge(females(end),end(persons)),
  96.          
  97.             ComputeMedianAge(employed_females_end,females_end),
  98.          
  99.             ComputeMedianAge(females_end,employed_males_end),
  100.          
  101.             ComputeMedianAge(employed_males_end,end(persons))  };
  102. }
  103.  
  104. void StatsManager::PrintStats(const AgeStats& stats,ostream& out_stream = cout)
  105. {
  106.     out_stream << "Median age = " << stats.total << endl <<
  107.    
  108.                   "Median age for females = " << stats.females << endl <<
  109.                
  110.                   "Median age for males = " << stats.males << endl <<
  111.          
  112.                   "Median age for employed females = " << stats.employed_females << endl <<
  113.          
  114.                   "Median age for unemployed females = " << stats.unemployed_females << endl <<
  115.          
  116.                   "Median age for employed males = " << stats.employed_males << endl <<
  117.          
  118.                   "Median age for unemployed males = " << stats.unemployed_males << endl;
  119. }
  120.  
  121. int main()
  122. {
  123.     //StatsManager::PrintStats(StatsManager::ComputeStats(StatsManager::ReadPersons()));
  124.     return 0;
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement