Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <io.h>
- char fname[20]="";
- int menu();
- char * fileName();
- void createFile();
- void writeData();
- void readFile();
- void displayFile();
- void convertFile();
- void quickSort();
- void selectionSort();
- void sortFile();
- void searchData();
- int main(){
- int i=1;
- do {
- switch(menu()){
- case 1: createFile();break;
- case 2: writeData();break;
- case 3: readFile();break;
- case 4: displayFile();break;
- case 5: convertFile();break;
- case 6: sortFile();break;
- case 7: searchData();break;
- case 0: i--;break;
- default : std::cout << "Choose right action!";
- }
- } while (i>0);
- return 0;
- }
- int menu(){
- int i;
- std::cout << "1. Create binary file" << std::endl;
- std::cout << "2. Write data at file" << std::endl;
- std::cout << "3. Open and read data from file" << std::endl;
- std::cout << "4. Display file data" << std::endl;
- std::cout << "5. Write data at txt file" << std::endl;
- std::cout << "6. Sort data and display" << std::endl;
- std::cout << "7. Search data at file" << std::endl;
- std::cout << "0. Close programm" << std::endl;
- std::cout << "Choose action: ";
- std::cin >> i;
- return i;
- }
- char * fileName(){
- std::cout << "Choose binary file name: ";
- std::cin >> fname;
- return fname;
- }
- void createFile(){
- FILE *fl;
- if((fl = fopen(fileName(), "wb+")) == NULL) {
- std::cout << "Error when create binary file" << std::endl;
- } else {
- std::cout << "~Completed" << std::endl;
- }
- }
- void writeData(){
- FILE *fl;
- struct tovari
- {
- char name[100];
- int quantity;
- int price;
- } *spisok, staff;
- if((fl = fopen(fileName(), "rb+")) == NULL) {
- std::cout << "~File isn't exist" << std::endl;
- } else {
- char ch;
- do {
- std::cout << "Enter name: ";
- std::cin >> staff.name;
- std::cout << "Enter quantity: ";
- std::cin >> staff.quantity;
- std::cout << "Enter price: ";
- std::cin >> staff.price;
- fwrite (&staff, sizeof(tovari), 1, fl);
- std::cout << std::endl;
- std::cout << "Do you want to write more data (y or n)? ";
- std::cin >> ch;
- std::cout << std::endl;
- } while (ch=='y');
- fclose(fl);
- }
- }
- void readFile(){
- struct tovari
- {
- char name[100];
- int quantity;
- int price;
- } *spisok, staff;
- FILE *fl;
- if((fl = fopen(fileName(), "rb")) == NULL){
- std::cout << "~File isn't exist" << std::endl;
- } else {
- int n=filelength(fileno(fl))/sizeof(tovari);
- spisok = new tovari[n];
- fread(spisok,sizeof(tovari),n,fl);
- std::cout << std::endl;
- for(int i=0;i<n;i++){
- std::cout << spisok[i].name << " - price: " << spisok[i].price << " - quantity: " << spisok[i].quantity << std::endl;
- }
- delete []spisok;
- }
- std::cout << std::endl;
- fclose(fl);
- }
- void displayFile(){
- struct tovari
- {
- char name[100];
- int quantity;
- int price;
- } *spisok, staff;
- FILE *fl;
- if((fl = fopen(fileName(), "rb")) == NULL) {
- std::cout << "~File isn't exist" << std::endl;
- } else {
- int n=filelength(fileno(fl))/sizeof(tovari);
- for(int i=0;i<n;i++){
- fread(&staff, sizeof(tovari), 1, fl);
- if(staff.quantity < 100 && staff.quantity > 10){
- std::cout << staff.name << " - " << staff.quantity << " - " << staff.price << std::endl;
- }
- }
- }
- fclose(fl);
- }
- void convertFile(){
- struct tovari
- {
- char name[100];
- int quantity;
- int price;
- } *spisok, staff;
- FILE *fl;
- char txtName[20];
- FILE *fl1;
- std::cout << "Choose txt file name: ";
- std::cin >> txtName;
- if((fl1 = fopen(txtName, "w")) == NULL){
- std::cout << "Error when create txt file" << std::endl;
- }
- if((fl = fopen(fileName(),"rb")) == NULL){
- std::cout << "~File isn't exist" << std::endl;
- } else {
- int n=filelength(fileno(fl))/sizeof(tovari);
- for(int i=0;i<n;i++){
- fread(&spisok,sizeof(tovari),1,fl);
- if(staff.quantity < 100 && staff.quantity > 10){
- fprintf(fl1,"%s, quantity: %d\n",staff.name,staff.quantity);
- }
- fclose(fl1);
- fclose(fl);
- }
- }
- }
- void quickSort(){
- }
- void selectionSort(){
- }
- void sortFile(){
- struct tovari
- {
- char name[100];
- int quantity;
- int price;
- } *spisok, staff;
- struct Functions
- {
- void qsortRecursive(tovari *mas, int size) {
- //Указатели в начало и в конец массива
- int i = 0;
- int j = size - 1;
- //Центральный элемент массива
- tovari mid = mas[size / 2];
- //Делим массив
- do {
- //Пробегаем элементы, ищем те, которые нужно перекинуть в другую часть
- //В левой части массива пропускаем(оставляем на месте) элементы, которые меньше центрального
- while (mas[i].price < mid.price) {
- i++;
- }
- //В правой части пропускаем элементы, которые больше центрального
- while (mas[j].price > mid.price) {
- j--;
- }
- //Меняем элементы местами
- if (i <= j) {
- tovari tmp = mas[i];
- mas[i] = mas[j];
- mas[j] = tmp;
- i++;
- j--;
- }
- } while (i <= j);
- //Рекурсивные вызовы, если осталось, что сортировать
- if (j > 0) {
- //"Левый кусок"
- qsortRecursive(mas, j + 1);
- }
- if (i < size) {
- //"Првый кусок"
- qsortRecursive(&mas[i], size - i);
- }
- }
- void choicesSort(tovari* arrayPtr, int length_array) // сортировка выбором
- {
- for (int repeat_counter = 0; repeat_counter < length_array; repeat_counter++)
- {
- tovari temp = arrayPtr[0]; // временная переменная для хранения значения перестановки
- for (int element_counter = repeat_counter + 1; element_counter < length_array; element_counter++)
- {
- if (arrayPtr[repeat_counter].price > arrayPtr[element_counter].price)
- {
- temp = arrayPtr[repeat_counter];
- arrayPtr[repeat_counter] = arrayPtr[element_counter];
- arrayPtr[element_counter] = temp;
- }
- }
- }
- }
- } func;
- int sortType;
- std::cout << "Choose sort type (1- QuickSort, 2- Selection sort): ";
- std::cin >> sortType;
- if (sortType==1){
- func.qsortRecursive(tovari *mas, int size);
- }
- if (sortType==2){
- func.choicesSort(tovari* arrayPtr, int length_array)
- } else {
- std::cout << "~No sort type for this number" << std::endl;;
- }
- }
- void searchData(){
- struct function
- {
- int Search_Binary(tovari array[], int left, int right, int key)
- {
- int midd = 0;
- while (1)
- {
- midd = (left + right) / 2;
- if (key < array[midd].price) // если искомое меньше значения в ячейке
- right = midd - 1; // смещаем правую границу поиска
- else if (key > array[midd].price) // если искомое больше значения в ячейке
- left = midd + 1; // смещаем левую границу поиска
- else // иначе (значения равны)
- return midd; // функция возвращает индекс ячейки
- if (left > right) // если границы сомкнулись
- return -1;
- }
- }
- };
- struct tovari
- {
- char name[100];
- int quantity;
- int price;
- } *spisok, staff;
- FILE *fl;
- int searchType,varPrice,counter=0;
- std::cout << "Choose search type (1-Linear search , 2- Binary search): ";
- std::cin >> searchType;
- std::cout << "Set search price: ";
- std::cin >> varPrice;
- if (searchType==1){
- if((fl = fopen(fileName(), "rb")) == NULL) {
- std::cout << "~File isn't exist" << std::endl;
- } else {
- int n=filelength(fileno(fl))/sizeof(tovari);
- for (int i=0;i<n;i++){
- fread(&staff, sizeof(tovari), 1, fl);
- if (staff.price == varPrice){
- std::cout << staff.name << " - " << staff.quantity << " - " << staff.price << std::endl;
- counter++;
- }
- }
- if (counter==0){
- std::cout << "Element with this price not found" << std::endl;
- }
- fclose(fl);
- }
- if (searchType==2){
- if((fl = fopen(fileName(), "rb")) == NULL) {
- std::cout << "~File isn't exist" << std::endl;
- } else {
- int n=filelength(fileno(fl))/sizeof(tovari);
- std::cout << "Enter search price" <<std:: endl;
- if (Search_Binary(&staff,0,(sizeof(&staff)/sizeof(staff)))==-1) //вот тут вызов функции
- if (counter == 0) {
- std::cout << "Element with this price not found" << std::endl;
- }
- fclose(fl);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement