Advertisement
Guest User

Untitled

a guest
May 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. struct product {
  8. unsigned long long id;
  9. char name[51];
  10. double price;
  11. int quantity;
  12. char date[11];
  13. };
  14.  
  15. struct products {
  16. struct product val;
  17. struct products *next;
  18. };
  19.  
  20. struct products* find_by_id(struct products*, unsigned long long id);
  21. int isUniqueID(struct products *products, unsigned long long id);
  22. void delete_by_id(struct products*, unsigned long long id);
  23. int change_quantity_of_product(struct products*);
  24. int print_all_expired_products(struct products*);
  25. int print_product_with_id(struct products*);
  26. int add_new_product(struct products*);
  27. int load_products(struct products*);
  28. int save_products(struct products*);
  29. time_t string_to_time(char*);
  30.  
  31. int main()
  32. {
  33. int option = 0,exit_flag = 1;
  34. struct products *products = NULL;
  35. products = malloc(sizeof(struct products));
  36. if (products == NULL)
  37. {
  38. printf("Memory allocation error");
  39. return 1;
  40. }
  41. products->next = NULL;
  42. if (load_products(products) == 1)
  43. {
  44. printf("Memory allocation error");
  45. return 1;
  46. }
  47.  
  48. while(exit_flag)
  49. {
  50. system("@cls||clear");
  51. printf(" *****************/MENU\\*****************\n");
  52. printf("* *\n");
  53. printf("* 1.Add new item in the warehouse *\n");
  54. printf("* 2.Change the quantity *\n");
  55. printf("* 3.Display the expired items *\n");
  56. printf("* 4.Display information for item *\n");
  57. printf("* 5.Exit *\n");
  58. printf("* *\n");
  59. printf("******************************************\n");
  60. printf("Insert option: ");
  61. if (scanf("%d", &option) == EOF)
  62. {
  63. break;
  64. }
  65.  
  66. switch (option) {
  67. case 1:
  68. if (add_new_product(products)) {
  69. return -1;
  70. }
  71. break;
  72. case 2:
  73. if (change_quantity_of_product(products)) {
  74. return -1;
  75. }
  76. break;
  77. case 3:
  78. if (print_all_expired_products(products)) {
  79. return -1;
  80. }
  81. break;
  82. case 4:
  83. if (print_product_with_id(products)) {
  84. return -1;
  85. }
  86. break;
  87. case 5:
  88. exit_flag = 0;
  89. break;
  90. default:
  91. break;
  92. }
  93.  
  94. system("pause");
  95. }
  96.  
  97. if (save_products(products))
  98. {
  99. return -1;
  100. }
  101. return 0;
  102. }
  103.  
  104. int isUniqueID(struct products *products, unsigned long long id)
  105. {
  106. while(products)
  107. {
  108. if(products->val.id == id)
  109. {
  110. printf("ID already exist!\n");
  111. return 0;
  112. }
  113. products = products->next;
  114. }
  115. return 1;
  116. }
  117.  
  118. int load_products(struct products *products)
  119. {
  120. FILE *fp;
  121. if ((fp = fopen("data", "rb")) == NULL)
  122. {
  123. printf("The file cannot be opened");
  124. exit(1);
  125. }
  126.  
  127. struct products *prev_products = products;
  128. struct products *curr_products = NULL;
  129. while (1)
  130. {
  131. struct product product;
  132. if (fread(&product, sizeof(struct product), 1, fp) != 1)
  133. {
  134. if (feof(fp))
  135. {
  136. break;
  137. }
  138. printf("reading error");
  139. break;
  140. }
  141.  
  142. curr_products = malloc(sizeof(struct products));
  143. if (curr_products == NULL)
  144. {
  145. printf("Memory allocation error");
  146. return 1;
  147. }
  148. curr_products->val = product;
  149. curr_products->next = NULL;
  150.  
  151. prev_products->next = curr_products;
  152. prev_products = curr_products;
  153. }
  154. fclose(fp);
  155. return 0;
  156. }
  157.  
  158. int save_products(struct products *products)
  159. {
  160. FILE *fp = NULL;
  161. if ((fp = fopen("data", "ab")) == NULL)
  162. {
  163. printf("The file cannot be opened");
  164. exit(2);
  165. }
  166. while (1)
  167. {
  168. products = products->next;
  169. if (products == NULL)
  170. {
  171. break;
  172. }
  173.  
  174. if (fwrite(&products->val, sizeof(struct product), 1, fp) != 1)
  175. {
  176. printf("writening error");
  177. exit(3);
  178. }
  179. }
  180. fclose(fp);
  181. return 0;
  182. }
  183.  
  184. struct products* find_by_id(struct products *products,unsigned long long id)
  185. {
  186. struct products *iterate_products = products->next;
  187. while(1)
  188. {
  189. if (iterate_products == NULL)
  190. {
  191. printf("The warehouse is empty.");
  192. exit(212);
  193. }
  194. if (iterate_products->val.id == id)
  195. {
  196. return iterate_products;
  197. }
  198. iterate_products = iterate_products->next;
  199. }
  200. return NULL;
  201. }
  202.  
  203. void delete_by_id(struct products *products, unsigned long long id)
  204. {
  205. struct products *prev_products = products;
  206. struct products *iterate_products = products->next;
  207. while(1)
  208. {
  209. if (iterate_products == NULL)
  210. {
  211. printf("The warehouse is empty.");
  212. break;
  213. }
  214. if (iterate_products->val.id == id)
  215. {
  216. prev_products->next = iterate_products->next;
  217. break;
  218. }
  219. iterate_products = iterate_products->next;
  220. prev_products = prev_products->next;
  221. }
  222. }
  223.  
  224. time_t string_to_time(char *string_time)
  225. {
  226. time_t result = 0;
  227. int day = 0, month = 0, year = 0;
  228.  
  229. if (sscanf(string_time, "%2d.%2d.%4d", &day, &month, &year) == 3)
  230. {
  231. struct tm time = {0};
  232. time.tm_year = year - 1900;
  233. time.tm_mon = month - 1;
  234. time.tm_mday = day;
  235. if ((result = mktime(&time)) == (time_t) - 1)
  236. {
  237. perror("mktime");
  238. return -1;
  239. }
  240. }
  241. return result;
  242. }
  243.  
  244. int add_new_product(struct products *products)
  245. {
  246. struct product product;
  247. unsigned long long id=0;
  248. char name[51] = "",date[11] = "";
  249. double price=0;
  250. int quantity=0;
  251. do{
  252. printf("Insert id: ");
  253. fflush(stdin);
  254. scanf("%llu", &id);
  255. }while(!(isUniqueID(products, id)) || !id || id > 999999999999|| id<100000000000);
  256.  
  257. printf("Insert name: ");
  258. scanf("%50s",name);
  259. printf("Insert price: ");
  260. scanf("%lf", &price);
  261. printf("Insert quantity: ");
  262. scanf("%d", &quantity);
  263. printf("Insert date /in dd.mm.yyyy format/: ");
  264. scanf("%s", date);
  265.  
  266. product.id = id;
  267. strcpy(product.name, name);
  268. product.price = price;
  269. product.quantity = quantity;
  270. strcpy(product.date, date);
  271.  
  272. struct products *last_products = products;
  273. while(last_products->next)
  274. {
  275. last_products = last_products->next;
  276. }
  277.  
  278. struct products *next_products = NULL;
  279. next_products = malloc(sizeof(struct products));
  280. if (next_products == NULL)
  281. {
  282. printf("Memory allocation error.");
  283. return -1;
  284. }
  285. next_products->val = product;
  286. next_products->next = NULL;
  287. last_products->next = next_products;
  288. return 0;
  289. }
  290.  
  291. int print_product_with_id(struct products *products)
  292. {
  293. unsigned long long id=0;
  294. printf("Insert id: ");
  295. scanf("%llu", &id);
  296.  
  297. struct products *products_with_id = find_by_id(products, id);
  298. if (products_with_id == NULL)
  299. printf("Missing product!\n");
  300. else
  301. {
  302. printf("id: %llu\n", products_with_id->val.id);
  303. printf("name: %s\n", products_with_id->val.name);
  304. printf("price: %.2f\n", products_with_id->val.price);
  305. printf("quantity: %d\n", products_with_id->val.quantity);
  306. printf("date: %s\n", products_with_id->val.date);
  307. }
  308. return 0;
  309. }
  310.  
  311. int print_all_expired_products(struct products *products)
  312. {
  313. char date[11];
  314. printf("Insert date to compare /in dd.mm.yyyy format/: ");
  315. scanf("%s",date);
  316. time_t date_t = string_to_time(date);
  317. if (date_t == -1)
  318. {
  319. return -1;
  320. }
  321.  
  322. struct products *iterate_products = products->next;
  323. while(1)
  324. {
  325. if (iterate_products == NULL)
  326. {
  327. printf("The warehouse is empty or the date is wrong entered.");
  328. break;
  329. }
  330. time_t product_date_t = string_to_time(iterate_products->val.date);
  331. if (product_date_t == -1)
  332. {
  333. return -1;
  334. }
  335. if (difftime(product_date_t, date_t) < 0)
  336. {
  337. printf("id: %llu\n", iterate_products->val.id);
  338. printf("name: %s\n", iterate_products->val.name);
  339. printf("price: %.2f\n", iterate_products->val.price);
  340. printf("quantity: %d\n", iterate_products->val.quantity);
  341. printf("date: %s\n", iterate_products->val.date);
  342. }
  343. iterate_products = iterate_products->next;
  344. }
  345. return 0;
  346. }
  347. int change_quantity_of_product(struct products *products)
  348. {
  349. unsigned long long id=0;int quantity=0;
  350. printf("Insert id: ");
  351. scanf(" %llu", &id);
  352. printf("Insert quantity to change[(+)for adding and (-)for subtraction]:");
  353. scanf("%d", &quantity);
  354. struct products *products_with_id = find_by_id(products, id);
  355. if (products_with_id == NULL)
  356. printf("Missing product!\n");
  357. else
  358. {
  359. int new_quantity = products_with_id->val.quantity + quantity;
  360. if (new_quantity < 0)
  361. printf("Quantity to change is bigger than product quantity!\n");
  362. else if (new_quantity > 0)
  363. products_with_id->val.quantity = new_quantity;
  364. else if (new_quantity == 0)
  365. delete_by_id(products, id);
  366. }
  367. return 0;
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement