Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <io.h>
  5. #include <string.h>
  6. using namespace std;
  7. FILE *file;
  8. FILE *txt;
  9. struct book
  10. {
  11.     char name[40];
  12.     char surname[20];
  13.     int year;
  14.     int pages;
  15. };
  16. void createfile();
  17. void readfile();
  18. void output();
  19. void selectionsort();
  20. void quicksort();
  21. void linesearch(int);
  22. void binsearch(int);
  23. int menu();
  24. int main()
  25. {
  26.     int x;//пусть пользователь вводит сколько страниц ему надо (условие 1575)
  27.     while (true)
  28.     {
  29.         switch (menu())
  30.         {
  31.         case 1: createfile(); break;
  32.         case 2: readfile(); break;
  33.         case 3: output(); break;
  34.         case 4: selectionsort(); break;
  35.         case 5: quicksort(); break;
  36.         case 6: cout << "Enter number of pages" << endl; cin >> x; linesearch(x); break;
  37.         case 7: cout << "Enter number of pages" << endl; cin >> x; binsearch(x);  break;
  38.         case 8: return 0;
  39.         default: cout << "Incorrect choice!";
  40.         }
  41.         system("pause");
  42.         system("cls");
  43.     }
  44. }
  45.  
  46. void createfile() {
  47.     if ((file = fopen("binaryfile.dat", "ab")) == NULL)
  48.         if ((file = fopen("binaryfile.dat", "wb")) == NULL)
  49.         {
  50.             cout << "creation error!"<<endl;
  51.             return;
  52.         }
  53.     char act;
  54.     do {
  55.         book spisok;
  56.         cout << "Enter bookname: " << endl;
  57.         cin >> spisok.name;
  58.         cout << "Enter author surname: " << endl;
  59.         cin >> spisok.surname;
  60.         cout << "Enter year of edition : " << endl;
  61.         cin >> spisok.year;
  62.         cout << "Enter number of pages " << endl;
  63.         cin >> spisok.pages;
  64.         cout << endl;
  65.         fwrite(&spisok, sizeof(book), 1, file);
  66.         cout << "Anything else? y/n ";
  67.         cin >> act;
  68.         cout << endl;
  69.     } while (act == 'y');
  70.     fclose(file);
  71. }
  72.  
  73. void readfile() {
  74.     if ((file = fopen("binaryfile.dat", "rb")) == NULL)
  75.     {
  76.         cout << "opening error!"<<endl;
  77.         return;
  78.     }
  79.     book spisok;
  80.     while (fread(&spisok, sizeof(book), 1, file))
  81.     {
  82.         cout << spisok.name << "  " << spisok.surname
  83.             << "  " << spisok.year << "  " << spisok.pages <<endl;
  84.     }
  85.     fclose(file);
  86. }
  87.  
  88. void output() {
  89.     if ((file = fopen("binaryfile.dat", "rb")) == NULL)
  90.     {
  91.         cout << "opening error!"<<endl;
  92.         return;
  93.     }
  94.     int n = _filelength(_fileno(file)) / sizeof(book);
  95.  
  96.     book *list = new book[n];
  97.     fread(list, sizeof(book), n, file);
  98.     fclose(file);
  99.     book swap;
  100.     for (int i = 0; i < n - 1; i++)
  101.         for (int j = i + 1; j < n; j++)
  102.             if (list[i].year <= 1990 && list[j].year <= 1990 && strcmp(list[i].name, list[j].name) == 1)
  103.             {
  104.                 swap = list[i];
  105.                 list[i] = list[j];
  106.                 list[j] = swap;
  107.             }
  108.  
  109.     if ((txt = fopen("fileout.txt", "w")) == NULL)
  110.     {
  111.         cout << "Error at creation!"<<endl;
  112.         return;
  113.     }
  114.     for (int i = 0; i < n; i++)
  115.         if (list[i].year <= 1990)
  116.         {
  117.             cout << list[i].name << endl;
  118.             fprintf(txt, "%c ", list[i].name);
  119.         }
  120.     delete[] list;
  121.     fclose(txt);
  122. }
  123.  
  124. void selectionsort()
  125. {
  126.     if ((file = fopen("binaryfile.dat", "rb")) == NULL)
  127.     {
  128.         cout << "opening error!" << endl;
  129.         return;
  130.     }
  131.     int n = _filelength(_fileno(file)) / sizeof(book);
  132.     book *list = new book[n];
  133.     fread(list, sizeof(book), n, file);
  134.     fclose(file);
  135.     book t;
  136.     int imin, i, j;
  137.     for (i = 0; i<n - 1; i++)
  138.     {
  139.      imin = i;
  140.         for (j = i + 1; j<n; j++)
  141.             if (list[imin].pages > list[j].pages) imin = j;
  142.         if (imin != i)
  143.         {
  144.             t = list[imin];
  145.             list[imin] = list[i];
  146.             list[i] = t;
  147.         }
  148.     }
  149.     if ((txt = fopen("fileout.txt", "w")) == NULL)
  150.     {
  151.         cout << "Error at creation!" << endl;
  152.         return;
  153.     }
  154.     for (int i = 0; i < n; i++)
  155.     //  if (list[i].year <= 1990)
  156.         {
  157.             cout << list[i].name <<"  "<<list[i].pages<< endl;
  158.             fprintf(txt, "%c ", list[i].name);
  159.             fprintf(txt, "%c ", list[i].pages);
  160.         }
  161.     delete[] list;
  162.     fclose(txt);
  163. }
  164.  
  165. void quicksort()
  166. {
  167.     if ((file = fopen("binaryfile.dat", "rb")) == NULL)
  168.     {
  169.         cout << "opening error!" << endl;
  170.         return;
  171.     }
  172.     int n = _filelength(_fileno(file)) / sizeof(book);
  173.     book *list = new book[n];
  174.     fread(list, sizeof(book), n, file);
  175.     fclose(file);
  176.     struct {
  177.         int l;
  178.         int r;
  179.     } stack[20];
  180.  
  181.     int i, j, left, right, s = 0;
  182.     book t, x;
  183.     stack[s].l = 0; stack[s].r = n - 1;
  184.  
  185.     while (s != -1)
  186.     {
  187.         left = stack[s].l; right = stack[s].r;
  188.         s--;
  189.         while (left < right)
  190.         {
  191.             i = left; j = right; x = list[(left + right) / 2];
  192.             while (i <= j)
  193.             {
  194.                 while (list[i].pages < x.pages) i++;
  195.                 while (list[j].pages > x.pages) j--;
  196.                 if (i <= j) {
  197.                     t = list[i];
  198.                     list[i] = list[j];
  199.                     list[j] = t;
  200.                     i++;  j--;
  201.                 }
  202.             }
  203.  
  204.             if ((j - left) < (right - i)) // Выбор более короткой части
  205.             {
  206.                 if (i < right) { s++;  stack[s].l = i;  stack[s].r = right; }
  207.                 right = j;
  208.             }
  209.             else {
  210.                 if (left < j) { s++;  stack[s].l = left;  stack[s].r = j; }
  211.                 left = i;
  212.             }
  213.         }
  214.     }
  215.     if ((txt = fopen("fileout.txt", "w")) == NULL)
  216.     {
  217.         cout << "Error at creation!" << endl;
  218.         return;
  219.     }
  220.     for (int i = 0; i < n; i++)
  221.         //  if (list[i].year <= 1990)
  222.     {
  223.         cout << list[i].name << "  " << list[i].pages << endl;
  224.         fprintf(txt, "%c ", list[i].name);
  225.         fprintf(txt, "%c ", list[i].pages);
  226.     }
  227.     delete[] list;
  228.     fclose(txt);
  229. }
  230.  
  231. void linesearch(int x)
  232. {
  233.     if ((file = fopen("binaryfile.dat", "rb")) == NULL)
  234.     {
  235.         cout << "opening error!" << endl;
  236.         return;
  237.     }
  238.     int n = _filelength(_fileno(file)) / sizeof(book);
  239.     book *list = new book[n];
  240.     fread(list, sizeof(book), n, file);
  241.     fclose(file);
  242.     list[n + 1].pages = x;
  243.     int i = 0;
  244.     while (list[i].pages != x) i++;
  245.     if (i == n + 1) cout << "Required book not found" << endl;
  246.     else cout <<"Found! "<< list[i].name << "  " << list[i].pages << endl;
  247. }
  248.  
  249. void binsearch(int x)
  250. {
  251.     if ((file = fopen("binaryfile.dat", "rb")) == NULL)
  252.     {
  253.         cout << "opening error!" << endl;
  254.         return;
  255.     }
  256.     int n = _filelength(_fileno(file)) / sizeof(book);
  257.     book *list = new book[n];
  258.     fread(list, sizeof(book), n, file);
  259.     fclose(file);
  260.     int i = 0, j = n - 1, m;
  261.     while (i < j)
  262.     {
  263.         m = (i + j) / 2;
  264.         if (x > list[m].pages) i = m + 1; else j = m;
  265.     }
  266.     if (list[i].pages == x) cout << "Found! " << list[i].name << "  " << list[i].pages << endl;
  267.     else cout << "Required book not found" << endl;
  268. }
  269.  
  270. int menu() {
  271.     int i;
  272.     cout << "Choose action:"<<endl;
  273.     cout << "1)Enter data to file" << endl;
  274.     cout << "2)Read from file" << endl;
  275.     cout << "3)Output" << endl;
  276.     cout << "4)Sort pages with selectionsort"<<endl;
  277.     cout << "5)Sort pages with QuickSort" << endl;
  278.     cout << "6)Line searching the required book " << endl;
  279.     cout << "7)Binary searching the required book" << endl;
  280.     cout << "8)Exit" << endl;
  281.     cin >> i;
  282.     return i;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement