Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <limits>
- #include <vector>
- using namespace std;
- template<typename T>
- T input(istream& inp, const char* prompt) {
- cout << prompt;
- T value{};
- inp >> value;
- inp.ignore(0x1000, '\n');
- return value;
- }
- template<>
- string input(istream& inp, const char* prompt) {
- cout << prompt;
- string value;
- getline(inp, value);
- return value;
- }
- struct Student {
- string name;
- char gender;
- short course;
- float height;
- short mathematics;
- short physics;
- short informatics;
- short chemistry;
- short economy;
- Student() :
- gender(' '),
- course(0),
- height(0.0),
- mathematics(0),
- physics(0),
- informatics(0),
- chemistry(0),
- economy(0) {}
- bool is_same_height(const Student& student) const {
- return height == student.height;
- }
- private:
- friend istream& operator>>(istream& inp, Student& student) {
- student.name = input<string>(inp, "Фамилия: ");
- student.gender = input<char>(inp, "Пол (м или ж): ");
- student.course = input<short>(inp, "Курс: ");
- student.height = input<float>(inp, "Рост: ");
- student.mathematics = input<short>(inp, "Оценка по математике: ");
- student.physics = input<short>(inp, "Оценка по физике: ");
- student.informatics = input<short>(inp, "Оценка по информатике: ");
- student.chemistry = input<short>(inp, "Оценка по химии: ");
- student.economy = input<short>(inp, "Оценка по экономике: ");
- return inp;
- }
- };
- class Students {
- public:
- Students(const size_t length) {
- table.resize(length);
- }
- bool is_man() const {
- for (const auto& student : table) {
- if (student.gender == 'м') {
- return true;
- }
- }
- return false;
- }
- Student tallest_man() const {
- Student sample;
- for (const auto& student : table) {
- if (student.gender == 'м' && sample.height < student.height) {
- sample = student;
- }
- }
- return sample;
- }
- Student shortest_man() const {
- Student sample;
- sample.height = numeric_limits<float>::max();
- for (const auto& student : table) {
- if (student.gender == 'м' && student.height < sample.height) {
- sample = student;
- }
- }
- return sample;
- }
- double average_height_of_men() const {
- auto sum = 0.0;
- auto quantity = 0U;
- for (const auto& student : table) {
- if (student.gender == 'м') {
- sum += student.height;
- ++quantity;
- }
- }
- return sum / quantity;
- }
- private:
- vector<Student> table;
- friend istream& operator>>(istream& inp, Students& students) {
- for (auto& student : students.table) inp >> student;
- return inp;
- }
- };
- int main() {
- system("chcp 1251 > nul");
- const auto n = input<size_t>(cin, "Количество студентов: ");
- Students students(n);
- cin >> students;
- if (!students.is_man()) puts("Мужчин не найдено!");
- else {
- auto high = students.tallest_man();
- auto low = students.shortest_man();
- if (high.is_same_height(low)) puts("Все мужчины одинакового роста");
- else {
- cout.setf(ios::fixed);
- cout.precision(1);
- auto avg = students.average_height_of_men();
- cout << "Средний рост студентов мужского пола: " << avg << '\n';
- cout << "Самый высокий: " << high.name << ", его рост: " << high.height << '\n';
- cout << "Самый низкий: " << low.name << ", его рост: " << low.height << '\n';
- }
- }
- system("pause > nul");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement