Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <string.h>
  6. struct Litr
  7. {
  8.     char _FIO[100];
  9.     int _yearb;
  10.     int _yeard;
  11.     char _genre[40];
  12.  
  13. }ltr;
  14.  
  15.  
  16.  
  17.  
  18. void add(Litr ltr)
  19. {
  20.     FILE *f;
  21.     fopen_s(&f, "Literary elite.bin", "ab");
  22.  
  23.     printf_s("%s", "\nEnter Full name\n");
  24.     getchar();
  25.     gets_s(ltr._FIO);
  26.  
  27.     printf_s("%s", "\nEnter date of Birth\n");
  28.     scanf("%d", &ltr._yearb);
  29.  
  30.     printf_s("%s", "\nEnter date of Death\n");
  31.     scanf("%d", &ltr._yeard);
  32.  
  33.     printf_s("%s", "\nEnter Character of creation\n");
  34.     getchar();
  35.     gets_s(ltr._genre);
  36.  
  37.  
  38.     fwrite(&ltr, sizeof(ltr), 1, f);
  39.     fclose(f);
  40. }
  41. void view(Litr ltr)
  42. {
  43.     FILE *f;
  44.     fopen_s(&f, "Literary elite.bin", "rb");
  45.     if (!f)
  46.     {
  47.         system("cls");
  48.         printf_s("Empty list! Add some information first\n");
  49.         system("pause");
  50.     }
  51.     else
  52.     {
  53.         int k = 0;
  54.         Litr poet;
  55.         printf("  __________________________________________________\n");
  56.         printf(" | Full name             | Years of life | Genre    |\n");
  57.         printf(" ---------------------------------------------------\n");
  58.         while (fread(&poet, sizeof(poet), 1, f))
  59.         {
  60.             printf(" %d %-24s %d - %d    %-20s\n", k, poet._FIO, poet._yearb, poet._yeard, poet._genre);
  61.             k++;
  62.         }
  63.         fclose(f);
  64.         system("pause");
  65.     }
  66. }
  67. void sort(Litr ltr)
  68. {
  69.     printf("Start sorting > ");
  70.     FILE * f;
  71.     FILE *f1;
  72.  
  73.     fopen_s(&f, "Literary elite.bin", "rb");
  74.     fopen_s(&f1, "1.bin", "wb");
  75.  
  76.     int k = 0;
  77.     Litr _Poet;
  78.     if (f == NULL)
  79.     {
  80.         printf("No information\n");
  81.         system("pause");
  82.     }
  83.     else
  84.     {
  85.         while (fread(&_Poet, sizeof(_Poet), 1, f)) k++;
  86.         fseek(f, 0, SEEK_SET);
  87.         struct  Litr *poet;
  88.  
  89.         poet = (struct Litr*)malloc(k * sizeof(struct Litr));
  90.         k = 0;
  91.         while (fread(&poet[k], sizeof(poet[k]), 1, f)) ++k;
  92.  
  93.         Litr buf;
  94.         for (int i = 0; i < k; i++)
  95.             for (int j = 0; j < k; j++)
  96.             {
  97.  
  98.                 if (_stricmp(poet[i]._FIO, poet[j]._FIO) < 0)
  99.                 {
  100.                     buf = poet[i];
  101.                     poet[i] = poet[j];
  102.                     poet[j] = buf;
  103.                 }
  104.  
  105.             }
  106.         for (int i = 0; i < k; i++)
  107.  
  108.             fwrite(&poet[i], sizeof(poet[i]), 1, f1);
  109.  
  110.         free(poet);
  111.         fclose(f);
  112.         fclose(f1);
  113.         remove("Literary elite.bin");
  114.         char oldfilename[] = "1.bin";
  115.         char newfilename[] = "Literary elite.bin";
  116.         rename(oldfilename, newfilename);
  117.  
  118.         printf("complete\n");
  119.    
  120.     view(_Poet);
  121.     }
  122.  
  123. }
  124. void find(Litr ltr)
  125. {
  126.     FILE *f;
  127.     fopen_s(&f, "Literary elite.bin", "rb");
  128.     if (!f)
  129.     {
  130.         system("cls");
  131.         printf_s("Empty list! Add some information first\n");
  132.         system("pause");
  133.        
  134.     }
  135.     else
  136.     {
  137.     char chr[40];
  138.     int lowe, highe, counter=0;
  139.  
  140.     printf("Input searching Character of creation | ");
  141.     scanf("%s", &chr);
  142.     printf("\tInput lower edge of year:     | ");
  143.     scanf("%d", &lowe);
  144.     printf("\tInput higher edge of year:    | ");
  145.     scanf("%d", &highe);
  146.  
  147.     printf("________________________________________________________________\n");
  148.  
  149.    
  150.  
  151.    
  152.         Litr poet;
  153.         while (fread(&poet, sizeof(poet), 1, f))
  154.         {
  155.  
  156.             if ((poet._yearb > lowe) && (poet._yeard < highe) && (_stricmp(poet._genre, chr) == 0))
  157.             {
  158.                 printf("    %-24s %d - %d    %-20s\n", poet._FIO, poet._yearb, poet._yeard, poet._genre);
  159.                 counter++;
  160.             }
  161.         }
  162.         if (counter == 0) printf("No match found\n");
  163.         else printf("Searching complete with %d results\n", counter);
  164.         counter = 0;
  165.         fclose(f);
  166.         system("pause");
  167.     }
  168.  
  169. }
  170. void main()
  171. {
  172.     int key = 0;
  173.     do
  174.     {
  175.         printf_s("%s", "1. Add writer\n2. View list of writers\n3. Sort list\n4. Find writer\n0. Exit\n");
  176.         scanf_s("%d", &key);
  177.         switch (key)
  178.         {
  179.         case 1:system("cls"); add(ltr); break;
  180.         case 2:system("cls"); view(ltr);    break;
  181.         case 3:system("cls"); sort(ltr);    break;
  182.         case 4:system("cls"); find(ltr);    break;
  183.         case 0:exit(1);
  184.         default:
  185.  
  186.             system("cls");
  187.             printf("Command not found!\n");
  188.             system("pause");
  189.             continue;
  190.  
  191.         }
  192.  
  193.         system("cls");
  194.     } while (key != 0);
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement