Cheeel666

Untitled

Jun 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #define ERROR_FILENAME 1
  7. #define ERROR_ARGS 53
  8. #define OK 0
  9. #define SUCCESS 0
  10. #define ERROR_FILEDATA 2
  11.  
  12. #define MAX_NAME_LEN 31
  13. #define MAX_MANUFACTURER_LEN 15
  14. #define ARRAY_LEN 300
  15.  
  16. typedef struct
  17. {
  18. char name[31];
  19. char manufacturer[16];
  20. unsigned int price;
  21. unsigned int count;
  22. } supplies;
  23.  
  24. void sort_supplies(supplies sup[], const int len)
  25. {
  26. supplies temp;
  27. for (int i = 0; i < len - 1; i ++)
  28. {
  29. int min_i = i;
  30. for (int j = i + 1; j < len; j++)
  31. {
  32. if (sup[j].price > sup[min_i].price)
  33. min_i = j;
  34. else if (sup[j].price == sup[min_i].price)
  35. if (sup[j].count > sup[min_i].count)
  36. min_i = j;
  37. }
  38. temp = sup[i];
  39. sup[i] = sup[min_i];
  40. sup[min_i] = temp;
  41. }
  42. }
  43.  
  44. int read_file(FILE *const f, supplies list[], int *const list_len)
  45. {
  46. while (fgets(list[*list_len].name, MAX_NAME_LEN, f))
  47. {
  48. if (!fgets(list[*list_len].manufacturer, MAX_MANUFACTURER_LEN, f))
  49. return ERROR_FILEDATA;
  50.  
  51. if (fscanf(f, "%u %u", &list[*list_len].price, &list[*list_len].count) != 2)
  52. return ERROR_FILEDATA;
  53.  
  54. *list_len += 1;
  55. fseek(f, 1, SEEK_CUR);
  56. }
  57. return OK;
  58. }
  59.  
  60. void print_supplies(FILE *const out, const supplies sup[], const int len)
  61. {
  62. for (int i = 0; i < len; i++)
  63. fprintf(out, "%s%s%u\n%u\n", sup[i].name, sup[i].manufacturer, sup[i].price, sup[i].count);
  64. }
  65.  
  66. void print_same_sup(const supplies sup[], const int len, const char str[MAX_NAME_LEN], int *const kol)
  67. {
  68. for (int i = 0; i < len; i++)
  69. {
  70. int len_name = strlen(sup[i].name) - 1;
  71. int len_substr = strlen(str);
  72.  
  73. if (len_name >= len_substr)
  74. {
  75. char check[MAX_NAME_LEN];
  76. strcpy(check, &sup[i].name[len_name - len_substr]);
  77. check[strlen(check) - 1] = '\0';
  78.  
  79. if (!strcmp(check, str))
  80. {
  81. printf("%s%s%u\n%u\n", sup[i].name, sup[i].manufacturer, sup[i].price, sup[i].count);
  82. ++(*kol);
  83. }
  84. }
  85. }
  86. }
  87.  
  88. int check_number(int n)
  89. {
  90. int i = 0;
  91. while (n > 0)
  92. {
  93. n = n / 10;
  94. i++;
  95. }
  96. return i;
  97. }
  98.  
  99. int find_new_len(supplies *const new, int *const len)
  100. {
  101. if (scanf("%s %s %u %u", new->name, new->manufacturer, &new->price, &new->count) != 4)
  102. return ERROR_FILEDATA;
  103.  
  104. *len += strlen(new->name);
  105. *len += strlen(new->manufacturer);
  106. *len += check_number(new->price);
  107. *len += check_number(new->count);
  108. *len += 4;
  109.  
  110. return OK;
  111. }
  112.  
  113. void find_pos_to_enter(FILE *const file_in, const supplies new, int *const len_to_out)
  114. {
  115. rewind(file_in);
  116. supplies check;
  117. while (!feof(file_in))
  118. {
  119. while (fgets(check.name, MAX_NAME_LEN, file_in))
  120. {
  121. if (!fgets(check.manufacturer, MAX_MANUFACTURER_LEN, file_in))
  122. return;
  123.  
  124. if (fscanf(file_in, "%u %u", &check.price, &check.count) != 2)
  125. return;
  126.  
  127. fseek(file_in, 1, SEEK_CUR);
  128.  
  129. if (new.price > check.price)
  130. break;
  131. else if (new.price == check.price)
  132. if (new.count > check.count)
  133. break;
  134. *len_to_out += strlen(check.name);
  135. *len_to_out += strlen(check.manufacturer);
  136. *len_to_out += check_number(check.price);
  137. *len_to_out += check_number(check.count);
  138. *len_to_out += 2;
  139. }
  140. }
  141. rewind(file_in);
  142. }
  143.  
  144. void shift_file(FILE *const f_from, const int sup_len, const int len_to_shift)
  145. {
  146. int size = 0;
  147. while (!feof(f_from))
  148. {
  149. getc(f_from);
  150. size++;
  151. }
  152. fseek(f_from, 0, SEEK_SET);
  153.  
  154. for (int z = 0; z < sup_len; z++)
  155. {
  156. for (int i = 1; i != size - len_to_shift + 1; i++)
  157. {
  158. int j = -i;
  159. fseek(f_from, j, SEEK_END);
  160. char c = fgetc(f_from);
  161. fputc(c, f_from);
  162. }
  163. }
  164. }
  165.  
  166. void put_in_file_new_sup(FILE *const ptr, const supplies new, const int len)
  167. {
  168. fseek(ptr, len, SEEK_SET);
  169. fprintf(ptr, "%s\n%s\n%u\n%u\n", new.name, new.manufacturer, new.price, new.count);
  170. }
  171.  
  172. int main(int argc, char *argv[])
  173. {
  174. if (argc < 3)
  175. return ERROR_ARGS;
  176.  
  177. if ((strcmp(argv[1], "st") == OK) && (argc == 4))
  178. {
  179. FILE *file_in;
  180. FILE *file_out;
  181.  
  182. if (!(file_in = fopen(argv[2], "rt")))
  183. return ERROR_FILENAME;
  184.  
  185. if (!(file_out = fopen(argv[3], "wt")))
  186. {
  187. fclose(file_in);
  188. return ERROR_FILENAME;
  189. }
  190.  
  191. supplies sup[ARRAY_LEN];
  192. int len = 0;
  193. int er = read_file(file_in, sup, &len);
  194. if (er)
  195. {
  196. fclose(file_in);
  197. fclose(file_out);
  198. return ERROR_FILEDATA;
  199. }
  200.  
  201. if (!len)
  202. {
  203. fclose(file_in);
  204. fclose(file_out);
  205. return ERROR_FILEDATA;
  206. }
  207.  
  208. sort_supplies(sup, len);
  209. print_supplies(file_out, sup, len);
  210.  
  211. fclose(file_in);
  212. fclose(file_out);
  213.  
  214. return SUCCESS;
  215. }
  216. else if (strcmp(argv[1], "ft") == 0 && argc == 4)
  217. {
  218. FILE *file_in;
  219. if (!(file_in = fopen(argv[2], "rt")))
  220. return ERROR_FILENAME;
  221.  
  222. supplies sup[ARRAY_LEN];
  223. int len = 0;
  224. int er = read_file(file_in, sup, &len);
  225. if (er)
  226. {
  227. fclose(file_in);
  228. return ERROR_FILEDATA;
  229. }
  230. if (!len)
  231. {
  232. fclose(file_in);
  233. return ERROR_FILEDATA;
  234. }
  235. int kol = 0;
  236. print_same_sup(sup, len, argv[3], &kol);
  237.  
  238. if (!kol)
  239. return ERROR_FILEDATA;
  240.  
  241. fclose(file_in);
  242.  
  243. return OK;
  244. }
  245. else if (strcmp(argv[1], "at") == 0 && argc == 3)
  246. {
  247. FILE *file_in;
  248. if (!(file_in = fopen(argv[2], "r+")))
  249. return ERROR_FILENAME;
  250.  
  251. int new_sup_len = 0;
  252. supplies new_sup;
  253. int er = find_new_len(&new_sup, &new_sup_len);
  254. if (er)
  255. return ERROR_FILEDATA;
  256.  
  257. int len_to_enter = 0;
  258. find_pos_to_enter(file_in, new_sup, &len_to_enter);
  259.  
  260. shift_file(file_in, new_sup_len, len_to_enter);
  261.  
  262. put_in_file_new_sup(file_in, new_sup, len_to_enter);
  263.  
  264. fclose(file_in);
  265. return OK;
  266. }
  267.  
  268. return ERROR_ARGS;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment