Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5.  
  6. typedef struct {
  7. int code;
  8. char name[20]; // הספר שם
  9. char author[20]; // הספר מחבר
  10. int amount;
  11. }book;
  12.  
  13. FILE* bDB;
  14. void main();
  15.  
  16.  
  17. book books[500];
  18.  
  19.  
  20. int print_main()
  21. {
  22. 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");
  23. return getchar();
  24. }
  25.  
  26. int check_empty()
  27. {
  28. int i = 0;
  29. while (books[i].code != 0)i++;
  30. return i;
  31. }
  32.  
  33. void new_book()
  34. {
  35. system("cls");
  36. int bID = check_empty();
  37. printf("\nBook name:");
  38. while (getchar() != '\n');
  39. gets_s(books[bID].name, 20);
  40. printf("\nBook Code: ");
  41. scanf_s("%d", &books[bID].code);
  42. printf("\nAuthor: ");
  43. while (getchar() != '\n');
  44. gets_s(books[bID].author, 20);
  45. printf("Amount:");
  46. scanf_s("%d", &books[bID].amount);
  47. main();
  48. }
  49.  
  50. void book_increase()
  51. {
  52. int bID;
  53. int bCode = 0;
  54. int incAmount;
  55. printf("\nBook code?\n");
  56. scanf_s("%d", &bCode);
  57. for (bID = 0; bID < 500; bID++)
  58. if (books[bID].code == bCode)
  59. {
  60. printf("\nBook found\n");
  61. printf("increase by how much?\n");
  62. scanf_s("%d", &incAmount);
  63. books[bID].amount += incAmount;
  64. printf("%s increased by %d", books[bID].name, incAmount);
  65. main();
  66. }
  67.  
  68. printf("Failed to find specificed book");
  69. main();
  70.  
  71. }
  72.  
  73. void book_decrease()
  74. {
  75. int bID;
  76. int bCode = 0;
  77. int decAmount;
  78. printf("\nBook code?\n");
  79. scanf_s("%d", &bCode);
  80. for (bID = 0; bID < 500; bID++)
  81. if (books[bID].code == bCode)
  82. {
  83. printf("\nBook found\n");
  84. printf("decrease by how much?\n");
  85. scanf_s("%d", &decAmount);
  86. books[bID].amount -= decAmount;
  87. printf("%s decreased by %d", books[bID].name, decAmount);
  88. main();
  89. }
  90.  
  91. printf("Failed to find specificed book");
  92. main();
  93.  
  94. }
  95.  
  96. void maximum_books()
  97. {
  98. int bID = 0;
  99. for (int i = 0; i < 500; i++)
  100. if (books[bID].amount < books[i].amount) i = bID;
  101.  
  102. printf("Maximum Books are: %d\n Book Name: %s\n", books[bID].amount, books[bID].name);
  103. main();
  104. }
  105.  
  106. int delete_book()
  107. {
  108. int bID;
  109. int bCode = 0;
  110. printf("\nBook code?\n");
  111. scanf_s("%d", &bCode);
  112. for (bID = 0; bID < 500; bID++)
  113. if (books[bID].code == bCode)
  114. {
  115. printf("\nBook deleted\n");
  116. books[bID].code = NULL;
  117. books[bID].amount = NULL;
  118. main();
  119. }
  120.  
  121. printf("Failed to find specificed book");
  122. main();
  123. }
  124.  
  125. void print_books()
  126. {
  127. for (int i = 0; i < 500; i++)
  128. {
  129. if (books[i].code != 0)
  130. {
  131. printf("Book code: %d\n Book Name: %s\n Book author: %s\n Book amount: %d\n",
  132. books[i].code, books[i].name, books[i].author, books[i].amount);
  133. }
  134. }
  135. printf("Press any key to continue");
  136. int key = 0;
  137. while (key == 0)
  138. {
  139. scanf_s("%d", &key);
  140. }
  141. main();
  142. }
  143.  
  144. void save_to_file()
  145. {
  146. remove(bDB);
  147. bDB = fopen("books.ini", "w+");
  148. if (bDB == NULL)exit(EOF);
  149. for (int bID = 0; bID < 500; bID++)
  150. {
  151. fprintf(bDB, "%d %-20.20s %-20.20s %d\n", books[bID].code, books[bID].name, books[bID].author, books[bID].amount);
  152.  
  153. }
  154. fclose(bDB);
  155. main();
  156. }
  157.  
  158. void load_from_file()
  159. {
  160. bDB = fopen("books.ini", "r");
  161. int test;
  162. if (bDB == NULL)exit(EOF);
  163. for (int bID = 0; bID < 500; bID++)
  164. {
  165. fscanf_s(bDB, "%d ", &books[bID].code);
  166. fgets(books[bID].name, 20, bDB);
  167. fgets(books[bID].author, 20, bDB);
  168. fscanf_s(bDB, "%d ", &books[bID].amount);
  169. }
  170. fclose(bDB);
  171. main();
  172. }
  173.  
  174. void main()
  175. {
  176. while (1)
  177. {
  178. system("cls");
  179. switch (print_main())
  180. {
  181. case 'a':
  182. new_book();
  183. break;
  184. case 'i':
  185. book_increase();
  186. break;
  187. case 'd':
  188. book_decrease();
  189. break;
  190. case 'm':
  191. maximum_books();
  192. break;
  193. case 'x':
  194. delete_book();
  195. break;
  196. case 'p':
  197. print_books();
  198. break;
  199. case 's':
  200. save_to_file();
  201. break;
  202.  
  203. case 'l':
  204. load_from_file();
  205. break;
  206. case 'q':
  207. exit(1);
  208. break;
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement