awsmpshk

Сортировки, Кузичкин 141 группа

May 21st, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. struct Student
  8. {
  9.   string F, I;
  10.   int score[3];
  11.   int fullScore()
  12.   {
  13.     return score[0] + score[1] + score[2];
  14.   }
  15.   double averageGeom()
  16.   {
  17.     return sqrt(score[0] * score[1] * score[2]);
  18.   }
  19.   bool pred(const Student& stud)
  20.   {
  21.     if (fullScore() < stud.fullScore()) return true;
  22.     else if (fullScore() == stud.fullScore() && averageGeom() < stud.averageGeom()) return true;
  23.     else if (averageGeom() == stud.averageGeom()) return false;
  24.   }
  25. };
  26.  
  27. vector<Student> initVector(ifstream& in)
  28. {
  29.   vector<Student> base;
  30.   while (in.peek() != EOF)
  31.   {
  32.     Student stud;
  33.     in >> stud.F >> stud.I;
  34.     for (int i = 0; i < 3; ++i) in >> stud.score[i];
  35.     base.push_back(stud);
  36.   }
  37.   return base;
  38. }
  39.  
  40. void printVector(vector<Student> base, ofstream& out)
  41. {
  42.   for (Student stud : base)
  43.   {
  44.     out << stud.F << " " << stud.I << " ";
  45.     for (int i = 0; i < 3; ++i) out << elem.score[i];
  46.   }
  47. }
  48.  
  49. int main()
  50. {
  51.   ifstream in("input.txt");
  52.   ofstream out("output.txt");
  53.   vector<Student> base = initVector(in);
  54.   sort(base.begin(), base.end(), pred);
  55.   printVector(base, out);
  56. }
  57.  
  58. input.txt:
  59. sidorov sergey 100 100 100
  60. kuzichkin pavel 50 50 50
  61. orlov daniil 90 90 90
  62. imanov rauf 100 100 100
  63. tihonov levan 84 87 99
  64.  
  65. output.txt:
  66. kuzichkin pavel 50 50 50
  67. tihonov levan 84 87 99  
  68. orlov daniil 90 90 90
  69. imanov rauf 100 100 100
  70. sidorov sergey 100 100 100
Add Comment
Please, Sign In to add comment