Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5. char fname[20]="";
  6.  
  7. int menu();
  8. char * fileName();
  9. void createFile();
  10. void writeData();
  11. void readFile();
  12. void displayFile();
  13. void convertFile();
  14. void quickSort();
  15. void selectionSort();
  16. void sortFile();
  17. void searchData();
  18.  
  19. int main(){
  20.     int i=1;
  21.     do {
  22.         switch(menu()){
  23.             case 1: createFile();break;
  24.             case 2: writeData();break;
  25.             case 3: readFile();break;
  26.             case 4: displayFile();break;
  27.             case 5: convertFile();break;
  28.             case 6: sortFile();break;
  29.             case 7: searchData();break;
  30.             case 0: i--;break;
  31.             default : std::cout << "Choose right action!";
  32.         }
  33.     } while (i>0);
  34.     return 0;
  35. }
  36.  
  37. int menu(){
  38.     int i;
  39.     std::cout << "1. Create binary file" << std::endl;
  40.     std::cout << "2. Write data at file" << std::endl;
  41.     std::cout << "3. Open and read data from file" << std::endl;
  42.     std::cout << "4. Display file data" << std::endl;
  43.     std::cout << "5. Write data at txt file" << std::endl;
  44.     std::cout << "6. Sort data and display" << std::endl;
  45.     std::cout << "7. Search data at file" << std::endl;
  46.     std::cout << "0. Close programm" << std::endl;
  47.     std::cout << "Choose action: ";
  48.     std::cin >> i;
  49.     return i;
  50. }
  51.  
  52. char * fileName(){
  53.     std::cout << "Choose binary file name: ";
  54.     std::cin >> fname;
  55.     return fname;
  56. }
  57.  
  58. void createFile(){
  59.     FILE *fl;
  60.     if((fl = fopen(fileName(), "wb+")) == NULL) {
  61.         std::cout << "Error when create binary file" << std::endl;
  62.     } else {
  63.         std::cout << "~Completed" << std::endl;
  64.     }
  65. }
  66.  
  67. void writeData(){
  68.     FILE *fl;
  69.     struct tovari
  70.     {
  71.     char name[100];
  72.     int quantity;
  73.     int price;  
  74.     } *spisok, staff;
  75.  
  76.     if((fl = fopen(fileName(), "rb+")) == NULL) {
  77.         std::cout << "~File isn't exist" << std::endl;
  78.     } else {
  79.         char ch;
  80.         do {
  81.             std::cout << "Enter name: ";
  82.             std::cin >> staff.name;
  83.             std::cout << "Enter quantity: ";
  84.             std::cin >> staff.quantity;
  85.             std::cout << "Enter price: ";
  86.             std::cin >> staff.price;
  87.             fwrite (&staff, sizeof(tovari), 1, fl);
  88.             std::cout << std::endl;
  89.             std::cout << "Do you want to write more data (y or n)? ";
  90.             std::cin >> ch;
  91.             std::cout << std::endl;
  92.         } while (ch=='y');
  93.         fclose(fl);
  94.     }
  95. }
  96.  
  97. void readFile(){
  98.     struct tovari
  99.     {
  100.     char name[100];
  101.     int quantity;
  102.     int price;
  103.     } *spisok, staff;
  104.  
  105.     FILE *fl;
  106.     if((fl = fopen(fileName(), "rb")) == NULL){
  107.         std::cout << "~File isn't exist" << std::endl;
  108.     } else {
  109.         int n=filelength(fileno(fl))/sizeof(tovari);
  110.         spisok = new tovari[n];
  111.         fread(spisok,sizeof(tovari),n,fl);
  112.         std::cout << std::endl;
  113.         for(int i=0;i<n;i++){
  114.             std::cout << spisok[i].name << " - price: " << spisok[i].price << " - quantity: " << spisok[i].quantity << std::endl;
  115.         }
  116.         delete []spisok;
  117.     }
  118.     std::cout << std::endl;
  119.     fclose(fl);
  120. }
  121.  
  122. void displayFile(){
  123.     struct tovari
  124. {
  125.     char name[100];
  126.     int quantity;
  127.     int price;
  128. } *spisok, staff;
  129.  
  130.     FILE *fl;
  131.     if((fl = fopen(fileName(), "rb")) == NULL) {
  132.         std::cout << "~File isn't exist" << std::endl;
  133.     } else {
  134.         int n=filelength(fileno(fl))/sizeof(tovari);
  135.         for(int i=0;i<n;i++){
  136.             fread(&staff, sizeof(tovari), 1, fl);
  137.             if(staff.quantity < 100 && staff.quantity > 10){
  138.                 std::cout << staff.name << " - " << staff.quantity << " - " << staff.price << std::endl;
  139.             }
  140.         }
  141.     }
  142.     fclose(fl);
  143. }
  144.  
  145. void convertFile(){
  146.     struct tovari
  147. {
  148.     char name[100];
  149.     int quantity;
  150.     int price;
  151. } *spisok, staff;
  152.  
  153.     FILE *fl;
  154.     char txtName[20];
  155.     FILE *fl1;
  156.     std::cout << "Choose txt file name: ";
  157.     std::cin >> txtName;
  158.    
  159.     if((fl1 = fopen(txtName, "w")) == NULL){
  160.         std::cout << "Error when create txt file" << std::endl;
  161.     }
  162.     if((fl = fopen(fileName(),"rb")) == NULL){
  163.         std::cout << "~File isn't exist" << std::endl;
  164.     } else {
  165.         int n=filelength(fileno(fl))/sizeof(tovari);
  166.         for(int i=0;i<n;i++){
  167.             fread(&spisok,sizeof(tovari),1,fl);
  168.             if(staff.quantity < 100 && staff.quantity > 10){
  169.                 fprintf(fl1,"%s, quantity: %d\n",staff.name,staff.quantity);
  170.             }
  171.             fclose(fl1);
  172.             fclose(fl);
  173.         }
  174.     }
  175. }
  176.  
  177. void quickSort(){
  178.    
  179. }
  180.  
  181. void selectionSort(){
  182.    
  183. }
  184.  
  185. void sortFile(){
  186.     struct tovari
  187.     {
  188.         char name[100];
  189.         int quantity;
  190.         int price;
  191.     } *spisok, staff;
  192.     struct Functions
  193.     {
  194.         void qsortRecursive(tovari *mas, int size) {
  195.             //Указатели в начало и в конец массива
  196.             int i = 0;
  197.             int j = size - 1;
  198.  
  199.             //Центральный элемент массива
  200.             tovari mid = mas[size / 2];
  201.  
  202.             //Делим массив
  203.             do {
  204.                 //Пробегаем элементы, ищем те, которые нужно перекинуть в другую часть
  205.                 //В левой части массива пропускаем(оставляем на месте) элементы, которые меньше центрального
  206.                 while (mas[i].price < mid.price) {
  207.                     i++;
  208.                 }
  209.                 //В правой части пропускаем элементы, которые больше центрального
  210.                 while (mas[j].price > mid.price) {
  211.                     j--;
  212.                 }
  213.  
  214.                 //Меняем элементы местами
  215.                 if (i <= j) {
  216.                     tovari tmp = mas[i];
  217.                     mas[i] = mas[j];
  218.                     mas[j] = tmp;
  219.  
  220.                     i++;
  221.                     j--;
  222.                 }
  223.             } while (i <= j);
  224.  
  225.  
  226.             //Рекурсивные вызовы, если осталось, что сортировать
  227.             if (j > 0) {
  228.                 //"Левый кусок"
  229.                 qsortRecursive(mas, j + 1);
  230.             }
  231.             if (i < size) {
  232.                 //"Првый кусок"
  233.                 qsortRecursive(&mas[i], size - i);
  234.             }
  235.         }
  236.         void choicesSort(tovari* arrayPtr, int length_array) // сортировка выбором
  237.         {
  238.             for (int repeat_counter = 0; repeat_counter < length_array; repeat_counter++)
  239.             {
  240.                 tovari temp = arrayPtr[0]; // временная переменная для хранения значения перестановки
  241.                 for (int element_counter = repeat_counter + 1; element_counter < length_array; element_counter++)
  242.                 {
  243.                     if (arrayPtr[repeat_counter].price > arrayPtr[element_counter].price)
  244.                     {
  245.                         temp = arrayPtr[repeat_counter];
  246.                         arrayPtr[repeat_counter] = arrayPtr[element_counter];
  247.                         arrayPtr[element_counter] = temp;
  248.                     }
  249.                 }
  250.             }
  251.         }
  252.     } func;
  253.     int sortType;
  254.     std::cout << "Choose sort type (1- QuickSort, 2- Selection sort):  ";
  255.     std::cin >> sortType;
  256.     if (sortType==1){
  257.         func.qsortRecursive(tovari *mas, int size);
  258.     }
  259.     if (sortType==2){
  260.         func.choicesSort(tovari* arrayPtr, int length_array)
  261.     } else {
  262.         std::cout << "~No sort type for this number" << std::endl;;
  263.     }
  264. }
  265.  
  266. void searchData(){
  267. struct function
  268.     {
  269.         int Search_Binary(tovari array[], int left, int right, int key)
  270.         {
  271.             int midd = 0;
  272.             while (1)
  273.             {
  274.                 midd = (left + right) / 2;
  275.  
  276.                 if (key < array[midd].price)       // если искомое меньше значения в ячейке
  277.                     right = midd - 1;      // смещаем правую границу поиска
  278.                 else if (key > array[midd].price)  // если искомое больше значения в ячейке
  279.                     left = midd + 1;       // смещаем левую границу поиска
  280.                 else                       // иначе (значения равны)
  281.                     return midd;           // функция возвращает индекс ячейки
  282.  
  283.                 if (left > right)          // если границы сомкнулись
  284.                     return -1;
  285.             }
  286.         }
  287.     };
  288.     struct tovari
  289.         {
  290.         char name[100];
  291.         int quantity;
  292.         int price;
  293.         } *spisok, staff;
  294.     FILE *fl;
  295.     int searchType,varPrice,counter=0;
  296.     std::cout << "Choose search type (1-Linear search , 2- Binary search): ";
  297.     std::cin >> searchType;
  298.     std::cout << "Set search price: ";
  299.     std::cin >> varPrice;
  300.     if (searchType==1){
  301.         if((fl = fopen(fileName(), "rb")) == NULL) {
  302.             std::cout << "~File isn't exist" << std::endl;
  303.         } else {
  304.             int n=filelength(fileno(fl))/sizeof(tovari);
  305.             for (int i=0;i<n;i++){
  306.                 fread(&staff, sizeof(tovari), 1, fl);
  307.                 if (staff.price == varPrice){
  308.                     std::cout << staff.name << " - " << staff.quantity << " - " << staff.price << std::endl;
  309.                     counter++;
  310.                 }
  311.             }
  312.             if (counter==0){
  313.                 std::cout << "Element with this price not found" << std::endl;
  314.             }
  315.         fclose(fl);
  316.     }
  317.     if (searchType==2){
  318.         if((fl = fopen(fileName(), "rb")) == NULL) {
  319.             std::cout << "~File isn't exist" << std::endl;
  320.         } else {
  321.             int n=filelength(fileno(fl))/sizeof(tovari);
  322.             std::cout << "Enter search price" <<std:: endl;
  323.             if (Search_Binary(&staff,0,(sizeof(&staff)/sizeof(staff)))==-1) //вот тут вызов функции
  324.                 if (counter == 0) {
  325.                     std::cout << "Element with this price not found" << std::endl;
  326.         }
  327.     fclose(fl);
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement