Advertisement
ilyaaaaa

Untitled

Feb 25th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.45 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #include<iomanip>
  9.  
  10. using namespace std;
  11. FILE* fl;
  12.  
  13.  
  14. struct train_to_brest   //struct
  15. {
  16.     int time_to;
  17.     char place_to[50];
  18.     int available_places;
  19. }train,*tr;
  20.  
  21. int  menu_list_1();   // menu list 1
  22. int  menu_list_2();   // menu list 2
  23.  
  24. void creating_f();  // 1.create new file
  25. void filling_f();   // 2.filling FILE
  26. void reading_f();   // 3.file reading
  27. void conclusion_screen();  // 4.conclusion_screen
  28. void conclusion_file();    // 5.conclusion_file
  29.  
  30.  
  31. //second part of programm
  32. void taking_elements();   //6. taking elements from file
  33. void sort_shell(train_to_brest*, int);   //6.sorting by shell
  34. void quick_sort_bez_reqursii(train_to_brest* a, int n);   //7.quick sort  
  35. void search(train_to_brest *, int );   //8.serach element
  36.  
  37. int menu_for_search_and_sort(train_to_brest *,int );
  38.  
  39. int search_by_dv(train_to_brest*,int ,int );
  40. int search_by_lin(train_to_brest*,int, int);
  41.  
  42.  
  43. int main()
  44. {
  45.     while (true)
  46.     {
  47.         switch (menu_list_1())
  48.         {
  49.         case 1: creating_f(); break;
  50.         case 2: filling_f(); break;
  51.         case 3: reading_f(); break;
  52.         case 4: conclusion_screen(); break;
  53.         case 5: conclusion_file(); break;
  54.         case 6: taking_elements(); break; //taking elements for different searches and sorting
  55.                                         //functions for search and sort lie in this function !
  56.         case 7: return 0;
  57.         default: cout<<"Choose correct!" << endl;
  58.         }
  59.        
  60.     }
  61. }
  62.  
  63. int menu_list_1()
  64. {
  65.     cout << "Choose: " << endl;
  66.     cout << "1.create new file" << endl;
  67.     cout << "2.filling FILE" << endl;
  68.     cout << "3.file reading" << endl;
  69.     cout << "4.conclusion_screen" << endl;
  70.     cout << "5.conclusion_file" << endl;
  71.     cout << "6.taking_elements" << endl;
  72.  
  73.     cout << "7.exit" << endl;
  74.     int k; cin >> k;
  75.     return k;
  76. }
  77.  
  78. void creating_f()  //creatin file
  79. {
  80.    
  81.     if ((fl = fopen("struct.dat", "wb")) == NULL)
  82.     {
  83.         cout << "file didn't create" << endl;
  84.     }
  85.     else cout << "file created GOOD"<<endl;
  86.     fclose(fl);
  87. }
  88.  
  89. void filling_f()  //filling file
  90. {
  91.     if ((fl = fopen("struct.dat", "rb+")) == NULL)
  92.     {
  93.         cout << "file didn't write" << endl;
  94.     }
  95.     else cout << "file write GOOD";
  96.  
  97.     char ch[10];
  98.     int count = 0;
  99.     do
  100.     {
  101.         cout << "Enter time_to: " << endl; cin >> train.time_to;
  102.         cout << "Enter Place_to: " << endl; cin >> train.place_to;
  103.         cout << "Enter available_places: " << endl; cin >> train.available_places;
  104.  
  105.         fwrite(&train, sizeof(train_to_brest), 1, fl); //writing
  106.  
  107.         cout << "Do u want to continue? Yes / No " << endl;
  108.         cin >> ch;
  109.     } while (strcmp(ch, "yes") == 0);
  110.  
  111.     fclose(fl);
  112. }
  113.  
  114. void reading_f()
  115. {
  116.    
  117.     if ((fl = fopen("struct.dat", "rb+")) == NULL)
  118.     {
  119.         cout << "file didn't read" << endl;
  120.     }
  121.     else cout << "file readed GOOD" << endl;
  122.  
  123.     int n = 0;
  124.  
  125.     n = _filelength(_fileno(fl)) / sizeof(train_to_brest);
  126.  
  127.     rewind(fl);
  128.  
  129.     for(int i=0; i<n ; i++)
  130.     {
  131.         cout << "--------------------------------------------------"<<endl;
  132.         fread(&train, sizeof(train_to_brest), 1, fl);
  133.         cout << "PLace_to: " << train.place_to << endl;
  134.         cout << "Time_to: " << train.time_to << endl;
  135.         cout << "Available places: " << train.available_places << endl;
  136.         cout << "--------------------------------------------------" << endl;
  137.     }
  138.     fclose(fl);
  139. }
  140.  
  141. void conclusion_screen()  // screen
  142. {
  143.     if ((fl = fopen("struct.dat", "rb+")) == NULL)
  144.     {
  145.         cout << "file didn't read" << endl;
  146.     }
  147.     else cout << "file readed GOOD" << endl;
  148.     rewind(fl);
  149.  
  150.     int n = 0;
  151.  
  152.     n = _filelength(_fileno(fl)) / sizeof(train_to_brest);
  153.  
  154.     rewind(fl);
  155.     fread(&train, sizeof(train_to_brest), 1, fl);
  156.     int min = train.available_places;
  157.  
  158.     int count;
  159.     cout << endl << "Conclusion on SCREEN:" << endl;
  160.     cout << "Trains to brest: available_places from MIN to MAX" << endl;
  161.     train_to_brest k, k_i;
  162.     int count_mesto = 0;
  163.     bool p;
  164.     for (int i = 0; i < n; i++)
  165.     {
  166.         p = false;
  167.         fseek(fl, sizeof(train_to_brest) * i, 0);
  168.         fread(&train, sizeof(train_to_brest), 1, fl);
  169.  
  170.         k_i=train;
  171.         count = i;
  172.  
  173.         if (strcmp(train.place_to, "brest") == 0)
  174.         {
  175.             min = train.available_places;
  176.  
  177.             while (fread(&train, sizeof(train_to_brest), 1, fl) == 1)
  178.             {
  179.                 count++;
  180.                 if (train.available_places < min && (strcmp(train.place_to, "brest") == 0))
  181.                 {
  182.                     p=true;
  183.                     min = train.available_places;
  184.                     count_mesto = count;
  185.                 }
  186.             }
  187.             if (p == true)
  188.             {
  189.                 fseek(fl, sizeof(train_to_brest) * count_mesto, 0);
  190.                 fread(&train, sizeof(train_to_brest), 1, fl);
  191.                 fseek(fl, sizeof(train_to_brest) * count_mesto, 0);
  192.                 k = train;
  193.  
  194.                 fwrite(&k_i, sizeof(train_to_brest), 1, fl);
  195.  
  196.                 fseek(fl, sizeof(train_to_brest) * i, 0);
  197.                 fwrite(&k, sizeof(train_to_brest), 1, fl);
  198.             }
  199.         }
  200.     }
  201.     rewind(fl);
  202.     cout << endl << endl;
  203.     while (fread(&train, sizeof(train_to_brest), 1, fl) == 1)
  204.     {
  205.         if (strcmp(train.place_to, "brest") == 0)
  206.         {
  207.             cout << "--------------------------------------------------" << endl;
  208.             cout << "PLace_to: " << train.place_to << endl;
  209.             cout << "Time_to: " << train.time_to << endl;
  210.             cout << "Available places: " << train.available_places << endl;
  211.             cout << "--------------------------------------------------" << endl;
  212.         }
  213.     }
  214.     fclose(fl);
  215.     cout << endl;
  216.    
  217. }
  218.  
  219. void conclusion_file()
  220. {
  221.     if ((fl = fopen("struct.dat", "rb+")) == NULL)
  222.     {
  223.         cout << "file didn't read" << endl;
  224.     }
  225.     else cout << "file readed GOOD" << endl;
  226.     rewind(fl);
  227.     int n = 0;
  228.  
  229.     n = _filelength(_fileno(fl)) / sizeof(train_to_brest);
  230.  
  231.     rewind(fl);
  232.    
  233.     fread(&train, sizeof(train_to_brest), 1, fl);
  234.     int min = train.available_places;
  235.  
  236.     int count;
  237.     cout << endl << "Conclusion on SCREEN:" << endl;
  238.     cout << "Trains to brest: available_places from MIN to MAX" << endl;
  239.     train_to_brest k, k_i;
  240.     int count_mesto = 0;
  241.     bool p;
  242.     for (int i = 0; i < n; i++)
  243.     {
  244.         p = false;
  245.         fseek(fl, sizeof(train_to_brest) * i, 0);
  246.         fread(&train, sizeof(train_to_brest), 1, fl);
  247.  
  248.         k_i = train;
  249.         count = i;
  250.  
  251.         if (strcmp(train.place_to, "brest") == 0)
  252.         {
  253.             min = train.available_places;
  254.  
  255.             while (fread(&train, sizeof(train_to_brest), 1, fl) == 1)
  256.             {
  257.                 count++;
  258.                 if (train.available_places < min && (strcmp(train.place_to, "brest") == 0))
  259.                 {
  260.                     p = true;
  261.                     min = train.available_places;
  262.                     count_mesto = count;
  263.                 }
  264.             }
  265.             if (p == true)
  266.             {
  267.                 fseek(fl, sizeof(train_to_brest) * count_mesto, 0);
  268.                 fread(&train, sizeof(train_to_brest), 1, fl);
  269.                 fseek(fl, sizeof(train_to_brest) * count_mesto, 0);
  270.                 k = train;
  271.  
  272.                 fwrite(&k_i, sizeof(train_to_brest), 1, fl);
  273.  
  274.                 fseek(fl, sizeof(train_to_brest) * i, 0);
  275.                 fwrite(&k, sizeof(train_to_brest), 1, fl);
  276.             }
  277.         }
  278.     }
  279.     rewind(fl);
  280.     cout << endl << endl;
  281.     count = 0;
  282.  
  283.     for (int i = 0; i < n; i++)
  284.     {
  285.         fread(&train, sizeof(train_to_brest), 1, fl);
  286.  
  287.         if (strcmp(train.place_to, "brest") == 0)
  288.         {
  289.             cout << "--------------------------------------------------" << endl;
  290.             cout << "PLace_to: " << train.place_to << endl;
  291.             cout << "Time_to: " << train.time_to << endl;
  292.             cout << "Available places: " << train.available_places << endl << endl;
  293.             cout << "--------------------------------------------------" << endl;
  294.  
  295.             fseek(fl, 0, 2);
  296.             fwrite(&train, sizeof(train_to_brest), 1, fl);
  297.             fseek(fl, sizeof(train_to_brest) * (i + 1), 0);
  298.         }
  299.     }
  300.         fclose(fl);
  301.         cout << endl;
  302. }
  303.  
  304.  
  305. //SECOND PART OF PROGRAMM
  306.  
  307.  
  308.  
  309. void taking_elements()  //6. taking elements from file and creating arrays of structures
  310. {
  311.     if ((fl = fopen("struct.dat", "rb+")) == NULL)
  312.     {
  313.         cout << "file didn't read" << endl;
  314.     }
  315.     else cout << "file readed GOOD" << endl;
  316.     rewind(fl);
  317.     int n = 0;
  318.  
  319.     n = _filelength(_fileno(fl)) / sizeof(train_to_brest);
  320.  
  321.     train_to_brest *mass = new train_to_brest[n];
  322.     int i = 0;
  323.  
  324.     rewind(fl);
  325.  
  326.     while (fread(&train, sizeof(train_to_brest), 1, fl) == 1)
  327.     {
  328.         mass[i] = train;
  329.         i++;
  330.     }
  331.     cout << endl << endl;
  332.  
  333.     menu_for_search_and_sort(mass,n);
  334. }
  335.  
  336.  
  337.  
  338.  
  339. int menu_for_search_and_sort(train_to_brest *mass,int n)
  340. {
  341.     while (true)
  342.     {
  343.         switch (menu_list_2())
  344.         {
  345.         case 1: search(mass,n); break;
  346.         case 2: sort_shell(mass,n); break;
  347.         case 3: quick_sort_bez_reqursii(mass,n); break;
  348.        
  349.         case 4: return 0;
  350.         default: cout << "Choose correct!" << endl;
  351.         }
  352.        
  353.     }
  354. }
  355.  
  356. int menu_list_2()
  357. {
  358.     cout << "Choose: " << endl;
  359.     cout << "1.search" << endl;
  360.     cout << "2.sort_shell" << endl;
  361.     cout << "3.quick_sort_bez_reqursii" << endl;
  362.     cout << "4.exit" << endl;
  363.     int k; cin >> k;
  364.     return k;
  365. }
  366.  
  367.  
  368.  
  369.  
  370.  
  371. void search(train_to_brest *mass,int n)   //SEARCH ELEMENT
  372. {
  373.     int key;
  374.     cout << endl << "write your KEY:"<<endl;
  375.     cin >> key;
  376.     int i = 0; //place of structures in massiv
  377.  
  378.    
  379.    
  380.         int choose;
  381.         do
  382.         {
  383.             cout << "Choose variant of searching: " << endl << "1.-Lineinie s barierom " << endl << "2.-Dvoinoi" << endl;
  384.             cin >> choose;
  385.             switch (choose)
  386.             {
  387.             case 1:
  388.                 if (search_by_lin(mass, n, key) == -1)
  389.                     cout << "Element not found!";
  390.                 break;
  391.             case 2:
  392.                 if (search_by_dv(mass, n, key) == -1)
  393.                     cout << "Element not found!";
  394.                 break;
  395.  
  396.  
  397.             default: cout << "choose correct:" << endl << endl;
  398.             }
  399.         } while (choose < 1 || choose>3);
  400.        
  401. }
  402.  
  403.  int search_by_lin(train_to_brest *mass,int n,int key)  // SEARCH 1 with bar
  404.          {
  405.  
  406.              mass[n] = train;
  407.              mass[n].time_to = key;
  408.              int i = 0;
  409.              while (mass[i].time_to != key)
  410.              {
  411.                  i++;
  412.              }
  413.  
  414.              if (i == n)
  415.              {
  416.                  return -1;
  417.              }
  418.              else
  419.              {
  420.                  cout << "This Train is going at " << key << endl;
  421.                  cout << "It is going to " << mass[i].place_to << endl;
  422.                  return i;
  423.              }
  424.          }
  425.  
  426.  int search_by_dv(train_to_brest* mass,int n, int key)
  427.          {
  428.              cout << "First of all you should Choose 1 of the sorting for this search:" << endl;
  429.              cout << "1. - Sort by shell" << endl;
  430.              cout << "2. - Quick sort" << endl;
  431.              int k;
  432.            
  433.  
  434.             do
  435.             {
  436.                 cin >> k;
  437.                  switch (k)
  438.                  {
  439.                  case 1:sort_shell(mass, n); break;
  440.                  case 2: quick_sort_bez_reqursii(mass, n); break;
  441.                  default: cout << "Choose correct!" << endl;
  442.            
  443.                  }
  444.             } while (k <1 || k > 2);
  445.              
  446.  
  447.              int left = 0;
  448.              int right = n - 1;
  449.              int medium;
  450.  
  451.              while (left < right)
  452.              {
  453.                  medium = (left + right) / 2;
  454.                  if (mass[medium].time_to < key)
  455.                  {
  456.                      left = medium + 1;
  457.                  }
  458.                  else right = medium;
  459.              }
  460.              if (mass[left].time_to == key)
  461.              {
  462.                  cout << "This Train is going at " << key << endl;
  463.                  cout << "It is going to " << mass[left].place_to << endl;
  464.                  return left;
  465.              }
  466.              else return -1;
  467.  
  468.          }
  469.  
  470.  
  471.  void sort_shell(train_to_brest* mass, int n)  //  сортировка по шеллу
  472.          {
  473.              train_to_brest tmp;
  474.  
  475.              for (int i = 0; i < n; i++)  //old array
  476.              {
  477.                  cout<<setw(10) << mass[i].time_to << setw(10);
  478.              }
  479.              cout << endl;
  480.  
  481.              for (int step = n / 2; step > 0; step /= 2)
  482.              {
  483.                  for (int i = step; i < n; i++)
  484.                  {
  485.                      for (int j = i - step; j >= 0; j -= step)
  486.                      {
  487.                          if (mass[j].time_to > mass[j + step].time_to)
  488.                          {
  489.                              tmp = mass[j];
  490.                              mass[j] = mass[j + step];
  491.                              mass[j + step] = tmp;
  492.                          }
  493.                      }
  494.                  }
  495.              }
  496.  
  497.              for (int i = 0; i < n; i++)  //new array by sort
  498.              {
  499.                  cout << setw(10) << mass[i].time_to << setw(10);
  500.              }
  501.              cout << endl;
  502.          }
  503.  
  504.  void quick_sort_bez_reqursii(train_to_brest *a, int n)
  505.  {
  506.      for (int k = 0; k < n; k++)
  507.      {
  508.          cout << setw(10) << a[k].time_to << setw(10);;
  509.      }
  510.      cout << endl << endl;
  511.  
  512.      struct
  513.      {
  514.          int l;
  515.          int r;
  516.      } stack[20];
  517.  
  518.      int i, j, left, right, s = 0;
  519.      int t, pivot;
  520.      stack[s].l = 0; stack[s].r = n - 1;
  521.      while (s != -1)
  522.      {
  523.          left = stack[s].l;
  524.          right = stack[s].r;
  525.          s--;
  526.  
  527.  
  528.  
  529.          while (left < right)
  530.          {
  531.              i = left;
  532.              j = right;
  533.              pivot = a[(left + right) / 2].time_to;
  534.  
  535.              while (i <= j)
  536.              {
  537.                  while (a[i].time_to < pivot) i++;
  538.                  while (a[j].time_to > pivot) j--;
  539.  
  540.                  if (i <= j)
  541.                  {
  542.                      t = a[i].time_to;
  543.                      a[i].time_to = a[j].time_to;
  544.                      a[j].time_to = t;
  545.  
  546.                      i++;
  547.                      j--;
  548.                  }
  549.              }
  550.              if ((j - left) < (right - i)) // Выбор более короткой части
  551.              {
  552.                  if (i < right)
  553.                  {
  554.                      s++;
  555.                      stack[s].l = i;
  556.                      stack[s].r = right;
  557.                  }
  558.                  right = j;
  559.              }
  560.              else
  561.              {
  562.                  if (left < j)
  563.                  {
  564.                      s++;
  565.                      stack[s].l = left;
  566.                      stack[s].r = j;
  567.                  }
  568.                  left = i;
  569.              }
  570.          }
  571.      }
  572.  
  573.      for (int k = 0; k < n; k++)
  574.      {
  575.          cout << setw(10) << a[k].time_to << setw(10);;
  576.      }
  577.      cout << endl << endl;
  578.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement