mirror of
https://github.com/nolongeractivemp3/shipthatcode-c.git
synced 2026-11-24 18:45:49 +01:00
Lesson 13 preprocessor
This commit is contained in:
1 parent
317cef8452
commit
99ab1ed89c
1 file changed
+14
-75
@@ -1,81 +1,20 @@
|
|||||||
|
#include <iso646.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define MAX_LINES 60
|
/* TODO: replace both placeholders with real function-like macros.
|
||||||
#define LINE_SIZE 256
|
CUBE(x) must give x cubed; MAX(a, b) must give the larger of a and b.
|
||||||
|
Remember: no trailing semicolon on a #define. */
|
||||||
/* given: read one line of stdin into dst without its newline */
|
#define CUBE(x) (x) * (x) * (x)
|
||||||
static void read_line(char *dst) {
|
#define MAX(a, b) (a > b ? a : b)
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Do not change main: it deliberately passes expressions to your macros. */
|
||||||
int main(void) {
|
int main(void) {
|
||||||
static char morning[MAX_LINES][LINE_SIZE];
|
int a = 0, b = 0;
|
||||||
static char evening[MAX_LINES][LINE_SIZE];
|
scanf("%d %d", &a, &b);
|
||||||
int n_morning = read_batch(morning);
|
printf("%d cubed is %d\n", a, CUBE(a));
|
||||||
int n_evening = read_batch(evening);
|
printf("%d cubed is %d\n", a + b, CUBE(a + b));
|
||||||
const char *path = "/tmp/diary.txt";
|
printf("%d cubed mod 7 is %d\n", b, CUBE(b) % 7);
|
||||||
|
printf("larger plus one is %d\n", MAX(a, b) + 1);
|
||||||
/* 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 <number>:<text> (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);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user