Advertisement
palenda21

9B

Mar 6th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <math.h>
  7.  
  8. using namespace std;
  9. FILE *fl;
  10. FILE *ft;
  11.  
  12. struct worker
  13. {
  14.     char surname[50];
  15.     int birthdate;
  16.     int workdate;
  17. } *spisok, rabotnik;
  18. char fname[20];
  19.  
  20. void vvod();    //Ввести список
  21. void chit();    //Прочитать список
  22. void reze();    //Вывести результат на экран
  23. void rezf();    //Вывести результат в файл
  24. int menu();    //Меню
  25. char * fnam();    //Ввод имени файла
  26.  
  27.  
  28. int main()
  29. {
  30.     while (true)
  31.     {
  32. switch (menu())
  33. {
  34. case 1: vvod(); break;
  35. case 2: chit(); break;
  36. case 3: reze(); break;
  37. case 4: rezf(); break;
  38. case 5: return 0;
  39. default: cout << "Choose correct!" << endl;
  40. }
  41.          cout << "Press any key" << endl;
  42.     }
  43. }
  44.  
  45. int menu()
  46. {
  47.     cout << "Choose: " << endl;
  48.     cout << "1.Vvesti spisok" << endl;
  49.     cout << "2.Chitat' spisok" << endl;
  50.     cout << "3.Rez na ekran" << endl;
  51.     cout << "4.Rez v file" << endl;
  52.     cout << "5.Exit" << endl;
  53.     int x;
  54.     cin >> x;
  55.     return x;
  56. }
  57.  
  58. char * nnf()
  59. {
  60.     if (strlen(fname))
  61.         return fname;
  62.     cout << "Enter the name of file: " << endl;
  63.     cin >> fname;
  64.     return fname;
  65. }
  66.  
  67. void vvod()
  68. {
  69.     if ((fl = fopen(nnf(), "ab")) == NULL)
  70.          if ((fl = fopen(nnf() , "wb")) == NULL)
  71.          {
  72.              cout << "File is not created" << endl;
  73.              return;
  74.          }
  75.     int n;
  76.     cout << "Enter amount of workers: ";
  77.     cin >> n;
  78.     spisok = new worker[n];
  79.     for (int i = 0; i < n; i++)
  80.     {
  81.         cout << "Enter the surname of worker:  ";
  82.         cin >> rabotnik.surname;
  83.         cout << "Enter the birthdate of worker: ";
  84.         cin >> rabotnik.birthdate;
  85.         cout << "Enter the date of first work: ";
  86.         cin >> rabotnik.workdate;
  87.         cout << endl;
  88.     }
  89.      fclose(fl);
  90.     }
  91.  
  92. void chit()
  93. {
  94.          if ((fl = fopen(nnf(), "rb+")) == NULL)
  95.          {
  96.              cout << "File wasn't read" << endl;
  97.          }
  98.          else cout << "File was readed" << endl;
  99.      
  100.          int n = 0;
  101.      
  102.          while (fread(&spisok, sizeof(spisok), 1, fl) == 1) //find number of structeres in file
  103.          {
  104.              n++;
  105.          }
  106.          rewind(fl);
  107.      
  108.          for(int i=0; i<n ; i++)
  109.          {
  110.              fread(&rabotnik, sizeof(spisok), 1, fl);
  111.              cout << "Enter the surname of worker:  " << rabotnik.surname << endl;
  112.              cout << "Enter the birthdate of worker: " << rabotnik.birthdate << endl;
  113.              cout << "Enter the date of first work: " << rabotnik.workdate << endl;
  114.          }
  115.        fclose(fl);
  116. }
  117.  
  118. void reze()
  119. {
  120.        if ((fl = fopen(nnf(), "rb+")) == NULL)
  121.  {
  122.        cout << "File wasn't read" << endl;
  123.  }
  124.        else cout << "File was readed" << endl;
  125.        rewind(fl);
  126.     int n;
  127.     cout << "Enter amount of workers: ";
  128.     cin >> n;
  129.     spisok = new worker[n];
  130.     for (int i = 0; i < n; i++)
  131.     {
  132.         fread (&spisok, sizeof(spisok), 1, fl);
  133.         if (spisok[i].birthdate < 1980)
  134.     {
  135.         cout << "Worker that is not earlier than 1980: " << spisok[i].surname << endl;
  136.         cout << "His birth date: " << spisok[i].birthdate << endl;
  137.         cout << "His date of first work: " << spisok[i].workdate;
  138.         cout << endl;
  139.     }
  140.        else cout << "> 1980";
  141.     }
  142.    
  143.     delete[] spisok;
  144.     fclose(fl);
  145. }
  146.  
  147. void rezf()
  148. {
  149.     char fnamet[20];
  150.     cout << "Enter the name of file: ";
  151.     cin >> fnamet;
  152.    
  153.     if ((ft = fopen(fnamet, "w")) == NULL)
  154.     {
  155.           cout << "File wasn't read" << endl;
  156.     }
  157.           else cout << "File was readed" << endl;
  158.           rewind(ft);
  159.     if ((fl = fopen(nnf(), "rb+")) == NULL)
  160.     {
  161.           cout << "File wasn't read" << endl;
  162.     }
  163.           else cout << "File was readed" << endl;
  164.           rewind(fl);
  165.     int n;
  166.        cout << "Enter amount of workers: ";
  167.        cin >> n;
  168.        spisok = new worker[n];
  169.        for (int i = 0; i < n; i++)
  170.        {
  171.            fread(&spisok, sizeof(spisok), 1, fl);
  172.            if (spisok[i].birthdate < 1980)
  173.            {
  174.                cout << "Worker that is not earlier than 1980: " << spisok[i].surname << endl;
  175.                cout << "His birth date: " << spisok[i].birthdate << endl;
  176.                cout << "His date of first work: " << spisok[i].workdate;
  177.                cout << endl;
  178.            }
  179.               else cout << "> 1980";
  180.            }
  181.     fclose(ft);
  182.     fclose(fl);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement