Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <cstdio>
- #include <iostream>
- #include <io.h>
- #include <string.h>
- using namespace std;
- FILE *file;
- FILE *txt;
- struct book
- {
- char name[40];
- char surname[20];
- int year;
- int pages;
- };
- void createfile();
- void readfile();
- void output();
- void selectionsort();
- void quicksort();
- void linesearch(int);
- void binsearch(int);
- int menu();
- int main()
- {
- int x;//пусть пользователь вводит сколько страниц ему надо (условие 1575)
- while (true)
- {
- switch (menu())
- {
- case 1: createfile(); break;
- case 2: readfile(); break;
- case 3: output(); break;
- case 4: selectionsort(); break;
- case 5: quicksort(); break;
- case 6: cout << "Enter number of pages" << endl; cin >> x; linesearch(x); break;
- case 7: cout << "Enter number of pages" << endl; cin >> x; binsearch(x); break;
- case 8: return 0;
- default: cout << "Incorrect choice!";
- }
- system("pause");
- system("cls");
- }
- }
- void createfile() {
- if ((file = fopen("binaryfile.dat", "ab")) == NULL)
- if ((file = fopen("binaryfile.dat", "wb")) == NULL)
- {
- cout << "creation error!"<<endl;
- return;
- }
- char act;
- do {
- book spisok;
- cout << "Enter bookname: " << endl;
- cin >> spisok.name;
- cout << "Enter author surname: " << endl;
- cin >> spisok.surname;
- cout << "Enter year of edition : " << endl;
- cin >> spisok.year;
- cout << "Enter number of pages " << endl;
- cin >> spisok.pages;
- cout << endl;
- fwrite(&spisok, sizeof(book), 1, file);
- cout << "Anything else? y/n ";
- cin >> act;
- cout << endl;
- } while (act == 'y');
- fclose(file);
- }
- void readfile() {
- if ((file = fopen("binaryfile.dat", "rb")) == NULL)
- {
- cout << "opening error!"<<endl;
- return;
- }
- book spisok;
- while (fread(&spisok, sizeof(book), 1, file))
- {
- cout << spisok.name << " " << spisok.surname
- << " " << spisok.year << " " << spisok.pages <<endl;
- }
- fclose(file);
- }
- void output() {
- if ((file = fopen("binaryfile.dat", "rb")) == NULL)
- {
- cout << "opening error!"<<endl;
- return;
- }
- int n = _filelength(_fileno(file)) / sizeof(book);
- book *list = new book[n];
- fread(list, sizeof(book), n, file);
- fclose(file);
- book swap;
- for (int i = 0; i < n - 1; i++)
- for (int j = i + 1; j < n; j++)
- if (list[i].year <= 1990 && list[j].year <= 1990 && strcmp(list[i].name, list[j].name) == 1)
- {
- swap = list[i];
- list[i] = list[j];
- list[j] = swap;
- }
- if ((txt = fopen("fileout.txt", "w")) == NULL)
- {
- cout << "Error at creation!"<<endl;
- return;
- }
- for (int i = 0; i < n; i++)
- if (list[i].year <= 1990)
- {
- cout << list[i].name << endl;
- fprintf(txt, "%c ", list[i].name);
- }
- delete[] list;
- fclose(txt);
- }
- void selectionsort()
- {
- if ((file = fopen("binaryfile.dat", "rb")) == NULL)
- {
- cout << "opening error!" << endl;
- return;
- }
- int n = _filelength(_fileno(file)) / sizeof(book);
- book *list = new book[n];
- fread(list, sizeof(book), n, file);
- fclose(file);
- book t;
- int imin, i, j;
- for (i = 0; i<n - 1; i++)
- {
- imin = i;
- for (j = i + 1; j<n; j++)
- if (list[imin].pages > list[j].pages) imin = j;
- if (imin != i)
- {
- t = list[imin];
- list[imin] = list[i];
- list[i] = t;
- }
- }
- if ((txt = fopen("fileout.txt", "w")) == NULL)
- {
- cout << "Error at creation!" << endl;
- return;
- }
- for (int i = 0; i < n; i++)
- // if (list[i].year <= 1990)
- {
- cout << list[i].name <<" "<<list[i].pages<< endl;
- fprintf(txt, "%c ", list[i].name);
- fprintf(txt, "%c ", list[i].pages);
- }
- delete[] list;
- fclose(txt);
- }
- void quicksort()
- {
- if ((file = fopen("binaryfile.dat", "rb")) == NULL)
- {
- cout << "opening error!" << endl;
- return;
- }
- int n = _filelength(_fileno(file)) / sizeof(book);
- book *list = new book[n];
- fread(list, sizeof(book), n, file);
- fclose(file);
- struct {
- int l;
- int r;
- } stack[20];
- int i, j, left, right, s = 0;
- book t, x;
- stack[s].l = 0; stack[s].r = n - 1;
- while (s != -1)
- {
- left = stack[s].l; right = stack[s].r;
- s--;
- while (left < right)
- {
- i = left; j = right; x = list[(left + right) / 2];
- while (i <= j)
- {
- while (list[i].pages < x.pages) i++;
- while (list[j].pages > x.pages) j--;
- if (i <= j) {
- t = list[i];
- list[i] = list[j];
- list[j] = t;
- i++; j--;
- }
- }
- if ((j - left) < (right - i)) // Выбор более короткой части
- {
- if (i < right) { s++; stack[s].l = i; stack[s].r = right; }
- right = j;
- }
- else {
- if (left < j) { s++; stack[s].l = left; stack[s].r = j; }
- left = i;
- }
- }
- }
- if ((txt = fopen("fileout.txt", "w")) == NULL)
- {
- cout << "Error at creation!" << endl;
- return;
- }
- for (int i = 0; i < n; i++)
- // if (list[i].year <= 1990)
- {
- cout << list[i].name << " " << list[i].pages << endl;
- fprintf(txt, "%c ", list[i].name);
- fprintf(txt, "%c ", list[i].pages);
- }
- delete[] list;
- fclose(txt);
- }
- void linesearch(int x)
- {
- if ((file = fopen("binaryfile.dat", "rb")) == NULL)
- {
- cout << "opening error!" << endl;
- return;
- }
- int n = _filelength(_fileno(file)) / sizeof(book);
- book *list = new book[n];
- fread(list, sizeof(book), n, file);
- fclose(file);
- list[n + 1].pages = x;
- int i = 0;
- while (list[i].pages != x) i++;
- if (i == n + 1) cout << "Required book not found" << endl;
- else cout <<"Found! "<< list[i].name << " " << list[i].pages << endl;
- }
- void binsearch(int x)
- {
- if ((file = fopen("binaryfile.dat", "rb")) == NULL)
- {
- cout << "opening error!" << endl;
- return;
- }
- int n = _filelength(_fileno(file)) / sizeof(book);
- book *list = new book[n];
- fread(list, sizeof(book), n, file);
- fclose(file);
- int i = 0, j = n - 1, m;
- while (i < j)
- {
- m = (i + j) / 2;
- if (x > list[m].pages) i = m + 1; else j = m;
- }
- if (list[i].pages == x) cout << "Found! " << list[i].name << " " << list[i].pages << endl;
- else cout << "Required book not found" << endl;
- }
- int menu() {
- int i;
- cout << "Choose action:"<<endl;
- cout << "1)Enter data to file" << endl;
- cout << "2)Read from file" << endl;
- cout << "3)Output" << endl;
- cout << "4)Sort pages with selectionsort"<<endl;
- cout << "5)Sort pages with QuickSort" << endl;
- cout << "6)Line searching the required book " << endl;
- cout << "7)Binary searching the required book" << endl;
- cout << "8)Exit" << endl;
- cin >> i;
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement