From 99ab1ed89cb91c893798859c00ac7cb0acfb34a1 Mon Sep 17 00:00:00 2001 From: "nolongeractive.mp3" <125088792+nolongeractivemp3@users.noreply.github.com> Date: Wed, 21 Oct 2026 22:40:31 +0200 Subject: [PATCH] Lesson 13 preprocessor --- main.c | 89 +++++++++------------------------------------------------- 1 file changed, 14 insertions(+), 75 deletions(-) diff --git a/main.c b/main.c index 87a9eb5..bdd407f 100644 --- a/main.c +++ b/main.c @@ -1,81 +1,20 @@ + #include #include -#include -#define MAX_LINES 60 -#define LINE_SIZE 256 - -/* given: read one line of stdin into dst without its newline */ -static void read_line(char *dst) { - if (fgets(dst, LINE_SIZE, stdin) == NULL) { - dst[0] = '\0'; - return; - } - size_t k = strlen(dst); - if (k > 0 && dst[k - 1] == '\n') { - dst[k - 1] = '\0'; - } -} - -/* given: read a count line, then that many text lines */ -static int read_batch(char batch[][LINE_SIZE]) { - char head[LINE_SIZE]; - read_line(head); - int count = 0; - sscanf(head, "%d", &count); - if (count < 0) - count = 0; - if (count > MAX_LINES) - count = MAX_LINES; - for (int i = 0; i < count; i++) { - read_line(batch[i]); - } - return count; -} +/* TODO: replace both placeholders with real function-like macros. + CUBE(x) must give x cubed; MAX(a, b) must give the larger of a and b. + Remember: no trailing semicolon on a #define. */ +#define CUBE(x) (x) * (x) * (x) +#define MAX(a, b) (a > b ? a : b) +/* Do not change main: it deliberately passes expressions to your macros. */ int main(void) { - static char morning[MAX_LINES][LINE_SIZE]; - static char evening[MAX_LINES][LINE_SIZE]; - int n_morning = read_batch(morning); - int n_evening = read_batch(evening); - const char *path = "/tmp/diary.txt"; - - /* TODO 1: create the file at `path`, check for NULL, write each of the - n_morning lines in morning[] on its own line, then close it. */ - FILE *fp = fopen(path, "w"); - if (fp == NULL) { /* the ONLY failure report you get */ - perror(path); /* prints: /tmp/scores.txt: Permission denied */ - return 2; - } - for (int i =0; i < n_morning; i++) { - fprintf(fp, "%s\n", morning[i]); /* printf with a stream in front */ - } - fclose(fp); - /* TODO 2: open the same file again so that the n_evening lines in - evening[] are added after the morning lines, check for NULL, - write them, then close it. */ - FILE *fps = fopen(path, "a"); - if (fps == NULL) { /* the ONLY failure report you get */ - perror(path); /* prints: /tmp/scores.txt: Permission denied */ - return 2; - } - for (int i =0; i < n_evening; i++) { - fprintf(fps, "%s\n", evening[i]); /* printf with a stream in front */ - } - fclose(fps); - /* TODO 3: open the file for reading, check for NULL, and print every - line in it as : (numbered from 1, no newline inside - the text), close it, then print the total as below. */ - int total = 0; - char buf[LINE_SIZE]; - FILE *r = fopen(path, "r"); /* w NOT closed yet */ - while (fgets(buf, sizeof buf, r) != NULL) { - total ++; - printf("%d:%s",total, buf); - } - fclose(r); - (void)n_morning; - (void)n_evening; - (void)path; - printf("lines: %d\n", total); + int a = 0, b = 0; + scanf("%d %d", &a, &b); + printf("%d cubed is %d\n", a, CUBE(a)); + printf("%d cubed is %d\n", a + b, CUBE(a + b)); + printf("%d cubed mod 7 is %d\n", b, CUBE(b) % 7); + printf("larger plus one is %d\n", MAX(a, b) + 1); return 0; } +