Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <conio.h>
- #include <stdio.h>
- #include <windows.h>
- #include <string.h>
- struct Litr
- {
- char _FIO[100];
- int _yearb;
- int _yeard;
- char _genre[40];
- }ltr;
- void add(Litr ltr)
- {
- FILE *f;
- fopen_s(&f, "Literary elite.bin", "ab");
- printf_s("%s", "\nEnter Full name\n");
- getchar();
- gets_s(ltr._FIO);
- printf_s("%s", "\nEnter date of Birth\n");
- scanf("%d", <r._yearb);
- printf_s("%s", "\nEnter date of Death\n");
- scanf("%d", <r._yeard);
- printf_s("%s", "\nEnter Character of creation\n");
- getchar();
- gets_s(ltr._genre);
- fwrite(<r, sizeof(ltr), 1, f);
- fclose(f);
- }
- void view(Litr ltr)
- {
- FILE *f;
- fopen_s(&f, "Literary elite.bin", "rb");
- if (!f)
- {
- system("cls");
- printf_s("Empty list! Add some information first\n");
- system("pause");
- }
- else
- {
- int k = 0;
- Litr poet;
- printf(" __________________________________________________\n");
- printf(" | Full name | Years of life | Genre |\n");
- printf(" ---------------------------------------------------\n");
- while (fread(&poet, sizeof(poet), 1, f))
- {
- printf(" %d %-24s %d - %d %-20s\n", k, poet._FIO, poet._yearb, poet._yeard, poet._genre);
- k++;
- }
- fclose(f);
- system("pause");
- }
- }
- void sort(Litr ltr)
- {
- printf("Start sorting > ");
- FILE * f;
- FILE *f1;
- fopen_s(&f, "Literary elite.bin", "rb");
- fopen_s(&f1, "1.bin", "wb");
- int k = 0;
- Litr _Poet;
- if (f == NULL)
- {
- printf("No information\n");
- system("pause");
- }
- else
- {
- while (fread(&_Poet, sizeof(_Poet), 1, f)) k++;
- fseek(f, 0, SEEK_SET);
- struct Litr *poet;
- poet = (struct Litr*)malloc(k * sizeof(struct Litr));
- k = 0;
- while (fread(&poet[k], sizeof(poet[k]), 1, f)) ++k;
- Litr buf;
- for (int i = 0; i < k; i++)
- for (int j = 0; j < k; j++)
- {
- if (_stricmp(poet[i]._FIO, poet[j]._FIO) < 0)
- {
- buf = poet[i];
- poet[i] = poet[j];
- poet[j] = buf;
- }
- }
- for (int i = 0; i < k; i++)
- fwrite(&poet[i], sizeof(poet[i]), 1, f1);
- free(poet);
- fclose(f);
- fclose(f1);
- remove("Literary elite.bin");
- char oldfilename[] = "1.bin";
- char newfilename[] = "Literary elite.bin";
- rename(oldfilename, newfilename);
- printf("complete\n");
- view(_Poet);
- }
- }
- void find(Litr ltr)
- {
- FILE *f;
- fopen_s(&f, "Literary elite.bin", "rb");
- if (!f)
- {
- system("cls");
- printf_s("Empty list! Add some information first\n");
- system("pause");
- }
- else
- {
- char chr[40];
- int lowe, highe, counter=0;
- printf("Input searching Character of creation | ");
- scanf("%s", &chr);
- printf("\tInput lower edge of year: | ");
- scanf("%d", &lowe);
- printf("\tInput higher edge of year: | ");
- scanf("%d", &highe);
- printf("________________________________________________________________\n");
- Litr poet;
- while (fread(&poet, sizeof(poet), 1, f))
- {
- if ((poet._yearb > lowe) && (poet._yeard < highe) && (_stricmp(poet._genre, chr) == 0))
- {
- printf(" %-24s %d - %d %-20s\n", poet._FIO, poet._yearb, poet._yeard, poet._genre);
- counter++;
- }
- }
- if (counter == 0) printf("No match found\n");
- else printf("Searching complete with %d results\n", counter);
- counter = 0;
- fclose(f);
- system("pause");
- }
- }
- void main()
- {
- int key = 0;
- do
- {
- printf_s("%s", "1. Add writer\n2. View list of writers\n3. Sort list\n4. Find writer\n0. Exit\n");
- scanf_s("%d", &key);
- switch (key)
- {
- case 1:system("cls"); add(ltr); break;
- case 2:system("cls"); view(ltr); break;
- case 3:system("cls"); sort(ltr); break;
- case 4:system("cls"); find(ltr); break;
- case 0:exit(1);
- default:
- system("cls");
- printf("Command not found!\n");
- system("pause");
- continue;
- }
- system("cls");
- } while (key != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement