Seal_of_approval

p78e11(3)

May 25th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 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. void qsort (vector <student>& group, int l, int r)
  18. {
  19.     int i = l;
  20.     int j = r;
  21.     student m = group[(l+r)/2];
  22.  
  23.     while (i <= j)
  24.     {
  25.         while (group[i].lastname.compare(m.lastname) > 0) i++;
  26.         while (group[j].lastname.compare(m.lastname) < 0) j--;
  27.             if ( i <= j)
  28.             {
  29.                 swap (group[i], group[j]);
  30.                 i++;
  31.                 j--;
  32.             }
  33.     }
  34.  
  35.     if (l < j) qsort (group, l, j);
  36.     if (i < r) qsort (group, i, r);
  37. }
  38.  
  39. void print(vector<student>& group)
  40. {
  41.         for (int i = 0; i < group.size(); i++)
  42.         {
  43.                 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;
  44.                 for (int j = 0; j < 5; j++)
  45.                         cout << group[i].marks[j] << " ";
  46.                 cout << setw(4) << group[i].sum;
  47.                 cout << endl;
  48.         }
  49. }
  50.  
  51.  
  52. int main(void)
  53. {
  54.         freopen("input.txt","r",stdin);
  55.         freopen("output.txt","w",stdout);
  56.         vector<student> group;
  57.  
  58.         student t;
  59.         string s;
  60.  
  61.         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])
  62.         {
  63.                 t.sum = 0;
  64.                 for (int i = 0; i < 5; i++)
  65.                         t.sum += t.marks[i];
  66.                 group.push_back(t);
  67.         }
  68.        
  69.         print(group);  
  70.         qsort(group,0,(group.size()-1));
  71.         cout<<"\n-----\n";
  72.         print(group);
  73. }
  74.  
  75. ______________________________________________
  76. input.txt
  77. 141 Severus Snape    Slytherin  1836 4 8 1 4 9
  78. 141 Albus Dumbledor  Puffendyi  1934 1 4 3 6 5
  79. 141 Harry Potter     Griffindor 1391 1 9 3 7 2
  80. 141 Hermione Granger Griffindor 2014 9 5 8 3 6
  81. 141 Black Sirious    Slytherin  1283 3 2 8 1 4
Advertisement
Add Comment
Please, Sign In to add comment