Advertisement
niks32

help2

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