Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. #include <fstream>
  5. using std::ifstream;
  6. using std::ofstream;
  7. #include <iomanip>
  8. #include <string>
  9. using std::string;
  10. #include <vector>
  11. using std::vector;
  12.  
  13. class student {
  14. public :
  15. string surname;
  16. int m, f, inf;
  17. float aver;
  18.  
  19. bool operator < (student&);
  20. void calculateAver();
  21.  
  22. static void sort(vector<student>&);
  23. };
  24.  
  25. int main() {
  26. ifstream in("input.txt");
  27. ofstream out("output.txt");
  28. int N;
  29. in >> N;
  30. in.ignore(1, '\n');
  31. vector<student> students;
  32. for (int i = 0; i < N; ++i) {
  33. student stud;
  34. in >> stud.surname >> stud.m;
  35. in.ignore(1, ',');
  36. in >> stud.f;
  37. in.ignore(1, ',');
  38. in >> stud.inf;
  39. stud.calculateAver();
  40. students.push_back(stud);
  41. }
  42. student::sort(students);
  43. out << std::fixed << std::setprecision(2);
  44. for (student s : students) {
  45. out << s.surname << " " << s.m << " " << s.f << " " << s.inf
  46. << " " << s.aver << "\n";
  47. }
  48. in.close();
  49. out.close();
  50. }
  51.  
  52. void student::sort(vector<student>& students) {
  53. int n = students.size();
  54. for (int i = 0; i < n - 1; ++i) {
  55. for (int j = i + 1; j < n; ++j) {
  56. if (students[i] < students[j]) {
  57. student t = students[i];
  58. students[i] = students[j];
  59. students[j] = t;
  60. }
  61. }
  62. }
  63. }
  64.  
  65. bool student::operator< (student& right) {
  66. if (aver == right.aver) {
  67. return surname > right.surname;
  68. }
  69. else {
  70. return aver < right.aver;
  71. }
  72. }
  73.  
  74. void student::calculateAver() {
  75. aver = (m + f + inf) / 3.0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement