Advertisement
STANAANDREY

pb2 ex simplified

Feb 4th, 2023 (edited)
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define IFILE "in.txt"
  4. #define OFILE "out.txt"
  5.  
  6. FILE *openFile(char path[], char mode[]) {
  7.   FILE *f = NULL;
  8.   if ((f = fopen(path, mode)) == NULL) {
  9.     perror("");
  10.     exit(-1);
  11.   }
  12.   return f;
  13. }
  14.  
  15. void closeFile(FILE *f) {
  16.   if (fclose(f) == EOF) {
  17.     perror("");
  18.   }
  19. }
  20.  
  21. int expr(FILE *fin) {
  22.   int a, b;
  23.   fscanf(fin, "%d", &a);
  24.   for (int i = 0; i < 3; i++) fgetc(fin);
  25.   fscanf(fin, "%d", &b);
  26.   return a + b;
  27. }
  28.  
  29. int main(void) {
  30.   FILE *fin = openFile(IFILE, "r");
  31.   FILE *fout = openFile(OFILE, "w");
  32.  
  33.   int id;
  34.   while (fscanf(fin, "%d", &id) == 1) {
  35.     fprintf(fout, "id: %d\n", id);
  36.     int sum = expr(fin);
  37.     fprintf(fout, "res=%d", sum);
  38.  
  39.     for (int last = 0, curr = 0; (curr = fgetc(fin)); last = curr) {
  40.       if (curr == EOF) {
  41.         break;
  42.       }
  43.       fputc(curr, fout);
  44.       if (last == curr && last == '\n') {
  45.         break;
  46.       }
  47.     }
  48.   }
  49.  
  50.   closeFile(fin);
  51.   closeFile(fout);
  52.   return 0;
  53. }
  54.  
  55. /*
  56. pattern:
  57. 1
  58. 2 + 3
  59. afkr
  60. frftgeth
  61. hryh
  62.  
  63. 2
  64. 3 + 4
  65. grs
  66. grff
  67. ff
  68.  
  69.  */
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement