SHARE
TWEET

Untitled

a guest Dec 8th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "solut.h"
  3. #include <iostream>
  4. #include <sstream>
  5. #include <cstdlib>
  6. #include <map>
  7. #include <set>
  8. #include <iterator>
  9. #include <algorithm>
  10.  
  11. using namespace std;
  12.  
  13.  
  14. void readData(std::istream& istr, StrVector& students, UShortVector& groups, GradesVector& studentGrades) {
  15.     string line;
  16.     while (std::getline(istr, line)) {
  17.         if (line.empty())
  18.             break;
  19.         std::istringstream strstream(line);
  20.         string name;
  21.         getline(strstream, name, ';');
  22.         students.push_back(name);
  23.         string group;
  24.         getline(strstream, group, ';');
  25.         groups.push_back(std::stoi(group));
  26.         UShortVector grades;
  27.         string grade;
  28.         while (getline(strstream, grade, ';')) {
  29.             grades.push_back(std::stoi(grade));
  30.         }
  31.         studentGrades.push_back(grades);
  32.     }
  33. }
  34.  
  35. UShortSet getUniqGroups(const UShortVector& groups){
  36.     UShortSet GROUPS;
  37.     for (int i = 0; i < groups.size(); i ++){
  38.         GROUPS.insert(groups[i]);
  39.     }
  40.     return GROUPS;
  41. }
  42.  
  43. /*!
  44.  * \brief printStudentsByGroup outputs a given list of students according to the
  45.  * special filter criteria.
  46.  *
  47.  * \param students is a list of students.
  48.  * \param groups is a correspoding list of student groups.
  49.  * \param studentGrades is a correspoding list of students' grades.
  50.  * \param thres is a threshold for an average grade.
  51.  *
  52.  * Function prints out a list of students passed through the corresponding vectors
  53.  * by grouping them into study groups.
  54.  * The function prints only those students whose average grade is greater then or
  55.  * equal to a given threshold \a thres.
  56.  * If a group doesn't contain any students passing the filter, its name is
  57.  * printed anyway.
  58.  * The group name is output starting with the beginning of the line, and every
  59.  * student is output with the preceding tab character.
  60.  * The names of the students inside of the group should be sorted alphabetically
  61.  * in ascending order.
  62.  * Example:
  63.  * 101
  64.  *         Aminah Mccoy: 5.85714
  65.  *         Sanah Wu: 2.4
  66.  *         ...
  67.  * 102
  68.  *         Asiyah Potter: 5.5
  69.  *         Ksawery Richard: 1.6
  70.  * ...
  71.  */
  72.  
  73.  
  74. double average(UShortVector gr) {
  75.     if (gr.empty())
  76.         return 0;
  77.     double sum = 0;
  78.     for(unsigned int i = 0; i < gr.size(); i++) {
  79.         sum += gr[i];
  80.     }
  81.     return (sum / gr.size());
  82. }
  83.  
  84. struct record {
  85.     string name;
  86.     UShortVector grds;
  87.     double avr;
  88.     const bool operator < (const record &r) const {
  89.         return (name < r.name);
  90.     }
  91. };
  92.  
  93. void printStudentsByGroup(const StrVector& students, const UShortVector& groups, const GradesVector& studentGrades, double thres)
  94. {
  95.     map<UShort, set<record>> G;
  96.     for (auto group : getUniqGroups(groups)) {
  97.         set<record> dummy;
  98.         G.insert({group, dummy});
  99.     }
  100.  
  101.     for(unsigned int i = 0; i < students.size(); i ++) {
  102.         if (average(studentGrades[i]) >= thres) {
  103.             G[groups[i]].insert({students[i], studentGrades[i], average(studentGrades[i])});
  104.         }
  105.     }
  106.  
  107.     for(auto& elem : G) {
  108.         cout << elem.first << endl;
  109.         for(auto& rec : elem.second) {
  110.             cout << "\t" << rec.name << " " << rec.avr << endl;
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116. int main() {
  117.  
  118.     StrVector students;
  119.     UShortVector groups;
  120.     GradesVector studentGrades;
  121.     readData(cin, students, groups, studentGrades);
  122.  
  123.     unsigned long long int size = students.size();
  124.     for (unsigned long long int i = 0; i < size; i++) {
  125.         cout << students[i] << " " << groups[i] << " ";
  126.         for(auto grade : studentGrades[i]) {
  127.             cout << grade << " ";
  128.         }
  129.         cout << endl;
  130.     }
  131.  
  132.     printStudentsByGroup(students, groups, studentGrades, 0);
  133.  
  134.     return 0;
  135. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top