Advertisement
danny-iv

zadacha 2222

Dec 22nd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct student
  7. {
  8.     string prof;
  9.     int group;
  10.     int fn;
  11.     char name[30];
  12.     float average;
  13. };
  14.  
  15. fstream fp;
  16.  
  17. void input(student st[])
  18. {
  19.     int i, n;
  20.     do{
  21.         cout << "\n Insert size of a students: ";
  22.         cin >> n;
  23.     } while (n<1 || n>30);
  24.  
  25.     fp.open("st.dat", ios::binary | ios::out);
  26.     if (!fp){
  27.         cout << "\n Error";
  28.         exit(1);
  29.     }
  30.  
  31.     for (i = 0; i<n; i++)
  32.     {
  33.         fflush(stdin);
  34.         cout << "\n FN:";
  35.         cin >> st[i].fn;
  36.         cout << "\n Name:";
  37.         cin >> st[i].name;
  38.         cout << "\n Average:";
  39.         cin >> st[i].average;
  40.         cout << "\n Group:";
  41.         cin>> st[i].group;
  42.         if (st[i].group < 1 || st[i].group > 4){
  43.             cout << "ERROR !!!"<<endl;
  44.         }
  45.         cin.ignore();
  46.         cout << "\n Profesiq: ";
  47.         getline (cin, st[i].prof);
  48.         if (!(strcmp("E", st[i].prof.c_str()) == 0 || strcmp("AIUT", st[i].prof.c_str()) == 0)){
  49.             cout << "ERROR !!!"<<endl;
  50.             break;
  51.         }
  52.     }
  53.     fp.write((char*)st, n*sizeof(student));
  54.     fp.close();
  55. }
  56. void append()
  57. {
  58.     student s;
  59.     long pos, n;
  60.     fp.open("st.dat", ios::binary | ios::in);
  61.     if (!fp){
  62.         cout << "\n Error"; exit(1);
  63.     }
  64.     fp.seekg(0L, ios::end);
  65.     pos = fp.tellg(); fp.close();
  66.     fp.open("st.dat", ios::binary | ios::app);
  67.     if (!fp){
  68.         cout << "\n Error"; exit(1);
  69.     }
  70.     n = pos / (sizeof(student));
  71.     cout << "\n Input " << n + 1 << " student \n";
  72.     fflush(stdin);
  73.     cout << "\n FN:"; cin >> s.fn;
  74.     cout << "\n Name:"; cin >> s.name;
  75.     cout << "\n Average:"; cin >> s.average;
  76.     cout << "\n Group:"; cin >> s.group;
  77.     cout << "\n Profesion:"; cin >> s.prof;
  78.     fp.write((char*)&s, sizeof(student));
  79.     fp.close();
  80. }
  81. void output(student st[])
  82. {
  83.     int i, n;
  84.     long pos;
  85.     student s;
  86.     cout << "\n List of students \n ";
  87.     fp.open("st.dat", ios::binary | ios::in);
  88.     if (fp.fail()){ cout << "\n Error"; exit(1); }
  89.     fp.seekg(0L, ios::end);
  90.     pos = fp.tellg();
  91.     fp.seekg(0L, ios::beg);
  92.     n = pos / (sizeof(student));
  93.     for (i = 0; i<n; i++)
  94.     {
  95.         fp.read((char*)&s, sizeof(student));
  96.         st[i] = s;
  97.     }
  98.     fp.close();
  99.     for (i = 0; i<n; i++)
  100.         cout << "\n FN:" << st[i].fn << "\t Name:" << st[i].name << "\t Average:" << st[i].average << "\t Profesion: " << st[i].prof;
  101. }
  102. void output_excel(student st[])
  103. {
  104.     int i, n;
  105.     long pos; // poziciq
  106.     student a;
  107.     fp.open("st.dat", ios::binary | ios::in);
  108.     if (!fp)
  109.     {
  110.         cout << endl << "ERORR!!" << endl;
  111.         exit(1);
  112.     }
  113.     fp.seekg(0L, ios::end); // premestva ukazatelq v kraq na faila ( L sled nulata ne 1 );
  114.     pos = fp.tellg(); // opredelq dyljinata na faila v br baitove
  115.     n = pos / sizeof(student); // opredelq kolko abonata ima v masiva ( delish sichki baitove na goleminata v baitove na edin abonat);
  116.     fp.seekg(0L, ios::beg); // pozicionariash v nachaloto shtoto v momenta e v kraq i nqma da moje da prochete sudurjanieto na faila
  117.     fp.read((char *)st, n * sizeof (student));
  118.     fp.close();
  119.     for ( i = 0; i < n; i++){
  120.         if (st[i].average >= 5.5 && st[i].average <=6.0)
  121.         {
  122.             cout << "\n FN:" << st[i].fn << "\t Name:" << st[i].name << "\t Average:" << st[i].average << "\t Profesion: " << st[i].prof;
  123.         }
  124.     }
  125.     fp.close();
  126. }
  127.  
  128. void output_search(student st[])
  129. {
  130.     int i, n;
  131.     long pos;
  132.     student s;
  133.     fp.open("st.dat", ios::binary | ios::in);
  134.     if (fp.fail()){ cout << "\n Error"; exit(1); }
  135.     fp.seekg(0L, ios::end);
  136.     pos = fp.tellg();
  137.     fp.seekg(0L, ios::beg);
  138.     n = pos / (sizeof(student));
  139.     for (i = 0; i<n; i++)
  140.     {
  141.         fp.read((char*)&s, sizeof(student));
  142.         st[i] = s;
  143.     }
  144.     fp.close();
  145.     int fn1;
  146.     cout << "\n Search for student with FN : ";
  147.     cin >> fn1;
  148.  
  149.     for (i = 0; i<n; i++)
  150.     if (st[i].fn == fn1)
  151.         cout << "\n FN:" << st[i].fn << "\t Name:" << st[i].name << "\t Average:" << st[i].average << "\t Profesion: " << st[i].prof;
  152. }
  153. void main(){
  154.     student st[30];
  155.     int ch;
  156.     do{
  157.         cout << "\n 1.Create file";
  158.         cout << "\n 2.Append new one ";
  159.         cout << "\n 3.List";
  160.         cout << "\n 4.Exellent students";
  161.         cout << "\n 5.Search on fn";
  162.         cout << "\n 6.Exit";
  163.         cout << "\n Your choice: ";
  164.         cin >> ch;
  165.         switch (ch)
  166.         {
  167.         case 1:input(st); break;
  168.         case 2:append(); break;
  169.         case 3:output(st); break;
  170.         case 4:output_excel(st); break;
  171.         case 5:output_search(st); break;
  172.         }
  173.     } while (ch != 6);
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement