Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <iomanip>
- using namespace std;
- struct student
- {
- int grp;
- string lastname;
- string firstname;
- string middlename;
- int year;
- int marks[5];
- int sum;
- };
- void qsort (vector <student>& group, int l, int r)
- {
- int i = l;
- int j = r;
- student m = group[(l+r)/2];
- while (i <= j)
- {
- while (group[i].lastname.compare(m.lastname) > 0) i++;
- while (group[j].lastname.compare(m.lastname) < 0) j--;
- if ( i <= j)
- {
- swap (group[i], group[j]);
- i++;
- j--;
- }
- }
- if (l < j) qsort (group, l, j);
- if (i < r) qsort (group, i, r);
- }
- void print(vector<student>& group)
- {
- for (int i = 0; i < group.size(); i++)
- {
- 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;
- for (int j = 0; j < 5; j++)
- cout << group[i].marks[j] << " ";
- cout << setw(4) << group[i].sum;
- cout << endl;
- }
- }
- int main(void)
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- vector<student> group;
- student t;
- string s;
- 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])
- {
- t.sum = 0;
- for (int i = 0; i < 5; i++)
- t.sum += t.marks[i];
- group.push_back(t);
- }
- print(group);
- qsort(group,0,(group.size()-1));
- cout<<"\n-----\n";
- print(group);
- }
- ______________________________________________
- input.txt
- 141 Severus Snape Slytherin 1836 4 8 1 4 9
- 141 Albus Dumbledor Puffendyi 1934 1 4 3 6 5
- 141 Harry Potter Griffindor 1391 1 9 3 7 2
- 141 Hermione Granger Griffindor 2014 9 5 8 3 6
- 141 Black Sirious Slytherin 1283 3 2 8 1 4
Advertisement
Add Comment
Please, Sign In to add comment