Advertisement
Proff_Ust

Students_binary

Oct 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define nmarks 2
  4. #define nsessions 2
  5. #define nstudent 3
  6.  
  7. using namespace std;
  8. struct Tstudent
  9. {
  10.     char FIO[40], exam_name[10];
  11.     unsigned int nkurs, ngroup, exam_mark;
  12.     int marks[nsessions][nmarks];
  13.     bool study_form;
  14. };
  15.  
  16. Tstudent* Read_from_file(ifstream &f)
  17. {
  18.     Tstudent* temp = new Tstudent;
  19.     f.read((char*)temp,sizeof(Tstudent));
  20.     return temp;
  21. }
  22.  
  23. unsigned int check(int m[nsessions][nmarks])
  24. {
  25.     bool otl = 1;
  26.     for(int i=0;i<nsessions;i++)
  27.     {
  28.         for(int j=0; j<nmarks; j++)
  29.             if(m[i][j]!=5)
  30.             {
  31.                 otl=0;
  32.                 break;
  33.             }
  34.         if(!otl)
  35.             break;
  36.     }
  37.     return otl;
  38. }
  39.  
  40. int Task(ifstream &f)
  41. {
  42.     unsigned int countOtl=0, count_max=0, nk=0, ng=0, ng_max=0;
  43.     unsigned int kurs_not_changed = 1;
  44.     unsigned int group_not_changed = 1;
  45.     Tstudent* temp = new Tstudent;
  46.     cout<<"_____"<<endl;
  47.     while(!f.eof())
  48.     {
  49.         f.read((char*)temp,sizeof(Tstudent));
  50.         if(ng!= temp->ngroup)
  51.         {
  52.             if(count_max<countOtl)
  53.             {
  54.                 ng_max=ng;
  55.                 count_max = countOtl;
  56.             }
  57.             if(ng!=0)
  58.                 group_not_changed = 0;
  59.             ng=temp->ngroup;
  60.             countOtl = 0;
  61.         }
  62.         if (nk==0)
  63.             nk = temp->nkurs;
  64.         if (nk!=temp->nkurs)
  65.         {
  66.             cout<<"|"<<nk<<"|"<<ng_max<<"|"<<endl;
  67.             nk=temp->nkurs;
  68.             countOtl = 0;
  69.             count_max = 0;
  70.             kurs_not_changed = 0;
  71.         }
  72.         countOtl = countOtl + check(temp->marks);
  73.  
  74.     }
  75.  
  76.     if(group_not_changed)
  77.         ng_max = ng;
  78.     if(count_max<countOtl)
  79.     {
  80.         ng_max = ng;
  81.         cout<<"|"<<nk<<"|"<<ng_max<<"|"<<endl;
  82.     }
  83.     if(kurs_not_changed)
  84.         cout<<"|"<<nk<<"|"<<ng_max<<"|"<<endl;
  85.     cout<<"_____"<<endl;
  86.     return 0;
  87. }
  88.  
  89. int Write_to_file(Tstudent* st, ofstream &f)
  90. {
  91.     if(st!=NULL)
  92.     {
  93.         f.write((char*)st,sizeof(Tstudent));
  94.         return 0;
  95.     }
  96.     else
  97.         return 1;
  98. }
  99.  
  100. Tstudent* Read_from_keyboard()
  101. {
  102.     Tstudent* temp = new Tstudent;
  103.     cout << "Введите ФИО студента ";
  104.     cin >> temp->FIO;
  105.     cout << "Введите номер курса ";
  106.     cin >> temp->nkurs;
  107.     cout << "Введите номер группы ";
  108.     cin >> temp->ngroup;
  109.     cout << "Введите оценки ";
  110.     for(int i=0;i<nsessions;i++)
  111.         for(int j=0;j<nmarks;j++)
  112.             cin >> temp->marks[i][j];
  113.     cout << "Введите форму обучение(1 - очная, 0 - заочная) ";
  114.     cin >> temp->study_form;
  115.     cout << "Введите название экзамена ";
  116.     cin >> temp->exam_name;
  117.     cout << "Введите оценку за экзамен ";
  118.     cin >> temp->exam_mark;
  119.     return temp;
  120. }
  121.  
  122. int main()
  123. {
  124.     setlocale(0,"");
  125.     char answer;
  126.     cout<<"Create new file?(y/n)";
  127.     cin>>answer;
  128.     if(answer=='y')
  129.     {
  130.         ofstream output_file("student.dat",ios::binary|ios::out);
  131.         for(int i=0;i<nstudent;i++)
  132.             Write_to_file(Read_from_keyboard(),output_file);
  133.  
  134.         output_file.close();
  135.         ifstream input_file("student.dat",ios::binary|ios::in);
  136.         Task(input_file);
  137.         input_file.close();
  138.         return 0;
  139.     }
  140.     else
  141.     {
  142.         ifstream input_file("student.dat",ios::binary|ios::in);
  143.         Task(input_file);
  144.         input_file.close();
  145.         return 0;
  146.     }
  147.     //return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement