Advertisement
NickAndNick

https://otvet.mail.ru/question/237747076

Apr 4th, 2024 (edited)
842
0
310 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | Software | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <limits>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. template<typename T>
  9. T input(istream& inp, const char* prompt) {
  10.     cout << prompt;
  11.     T value{};
  12.     inp >> value;
  13.     inp.ignore(0x1000, '\n');
  14.     return value;
  15. }
  16.  
  17. template<>
  18. string input(istream& inp, const char* prompt) {
  19.     cout << prompt;
  20.     string value;
  21.     getline(inp, value);
  22.     return value;
  23. }
  24.  
  25. struct Student {
  26.     string name;
  27.     char gender;
  28.     short course;
  29.     float height;
  30.     short mathematics;
  31.     short physics;
  32.     short informatics;
  33.     short chemistry;
  34.     short economy;
  35.     Student() :
  36.         gender(' '),
  37.         course(0),
  38.         height(0.0),
  39.         mathematics(0),
  40.         physics(0),
  41.         informatics(0),
  42.         chemistry(0),
  43.         economy(0) {}
  44.     bool is_same_height(const Student& student) const {
  45.         return height == student.height;
  46.     }
  47. private:
  48.     friend istream& operator>>(istream& inp, Student& student) {
  49.         student.name = input<string>(inp, "Фамилия: ");
  50.         student.gender = input<char>(inp, "Пол (м или ж): ");
  51.         student.course = input<short>(inp, "Курс: ");
  52.         student.height = input<float>(inp, "Рост: ");
  53.         student.mathematics = input<short>(inp, "Оценка по математике: ");
  54.         student.physics = input<short>(inp, "Оценка по физике: ");
  55.         student.informatics = input<short>(inp, "Оценка по информатике: ");
  56.         student.chemistry = input<short>(inp, "Оценка по химии: ");
  57.         student.economy = input<short>(inp, "Оценка по экономике: ");
  58.         return inp;
  59.     }
  60. };
  61.  
  62. class Students {
  63. public:
  64.     Students(const size_t length) {
  65.         table.resize(length);
  66.     }
  67.     bool is_man() const {
  68.         for (const auto& student : table) {
  69.             if (student.gender == 'м') {
  70.                 return true;
  71.             }
  72.         }
  73.         return false;
  74.     }
  75.     Student tallest_man() const {
  76.         Student sample;
  77.         for (const auto& student : table) {
  78.             if (student.gender == 'м' && sample.height < student.height) {
  79.                 sample = student;
  80.             }
  81.         }
  82.         return sample;
  83.     }
  84.     Student shortest_man() const {
  85.         Student sample;
  86.         sample.height = numeric_limits<float>::max();
  87.         for (const auto& student : table) {
  88.             if (student.gender == 'м' && student.height < sample.height) {
  89.                 sample = student;
  90.             }
  91.         }
  92.         return sample;
  93.     }
  94.     double average_height_of_men() const {
  95.         auto sum = 0.0;
  96.         auto quantity = 0U;
  97.         for (const auto& student : table) {
  98.             if (student.gender == 'м') {
  99.                 sum += student.height;
  100.                 ++quantity;
  101.             }
  102.         }
  103.         return sum / quantity;
  104.     }
  105. private:
  106.     vector<Student> table;
  107.     friend istream& operator>>(istream& inp, Students& students) {
  108.         for (auto& student : students.table) inp >> student;
  109.         return inp;
  110.     }
  111. };
  112.  
  113. int main() {
  114.     system("chcp 1251 > nul");
  115.     const auto n = input<size_t>(cin, "Количество студентов: ");
  116.     Students students(n);
  117.     cin >> students;
  118.     if (!students.is_man()) puts("Мужчин не найдено!");
  119.     else {
  120.         auto high = students.tallest_man();
  121.         auto low = students.shortest_man();
  122.         if (high.is_same_height(low)) puts("Все мужчины одинакового роста");
  123.         else {
  124.             cout.setf(ios::fixed);
  125.             cout.precision(1);
  126.             auto avg = students.average_height_of_men();
  127.             cout << "Средний рост студентов мужского пола: " << avg << '\n';
  128.             cout << "Самый высокий: " << high.name << ", его рост: " << high.height << '\n';
  129.             cout << "Самый низкий: " << low.name << ", его рост: " << low.height << '\n';
  130.         }
  131.     }
  132.     system("pause > nul");
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement