Seal_of_approval

PrSort6

Jul 6th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. struct student
  9. {
  10.     string lastname;
  11.     string firstname;
  12.     string middlename;
  13.     int year;
  14.     int marks[5];
  15. };
  16.  
  17. void print(vector<student>& group, ofstream& out)
  18. {
  19.     for (int i = 0; i < group.size(); i++)
  20.     {
  21.         out << " " << left << group[i].lastname.c_str() << " " << group[i].firstname.c_str() << " " << group[i].middlename.c_str() << " " << group[i].year << " ";
  22.         for (int j = 0; j < 5; j++)
  23.             out << group[i].marks[j] << " ";
  24.         out << endl;
  25.     }
  26. }
  27.  
  28. bool cmp(student& a, student& b) {
  29.     if (a.lastname.compare(b.lastname) > 0 ) return false;
  30.     else if (a.lastname.compare(b.lastname) < 0) return true;
  31.     else {
  32.         if (a.firstname.compare(b.firstname) > 0) return false;
  33.         else if (a.firstname.compare(b.firstname) < 0) return true;
  34.         else {
  35.             if (a.middlename.compare(b.middlename) > 0) return false;
  36.             else if (a.middlename.compare(b.middlename) <= 0) return true;
  37.         }
  38.     }
  39.     return false;
  40. }
  41.  
  42. int main(void)
  43. {
  44.     ifstream in("input.txt");
  45.     ofstream out("output.txt");
  46.     vector<student> group;
  47.    
  48.     int n;
  49.     in >> n;
  50.  
  51.     student t;
  52.     string s;
  53.     getline(in, s);
  54.     while(in >> t.lastname >> t.firstname >> t.middlename >> t.year >> t.marks[0] >> t.marks[1] >> t.marks[2] >> t.marks[3] >> t.marks[4])
  55.         group.push_back(t);
  56.  
  57.     sort(group.begin(), group.end(), cmp);
  58.     print(group, out);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment