Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- typedef struct {
- int code;
- char name[20]; // הספר שם
- char author[20]; // הספר מחבר
- int amount;
- }book;
- FILE* bDB;
- void main();
- book books[500];
- int print_main()
- {
- printf("------------- M E N U ---------\na-- - Add a new Book\ni-- - increase items quantity\nd-- - decrease items quantity\nm-- - find maximum books\nx-- - To delete book \np----Print Books all \ns----Save to File\nl----Load from File\nq----Quit To\n");
- return getchar();
- }
- int check_empty()
- {
- int i = 0;
- while (books[i].code != 0)i++;
- return i;
- }
- void new_book()
- {
- system("cls");
- int bID = check_empty();
- printf("\nBook name:");
- while (getchar() != '\n');
- gets_s(books[bID].name, 20);
- printf("\nBook Code: ");
- scanf_s("%d", &books[bID].code);
- printf("\nAuthor: ");
- while (getchar() != '\n');
- gets_s(books[bID].author, 20);
- printf("Amount:");
- scanf_s("%d", &books[bID].amount);
- main();
- }
- void book_increase()
- {
- int bID;
- int bCode = 0;
- int incAmount;
- printf("\nBook code?\n");
- scanf_s("%d", &bCode);
- for (bID = 0; bID < 500; bID++)
- if (books[bID].code == bCode)
- {
- printf("\nBook found\n");
- printf("increase by how much?\n");
- scanf_s("%d", &incAmount);
- books[bID].amount += incAmount;
- printf("%s increased by %d", books[bID].name, incAmount);
- main();
- }
- printf("Failed to find specificed book");
- main();
- }
- void book_decrease()
- {
- int bID;
- int bCode = 0;
- int decAmount;
- printf("\nBook code?\n");
- scanf_s("%d", &bCode);
- for (bID = 0; bID < 500; bID++)
- if (books[bID].code == bCode)
- {
- printf("\nBook found\n");
- printf("decrease by how much?\n");
- scanf_s("%d", &decAmount);
- books[bID].amount -= decAmount;
- printf("%s decreased by %d", books[bID].name, decAmount);
- main();
- }
- printf("Failed to find specificed book");
- main();
- }
- void maximum_books()
- {
- int bID = 0;
- for (int i = 0; i < 500; i++)
- if (books[bID].amount < books[i].amount) i = bID;
- printf("Maximum Books are: %d\n Book Name: %s\n", books[bID].amount, books[bID].name);
- main();
- }
- int delete_book()
- {
- int bID;
- int bCode = 0;
- printf("\nBook code?\n");
- scanf_s("%d", &bCode);
- for (bID = 0; bID < 500; bID++)
- if (books[bID].code == bCode)
- {
- printf("\nBook deleted\n");
- books[bID].code = NULL;
- books[bID].amount = NULL;
- main();
- }
- printf("Failed to find specificed book");
- main();
- }
- void print_books()
- {
- for (int i = 0; i < 500; i++)
- {
- if (books[i].code != 0)
- {
- printf("Book code: %d\n Book Name: %s\n Book author: %s\n Book amount: %d\n",
- books[i].code, books[i].name, books[i].author, books[i].amount);
- }
- }
- printf("Press any key to continue");
- int key = 0;
- while (key == 0)
- {
- scanf_s("%d", &key);
- }
- main();
- }
- void save_to_file()
- {
- remove(bDB);
- bDB = fopen("books.ini", "w+");
- if (bDB == NULL)exit(EOF);
- for (int bID = 0; bID < 500; bID++)
- {
- fprintf(bDB, "%d %-20.20s %-20.20s %d\n", books[bID].code, books[bID].name, books[bID].author, books[bID].amount);
- }
- fclose(bDB);
- main();
- }
- void load_from_file()
- {
- bDB = fopen("books.ini", "r");
- int test;
- if (bDB == NULL)exit(EOF);
- for (int bID = 0; bID < 500; bID++)
- {
- fscanf_s(bDB, "%d ", &books[bID].code);
- fgets(books[bID].name, 20, bDB);
- fgets(books[bID].author, 20, bDB);
- fscanf_s(bDB, "%d ", &books[bID].amount);
- }
- fclose(bDB);
- main();
- }
- void main()
- {
- while (1)
- {
- system("cls");
- switch (print_main())
- {
- case 'a':
- new_book();
- break;
- case 'i':
- book_increase();
- break;
- case 'd':
- book_decrease();
- break;
- case 'm':
- maximum_books();
- break;
- case 'x':
- delete_book();
- break;
- case 'p':
- print_books();
- break;
- case 's':
- save_to_file();
- break;
- case 'l':
- load_from_file();
- break;
- case 'q':
- exit(1);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement