Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define IFILE "in.txt"
- #define OFILE "out.txt"
- FILE *openFile(char path[], char mode[]) {
- FILE *f = NULL;
- if ((f = fopen(path, mode)) == NULL) {
- perror("");
- exit(-1);
- }
- return f;
- }
- void closeFile(FILE *f) {
- if (fclose(f) == EOF) {
- perror("");
- }
- }
- int expr(FILE *fin) {
- int a, b;
- fscanf(fin, "%d", &a);
- for (int i = 0; i < 3; i++) fgetc(fin);
- fscanf(fin, "%d", &b);
- return a + b;
- }
- int main(void) {
- FILE *fin = openFile(IFILE, "r");
- FILE *fout = openFile(OFILE, "w");
- int id;
- while (fscanf(fin, "%d", &id) == 1) {
- fprintf(fout, "id: %d\n", id);
- int sum = expr(fin);
- fprintf(fout, "res=%d", sum);
- for (int last = 0, curr = 0; (curr = fgetc(fin)); last = curr) {
- if (curr == EOF) {
- break;
- }
- fputc(curr, fout);
- if (last == curr && last == '\n') {
- break;
- }
- }
- }
- closeFile(fin);
- closeFile(fout);
- return 0;
- }
- /*
- pattern:
- 1
- 2 + 3
- afkr
- frftgeth
- hryh
- 2
- 3 + 4
- grs
- grff
- ff
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement