fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define BUF 200
  5. #define RECORDS 50
  6. #define SPACES 4
  7.  
  8. FILE * file1, * file2, *resultFile;
  9.  
  10. struct record {
  11. char ingr[BUF/2];
  12. char weight[BUF/2];
  13. char dv[BUF/2];
  14. };
  15.  
  16. void parseFile(struct record * records, FILE * file);
  17. void writeResultToFile(struct record * f1, struct record * f2);
  18. void replace(char * str, char c, char r);
  19. void reverse(char * str);
  20. void crop(char * str, char c);
  21. void trim(char * str);
  22. void fillWeight(char * str, char * weight);
  23. void fillDv(char * str, char * dv);
  24.  
  25. int main(int argc, char * argv[]) {
  26. if (argc != 3) {
  27. fprintf(stderr, "Usage: cmpr.exe F1 F2\n");
  28. exit(1);
  29. }
  30. char * fileName1 = argv[1];
  31. file1 = fopen(fileName1, "r");
  32. if (file1 == NULL) {
  33. fprintf(stderr, "Can't open input file %s!\n", file1);
  34. exit(1);
  35. }
  36. char * fileName2 = argv[2];
  37. file2 = fopen(fileName2, "r");
  38. if (file2 == NULL) {
  39. fprintf(stderr, "Can't open input file %s!\n", file2);
  40. exit(1);
  41. }
  42. char * resultFileName = fileName1;
  43. strcat(resultFileName, "_");
  44. strcat(resultFileName, fileName2);
  45. strcat(resultFileName, "_DIFF.txt");
  46. resultFile = fopen(resultFileName, "w");
  47. if (resultFile == NULL) {
  48. fprintf(stderr, "Can't open input file %s!\n", resultFile);
  49. exit(1);
  50. }
  51.  
  52. struct record fOneRecs [RECORDS];
  53. struct record fTwoRecs [RECORDS];
  54.  
  55. parseFile(fOneRecs, file1);
  56. parseFile(fTwoRecs, file2);
  57. writeResultToFile(fOneRecs, fTwoRecs);
  58.  
  59. fclose(file1);
  60. fclose(file2);
  61. fclose(resultFile);
  62. return 0;
  63. }
  64.  
  65. void parseFile(struct record * records, FILE * file) {
  66. char line [BUF];
  67. char ingr [BUF];
  68. char params [BUF];
  69. char weight [BUF];
  70. char dv [BUF];
  71. int recCount = 0;
  72.  
  73. while (fgets(line, sizeof(line), file) != NULL) {
  74. replace(line, '\t', ' ');
  75. reverse(line);
  76. int i = 0, count = 0;
  77. while (line[i]) {
  78. params[i] = line[i];
  79. if (count == SPACES) {
  80. int j = 0;
  81. ++i;
  82. params[i] = '\0';
  83. while (line[i]) {
  84. ingr[j++] = line[i++];
  85. }
  86. ingr[j] = '\0';
  87. break;
  88. }
  89. if (line[i] == ' ') {
  90. count++;
  91. }
  92. ++i;
  93. }
  94. reverse(ingr);
  95. crop(ingr, '(');
  96. fillDv(params, dv);
  97. reverse(dv);
  98. reverse(params);
  99. trim(ingr);
  100. trim(params);
  101. fillWeight(params, weight);
  102. strcpy(records[recCount].ingr, ingr);
  103. strcpy(records[recCount].weight, weight);
  104. strcpy(records[recCount].dv, dv);
  105. recCount++;
  106. }
  107. }
  108.  
  109. void writeResultToFile(struct record * f1, struct record * f2) {
  110. int i = 0, j = 0, k = 0;
  111. while (1) {
  112. j = 0;
  113. if (!strcmp(f1[i].ingr, "")) break;
  114. while (1) {
  115. if (!strcmp(f2[j].ingr, "")) break;
  116. if (!strcmp(f1[i].ingr, f2[j].ingr)) {
  117. fprintf(resultFile, "%-20s %-10s(%-7sDV) \t%-5s %-10s(%-7sDV)\n", f1[i].ingr, f1[i].weight, f1[i].dv, "<>", f2[j].weight, f2[j].dv);
  118. k++;
  119. }
  120. j++;
  121. }
  122. i++;
  123. }
  124. }
  125.  
  126.  
  127. void replace(char *str, char c, char r) {
  128. while (*str) {
  129. if (*str == c) {
  130. *str = r;
  131. }
  132. ++str;
  133. }
  134.  
  135. }
  136.  
  137. void reverse(char * str) {
  138. char buffer[BUF];
  139. int i = strlen(str) - 1;
  140. int j = 0;
  141. for (i; i >= 0; i--) {
  142. buffer[j++] = *(str + i);
  143. }
  144. for (i = 0; i < strlen(str); i++) {
  145. *(str + i) = buffer[i];
  146. }
  147. *(str + i) = '\0';
  148. }
  149.  
  150. void crop(char * str, char c) {
  151. while (*str) {
  152. if (*str == c) {
  153. *str = '\0';
  154. break;
  155. }
  156. str++;
  157. }
  158. }
  159.  
  160. void trim(char * str) {
  161. char * end = str + strlen(str) - 1;
  162. if (*end == ' ') {
  163. *end = '\0';
  164. }
  165. }
  166.  
  167. void fillWeight(char * str, char * weight) {
  168. int spaceCount = 0;
  169. while (*str == ' ' || *str == '\t') {
  170. ++str;
  171. }
  172. while (*str) {
  173. if (*str == ' ' || *str == '\t') ++spaceCount;
  174. if (spaceCount == 2) break;
  175. *weight++ = *str++;
  176. }
  177. *weight = '\0';
  178. }
  179. void fillDv(char * str, char * dv) {
  180. int spaceCount = 0;
  181. while(*str != ' ' && *str != '\t') {
  182. if (*str == '\n') {
  183. ++str;
  184. continue;
  185. }
  186. *dv++ = *str++;
  187. }
  188. *dv = '\0';
  189. }
  190.  
Runtime error #stdin #stdout #stderr 0s 2288KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Usage: cmpr.exe F1 F2