Seal_of_approval

p78e11(1)

Mar 15th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. struct student
  8. {
  9. int grp;
  10. string lastname;
  11. string firstname;
  12. string middlename;
  13. int year;
  14. int marks[5];
  15. int sum;
  16. };
  17.  
  18. void sort(vector <student>& group)
  19. {
  20. int step = group.size() / 2;
  21. while (step > 0)
  22. {
  23. for (int i = 0; i < (group.size() - step); i++)
  24. {
  25. int j = i;
  26. while (j >= 0 && group[j].lastname.compare(group[j + step].lastname) > 0)
  27.  
  28. {
  29. //меняем их местами
  30. student temp = group[j];
  31. group[j] = group[j + step];
  32. group[j + step] = temp;
  33. j--;
  34. }
  35. }
  36. step = step / 2;//уменьшаем шаг
  37. }
  38. }
  39.  
  40.  
  41. void print(vector<student>& group)
  42. {
  43. for (int i = 0; i < group.size(); i++)
  44. {
  45. cout << setw(4) << left << group[i].grp << setw(12) << group[i].lastname.c_str() << setw(10) << group[i].firstname.c_str() << setw(15) << group[i].middlename.c_str() << setw(5) << group[i].year;
  46. for (int j = 0; j < 5; j++)
  47. cout << group[i].marks[j] << " ";
  48. cout << setw(4) << group[i].sum;
  49. cout << endl;
  50. }
  51. }
  52.  
  53.  
  54. int main(void)
  55. {
  56. freopen("input.txt","r",stdin);
  57. freopen("output.txt","w",stdout);
  58. vector<student> group;
  59.  
  60. student t;
  61. string s;
  62. getline(cin, s);
  63. while(cin >> t.grp >> t.lastname >> t.firstname >> t.middlename >> t.year >> t.marks[0] >> t.marks[1] >> t.marks[2] >> t.marks[3] >> t.marks[4])
  64. {
  65. t.sum = 0;
  66. for (int i = 0; i < 5; i++)
  67. t.sum += t.marks[i];
  68. group.push_back(t);
  69. }
  70. print(group);
  71. sort(group);
  72. cout<<"\n-----\n";
  73. print(group);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment