Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. class Students {
  8. private:
  9.     struct Student {
  10.         struct Grade
  11.         {
  12.             int math;
  13.             int inform;
  14.             int phys;
  15.         };
  16.         std::string surname;
  17.         std::string name;
  18.         std::string pantronymic;
  19.         Grade grade;
  20.     };
  21.  
  22.     std::vector<Student> data;
  23.     std::ifstream ifs;
  24.  
  25.     void readData() {
  26.         int start = 0, space = 0;
  27.         for (char a; ifs.get(a);) {
  28.             if (start != 0) {
  29.                 if (a != '\n') {
  30.                     if (space == 5) {
  31.                         if (a != ' ') {
  32.                             data.at(start - 1).grade.inform = std::atoi(( const char*) & a);
  33.                         }
  34.                         else {
  35.                             ++space;
  36.                         }
  37.                     }
  38.                     if (space == 4) {
  39.                         if (a != ' ') {
  40.                             data.at(start - 1).grade.phys = std::atoi(( const char*) & a);
  41.                         }
  42.                         else {
  43.                             ++space;
  44.                         }
  45.                     }
  46.                     if (space == 3) {
  47.                         if (a != ' ') {
  48.                             data.at(start - 1).grade.math = std::atoi(( const char*) & a);
  49.                         }
  50.                         else {
  51.                             ++space;
  52.                         }
  53.                     }
  54.                     if (space == 2) {
  55.                         if (a != ' ') {
  56.                             data.at(start - 1).pantronymic += a;
  57.                         }
  58.                         else {
  59.                             ++space;
  60.                         }
  61.                     }
  62.                     if (space == 1) {
  63.                         if (a != ' ') {
  64.                             data.at(start - 1).name += a;
  65.                         }
  66.                         else {
  67.                             ++space;
  68.                         }
  69.                     }
  70.                     if (space == 0) {
  71.                         if (a != ' ') {
  72.                             data.at(start - 1).surname += a;
  73.                         }
  74.                         else {
  75.                             ++space;
  76.                         }
  77.                     }
  78.                 }
  79.                 else {
  80.                     ++start;
  81.                     space = 0;
  82.                 }
  83.             }
  84.             else {
  85.                 if (a == '\n') {
  86.                     ++start;
  87.                 }
  88.                 if (a != '\n') {
  89.                     data.resize(std::atoi(( const char*) & a));
  90.                 }
  91.             }
  92.         }
  93.     }
  94.     void sortData() {
  95.         for (int index_1 = 0; index_1 < data.size() - 1; ++index_1) {
  96.             float midgradeindex_1 = (data.at(index_1).grade.inform + data.at(index_1).grade.math + data.at(index_1).grade.phys) / 3;
  97.             bool over = true;
  98.             for (int index_2 = index_1 + 1; index_2 < data.size(); ++index_2) {
  99.                 float midgradeindex_2 = (data.at(index_2).grade.inform + data.at(index_2).grade.math + data.at(index_2).grade.phys) / 3;
  100.                 if (midgradeindex_2 > midgradeindex_1) {
  101.                     over = false;
  102.                     std::swap(data.at(index_1), data.at(index_2));
  103.                 }
  104.             }
  105.             if (over) {
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.  
  111. public:
  112.     Students() {
  113.         ifs.open("F:\\input_data.txt", std::ios::in);
  114.         readData();
  115.         sortData();
  116.     }
  117.  
  118.     void printData() {
  119.         for (auto& stuedents : data) {
  120.             std::cout << stuedents.surname << " " << stuedents.name << " " << stuedents.pantronymic << " - " << "Информатика: " << stuedents.grade.inform
  121.                 << " Математика: " << stuedents.grade.math << " Физика: " << stuedents.grade.phys << std::endl;
  122.         }
  123.     }
  124. };
  125.  
  126. void main() {
  127.     setlocale(0, "");
  128.     Students students;
  129.     students.printData();
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement