Advertisement
Eksekk

Colloquium

May 18th, 2021
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. struct Student
  7. {
  8.     std::string imie;
  9.     std::vector<double> oceny;
  10.  
  11.     Student(const std::string& imie, const std::vector<double>& oceny) : imie(imie), oceny(oceny) {}
  12. };
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     std::stringstream ss;
  17.     std::fstream plik(argv[1], std::ios::in);
  18.     std::vector<Student> studenci;
  19.  
  20.     std::string linia;
  21.     getline(plik, linia);
  22.     std::vector<double> maksymalneOceny;
  23.     ss << linia;
  24.     double n;
  25.     while (ss >> n)
  26.     {
  27.         maksymalneOceny.push_back(n);
  28.     }
  29.  
  30.     std::string nazwa;
  31.     while (plik >> nazwa)
  32.     {
  33.         std::vector<double> oceny(maksymalneOceny.size());
  34.         for (int i = 0; i < oceny.size(); ++i)
  35.         {
  36.             plik >> oceny[i];
  37.         }
  38.         studenci.push_back(Student(nazwa, oceny));
  39.     }
  40.  
  41.     for (int i = 0; i < studenci.size(); ++i)
  42.     {
  43.         const auto& s = studenci[i];
  44.         std::cout << s.imie<< ' ' ;
  45.         double suma = 0;
  46.         for (int j = 0; j < s.oceny.size(); ++j)
  47.         {
  48.             suma += s.oceny[j];
  49.         }
  50.         std::cout << suma << '\n';
  51.     }
  52.     std::cout << '\n';
  53.     for (int i = 0; i < maksymalneOceny.size(); ++i)
  54.     {
  55.         double srednia = 0;
  56.         for (int j = 0; j < studenci.size(); ++j)
  57.         {
  58.             srednia += studenci[j].oceny[i];
  59.         }
  60.         srednia /= maksymalneOceny.size();
  61.         std::cout << i + 1 << ' ' << srednia << '\n';
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement