Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "solut.h"
- #include <iostream>
- #include <sstream>
- #include <cstdlib>
- #include <map>
- #include <set>
- #include <iterator>
- #include <algorithm>
- using namespace std;
- void readData(std::istream& istr, StrVector& students, UShortVector& groups, GradesVector& studentGrades) {
- string line;
- while (std::getline(istr, line)) {
- if (line.empty())
- break;
- std::istringstream strstream(line);
- string name;
- getline(strstream, name, ';');
- students.push_back(name);
- string group;
- getline(strstream, group, ';');
- groups.push_back(std::stoi(group));
- UShortVector grades;
- string grade;
- while (getline(strstream, grade, ';')) {
- grades.push_back(std::stoi(grade));
- }
- studentGrades.push_back(grades);
- }
- }
- UShortSet getUniqGroups(const UShortVector& groups){
- UShortSet GROUPS;
- for (int i = 0; i < groups.size(); i ++){
- GROUPS.insert(groups[i]);
- }
- return GROUPS;
- }
- /*!
- * \brief printStudentsByGroup outputs a given list of students according to the
- * special filter criteria.
- *
- * \param students is a list of students.
- * \param groups is a correspoding list of student groups.
- * \param studentGrades is a correspoding list of students' grades.
- * \param thres is a threshold for an average grade.
- *
- * Function prints out a list of students passed through the corresponding vectors
- * by grouping them into study groups.
- * The function prints only those students whose average grade is greater then or
- * equal to a given threshold \a thres.
- * If a group doesn't contain any students passing the filter, its name is
- * printed anyway.
- * The group name is output starting with the beginning of the line, and every
- * student is output with the preceding tab character.
- * The names of the students inside of the group should be sorted alphabetically
- * in ascending order.
- * Example:
- * 101
- * Aminah Mccoy: 5.85714
- * Sanah Wu: 2.4
- * ...
- * 102
- * Asiyah Potter: 5.5
- * Ksawery Richard: 1.6
- * ...
- */
- double average(UShortVector gr) {
- if (gr.empty())
- return 0;
- double sum = 0;
- for(unsigned int i = 0; i < gr.size(); i++) {
- sum += gr[i];
- }
- return (sum / gr.size());
- }
- struct record {
- string name;
- UShortVector grds;
- double avr;
- const bool operator < (const record &r) const {
- return (name < r.name);
- }
- };
- void printStudentsByGroup(const StrVector& students, const UShortVector& groups, const GradesVector& studentGrades, double thres)
- {
- map<UShort, set<record>> G;
- for (auto group : getUniqGroups(groups)) {
- set<record> dummy;
- G.insert({group, dummy});
- }
- for(unsigned int i = 0; i < students.size(); i ++) {
- if (average(studentGrades[i]) >= thres) {
- G[groups[i]].insert({students[i], studentGrades[i], average(studentGrades[i])});
- }
- }
- for(auto& elem : G) {
- cout << elem.first << endl;
- for(auto& rec : elem.second) {
- cout << "\t" << rec.name << " " << rec.avr << endl;
- }
- }
- }
- int main() {
- StrVector students;
- UShortVector groups;
- GradesVector studentGrades;
- readData(cin, students, groups, studentGrades);
- unsigned long long int size = students.size();
- for (unsigned long long int i = 0; i < size; i++) {
- cout << students[i] << " " << groups[i] << " ";
- for(auto grade : studentGrades[i]) {
- cout << grade << " ";
- }
- cout << endl;
- }
- printStudentsByGroup(students, groups, studentGrades, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement