Advertisement
niks32

Untitled

Oct 19th, 2022
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.79 KB | None | 0 0
  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<iterator>
  5.  
  6. using namespace std;
  7.  
  8. struct Student
  9. {
  10.     char name[30];
  11.     int group;
  12.     int score[5];
  13. };
  14.  
  15. void setDataStudent(vector<Student> &currentStudent, int count);
  16. void setDataTest(vector<Student> &currentStudent, int count);       //Данные для теста
  17.  
  18. void printDataStudent(vector<Student> &currentStudents);
  19. void sortStudentMidScore(vector<Student>& currentStudent);
  20.  
  21. void chekMidScore(const vector<Student>& currentStudents, vector<Student>& filterResult);
  22. void chekScoreToTwo(const vector<Student> &currentStudents, vector<Student>& filterResult);
  23. void chekScoreToForFive(const vector<Student>& currentStudents, vector<Student>& filterResult);
  24.  
  25.  
  26. int main()
  27. {
  28.     setlocale(LC_ALL, "Rus");
  29.     int countOfStudent = 4;
  30.     vector<Student> currentStudents;
  31.     vector<Student> filterResult;
  32.     int key;
  33.  
  34.     //Объяви тип указатель на функцию, которая принимает на вход массив структур студентов (и
  35.     typedef void(*studentFilter)(const vector<Student>&, vector<Student>&);
  36.     //Объяви map, ключ это номер выбора из меню, значение это указатель на функцию
  37.     const map<int, studentFilter> MENU =
  38.     {  
  39.         {1, chekMidScore},
  40.         {2, chekScoreToTwo},
  41.         {3, chekScoreToForFive}
  42.     };
  43.  
  44.     map<int, studentFilter>::const_iterator it_MENU;
  45.  
  46.     //setDataStudent(currentStudents, countOfStudent);          //Ввод данных о студентах с клавиатуры
  47.     setDataTest(currentStudents, countOfStudent);
  48.     sortStudentMidScore(currentStudents);
  49.    
  50.     cout << "Выберите действие:" << endl;
  51.     cout << "1) Средний балл студента больше 4.0" << endl;
  52.     cout << "2) имеющих хотя бы одну оценку 2" << endl;
  53.     cout << "3) имеющих оценки 4 и 5" << endl;
  54.     cout << " > ";
  55.     cin >> key;
  56.    
  57.     it_MENU = MENU.find(key);
  58.     if (it_MENU == MENU.end())
  59.     {
  60.         cout << "Такой инструкции нет!!!" << endl;
  61.     }
  62.     else
  63.     {
  64.         it_MENU->second(currentStudents, filterResult);
  65.         printDataStudent(filterResult);
  66.     }
  67.  
  68.     return 0;
  69. }
  70.  
  71. void setDataStudent(vector<Student> &currentStudents, int count)
  72. {
  73.     for (int i = 0; i < count; i++)
  74.     {
  75.         currentStudents.push_back(Student());
  76.         cout << "----Ввод данных о студентах------------------------------------------" << endl;
  77.         cout << "Введите данные студента №" << i + 1 << endl;
  78.         cout << "Введите Имя, фамилие студента: ";
  79.         cin.getline(currentStudents[i].name, 30);
  80.         cout << "Введите номер группы: ";
  81.         cin >> currentStudents[i].group;
  82.  
  83.         double tmpSumScore = 0;
  84.  
  85.         for (int j = 0; j < 5; j++)
  86.         {
  87.             cout << "Введите оценку №" << j + 1 << " ";
  88.             cin >> currentStudents[i].score[j];
  89.         }
  90.         cin.get(); // считывает из потока Enter который пользователь нажимает после последнего ввода
  91.     }
  92. }
  93.  
  94. void printDataStudent(vector<Student> &currentStudents)
  95. {
  96.     for (int i = 0; i < currentStudents.size(); i++)
  97.     {
  98.         cout << "------------------------------------------------" << endl;
  99.         cout << "Имя, фамилие студента: " << currentStudents[i].name << endl;
  100.         cout << "Номер группы: " << currentStudents[i].group << endl;
  101.         cout << "!!! Техническое, убрать перед релизом!!! Оценки: ";
  102.         for (int j = 0; j < 5; j++)
  103.         {
  104.             cout << currentStudents[i].score[j] << "   ";
  105.         }
  106.         cout << endl;
  107.     }
  108.    
  109.     if (currentStudents.empty())
  110.     {
  111.         cout << "Студентов удовлетваряющих условиям нет!" << endl;
  112.     }
  113. }
  114.  
  115. void sortStudentMidScore(vector<Student> &currentStudent)
  116. {
  117.     if (!currentStudent.empty())
  118.     {
  119.         int sizeOfVector = currentStudent.size();
  120.         //Считаем средний балл
  121.         vector<double> tmpMidlScoerStudents;    // средние баллы студентов
  122.         double tmpMidlScore;                    // средний балл текущего студента
  123.         for (int i = 0; i < sizeOfVector; i++)
  124.         {
  125.             tmpMidlScore = 0;
  126.             for (int j = 0; j < 5; j++) {
  127.                 tmpMidlScore = tmpMidlScore + currentStudent[i].score[j];
  128.             }
  129.             tmpMidlScoerStudents.push_back(tmpMidlScore / 5);
  130.         }
  131.  
  132.         //Сортировка студентов по среднему баллу tmpMidlScoerStudents[]
  133.         Student tmpStudentForSwap;
  134.  
  135.         for (int i = 0; i < sizeOfVector; i++)
  136.         {
  137.             int indexMinElement = i;
  138.             tmpMidlScore = tmpMidlScoerStudents[i];
  139.             for (int j = i; j < sizeOfVector; j++)
  140.             {
  141.                 if (tmpMidlScore > tmpMidlScoerStudents[j])
  142.                 {
  143.                     tmpMidlScore = tmpMidlScoerStudents[j];
  144.                     indexMinElement = j;
  145.                 }
  146.             }
  147.  
  148.             tmpMidlScoerStudents[indexMinElement] = tmpMidlScoerStudents[i];
  149.             tmpMidlScoerStudents[i] = tmpMidlScore;
  150.  
  151.             tmpStudentForSwap = currentStudent[indexMinElement];
  152.             currentStudent[indexMinElement] = currentStudent[i];
  153.             currentStudent[i] = tmpStudentForSwap;
  154.         }
  155.     }
  156. }
  157.  
  158. void chekMidScore(const vector<Student>& currentStudents, vector<Student>& filterResult)
  159. {
  160.     filterResult.clear();
  161.    
  162.     double tmpMidlScore; // средний балл текущего студента
  163.     for (int i = 0; i < currentStudents.size(); i++)
  164.     {
  165.         tmpMidlScore = 0;
  166.         for (int j = 0; j < 5; j++)
  167.         {
  168.             tmpMidlScore = tmpMidlScore + currentStudents[i].score[j];
  169.         }
  170.         tmpMidlScore = tmpMidlScore / 5;
  171.        
  172.         if (tmpMidlScore > 4.0)
  173.         {
  174.             filterResult.push_back(currentStudents[i]);
  175.         }
  176.     }
  177. }
  178.  
  179. void chekScoreToTwo(const vector<Student>& currentStudents, vector<Student>& filterResult)
  180. {
  181.     filterResult.clear();
  182.  
  183.     for (int i = 0; i < currentStudents.size(); i++)
  184.     {
  185.         for (int j = 0; j < 5; j++)
  186.         {
  187.             if (currentStudents[i].score[j] == 2)
  188.             {
  189.                 filterResult.push_back(currentStudents[i]);
  190.                 break;
  191.             }
  192.         }
  193.     }
  194. }
  195.  
  196. void chekScoreToForFive(const vector<Student> &currentStudents, vector<Student>& filterResult)
  197. {
  198.     filterResult.clear();
  199.  
  200.     for (int i = 0; i < currentStudents.size(); i++)
  201.     {
  202.         //есть еще идея как реализовать проверку. Сперва добавлять все обьекты, после (если находим другие оценки, удалять)
  203.         for (int j = 0; j < 5; j++)
  204.         {
  205.             if (currentStudents[i].score[j] == 4 || currentStudents[i].score[j] == 5)
  206.             {
  207.                 if (j = 5)
  208.                 {
  209.                     filterResult.push_back(currentStudents[i]);
  210.                 }
  211.             }
  212.             else
  213.             {
  214.                 break;
  215.             }
  216.         }
  217.     }
  218. }
  219.  
  220.  
  221. void setDataTest(vector<Student> &currentStudent, int count)
  222. {
  223.     for (int i = 0; i < count; i++)
  224.     {
  225.         currentStudent.push_back(Student());
  226.     }
  227.    
  228.     strcpy_s(currentStudent[0].name, "Первый");
  229.     currentStudent[0].group = 1;
  230.     currentStudent[0].score[0] = 5;
  231.     currentStudent[0].score[1] = 5;
  232.     currentStudent[0].score[2] = 5;
  233.     currentStudent[0].score[3] = 5;
  234.     currentStudent[0].score[4] = 5;
  235.     //Средний балл студента  5
  236.  
  237.     strcpy_s(currentStudent[1].name, "Второй");
  238.     currentStudent[1].group = 2;
  239.     currentStudent[1].score[0] = 1;
  240.     currentStudent[1].score[1] = 1;
  241.     currentStudent[1].score[2] = 2;
  242.     currentStudent[1].score[3] = 1;
  243.     currentStudent[1].score[4] = 2;
  244.     //Средний балл студента 1.4
  245.  
  246.     strcpy_s(currentStudent[2].name, "Третий");
  247.     currentStudent[2].group = 3;
  248.     currentStudent[2].score[0] = 4;
  249.     currentStudent[2].score[1] = 4;
  250.     currentStudent[2].score[2] = 5;
  251.     currentStudent[2].score[3] = 4;
  252.     currentStudent[2].score[4] = 5;
  253.     //Средний балл студента 4.4
  254.  
  255.     strcpy_s(currentStudent[3].name, "Четвертый");
  256.     currentStudent[3].group = 4;
  257.     currentStudent[3].score[0] = 3;
  258.     currentStudent[3].score[1] = 3;
  259.     currentStudent[3].score[2] = 5;
  260.     currentStudent[3].score[3] = 4;
  261.     currentStudent[3].score[4] = 2;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement