Advertisement
Argintos

вопрос

Apr 22nd, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. const int sizeRating = 5;
  7. char filePath[100];
  8. bool firstRun = true;
  9. int option;
  10.  
  11. struct Stud
  12. {
  13.     char name[20];
  14.     int groupId;
  15.     int rating[sizeRating];
  16. };
  17.  
  18. void menu (Stud* groupMenu, int &sizeGroup);
  19. void create(Stud* groupCreate, int &sizeGroup);
  20. void add(Stud* groupWrite, int &sizeGroup);
  21. void read(Stud* groupRead, int &sizeGroup);
  22.  
  23. int main ()
  24. {
  25.     int sizeGroup = 2;
  26.     Stud* group = new Stud[sizeGroup];
  27.  
  28.     cout << "1. Create new database" << endl;
  29.     cout << "2. Complete existing" << endl;
  30.     cout << "3. Read existing" << endl;
  31.  
  32.     cout << endl;
  33.     while (option <= 0 || option > 3)
  34.     {
  35.         cout << "Select an action 1 - 3: ";
  36.         cin >> option;
  37.     }
  38.  
  39.     menu(group, sizeGroup);
  40.  
  41.     return 0;
  42. }
  43.  
  44. void menu (Stud* groupMenu, int &sizeGroup)
  45. {
  46.     if (!firstRun)
  47.     {
  48.         option = 0;
  49.         cout << "1. Create another database" << endl;
  50.         cout << "2. Complete existing" << endl;
  51.         cout << "3. Read existing" << endl;
  52.         cout << "4. Exit" << endl;
  53.     }
  54.  
  55.     cout << endl;
  56.     while (option <= 0 || option > 4)
  57.     {
  58.         cout << "Select an action 1 - 4: ";
  59.         cin >> option;
  60.     }
  61.  
  62.     switch (option)
  63.     {
  64.     case 1:
  65.         create(groupMenu, sizeGroup);
  66.         break;
  67.     case 2:
  68.         add(groupMenu, sizeGroup);
  69.         break;
  70.     case 3:
  71.         read(groupMenu, sizeGroup);
  72.         break;
  73.     case 4:
  74.         break;
  75.     }
  76. }
  77.  
  78. void create(Stud* groupCreate, int &sizeGroup)
  79. {
  80.     cout << endl;
  81.     cout << "Specify a destination and filename:" << endl;
  82.     cin >> filePath;
  83.     cout << "Students to create: ";
  84.     cin >> sizeGroup;
  85.  
  86.     FILE* fileCreate = fopen(filePath, "w+b");
  87.  
  88.     cout << endl;
  89.     for (int i = 0; i < sizeGroup; i++)
  90.     {
  91.         cout << "Student " << i + 1 << endl;
  92.         cout << "Name: ";
  93.         cin >> groupCreate[i].name;
  94.         cout << "Group Id: ";
  95.         cin >> groupCreate[i].groupId;
  96.         cout << "Rating: " << endl;
  97.         for (int j = 0; j < sizeRating; j++)
  98.         {
  99.             cout << j + 1 << ". ";
  100.             cin >> groupCreate[i].rating[j];
  101.         }
  102.         cout << endl;
  103.  
  104.     }
  105.  
  106.     fwrite(groupCreate, sizeof(Stud), sizeGroup, fileCreate);
  107.  
  108.     fclose(fileCreate);
  109.     firstRun = false;
  110.     menu(groupCreate, sizeGroup);
  111. }
  112.  
  113. void add(Stud* groupWrite, int &sizeGroup)
  114. {
  115.     int addStud = 0;
  116.  
  117.     cout << endl;
  118.     cout << "Specify a destination and filename:" << endl;
  119.     cin >> filePath;
  120.     cout << "Students to add: ";
  121.     cin >> addStud;
  122.  
  123.     FILE* fileAdd = fopen(filePath, "a+b");
  124.  
  125.     cout << endl;
  126.     for (int i = 0; i < addStud; i++)
  127.     {
  128.         cout << "Student " << i + 1 << endl;
  129.         cout << "Name: ";
  130.         cin >> groupWrite[i].name;
  131.         cout << "Group Id: ";
  132.         cin >> groupWrite[i].groupId;
  133.         cout << "Rating: " << endl;
  134.         for (int j = 0; j < sizeRating; j++)
  135.         {
  136.             cout << j + 1 << ". ";
  137.             cin >> groupWrite[i].rating[j];
  138.         }
  139.         cout << endl;
  140.     }
  141.  
  142.     fwrite(groupWrite, sizeof(Stud), addStud, fileAdd);
  143.  
  144.     fclose(fileAdd);
  145.     firstRun = false;
  146.     menu(groupWrite, sizeGroup);
  147. }
  148.  
  149. void read(Stud* groupRead, int &sizeGroup)
  150. {
  151.     cout << endl;
  152.     cout << "Specify a destination and filename:" << endl;
  153.     cin >> filePath;
  154.  
  155.     FILE* fileRead = fopen(filePath, "r+b");
  156.     int i = 0;
  157.     while (fread(&groupRead[i], sizeof(Stud), 1, fileRead))
  158.     {
  159.         cout << endl;
  160.         cout << "Student: " << i + 1 << endl;
  161.         cout << "Name: "<< groupRead[i].name << endl;
  162.         cout << "Group ID: " << groupRead[i].groupId << endl;
  163.         cout << "Rating: ";
  164.         for (int j = 0; j < sizeRating; j++)
  165.         {
  166.             cout << groupRead[i].rating[j] << " ";
  167.         }
  168.         cout << endl;
  169.         i++;
  170.     }
  171.     cout << endl;
  172.    
  173.     fclose(fileRead);
  174.     firstRun = false;
  175.     menu(groupRead, sizeGroup);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement