Advertisement
Guest User

Untitled

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