1
0
Fork 0

05: C: Handle variable-size input without trailing whitespace

This commit is contained in:
Mia Herkt 2022-12-05 18:20:05 +01:00
parent 476b55d7aa
commit da4d5ef41d
Signed by: mia
GPG key ID: 72E154B8622EC191

View file

@ -9,16 +9,25 @@ typedef struct crate {
struct crate *nB;
} crate;
#define alloc_stacks(C, N) \
if (C) C = realloc(C, sizeof(crate) * N); \
else C = calloc(1, sizeof(crate) * N);
int main(void) {
char l[64];
crate *A[9] = {0}, *B[9] = {0}, *c, *p;
int i, ll = 0, n, f, t;
char l[BUFSIZ], *e, *m = 0;
crate **A = 0, **B = 0, *c, *p;
int i, n, N = 0, f, t;
while (fgets(l, 64, stdin)) {
if (!ll) ll = strlen(l);
while (fgets(l, BUFSIZ, stdin)) {
if ((e = strrchr(l, ']'))) {
if (e > m) {
m = e;
N = (m - l) / 4 + 1;
alloc_stacks(A, N);
alloc_stacks(B, N);
}
if (strlen(l) == ll && !isdigit(l[1])) {
for (i = 0; i < ll / 4; i++) {
for (i = 0; i < N; i++) {
c = calloc(1, sizeof(crate));
c->c = l[i*4+1];
@ -54,10 +63,10 @@ int main(void) {
}
printf("Silver: ");
for (i = 0; i < ll / 4; i++)
for (i = 0; i < N; i++)
if (A[i]) printf("%c", A[i]->c);
printf("\nGold: ");
for (i = 0; i < ll / 4; i++)
for (i = 0; i < N; i++)
if (B[i]) printf("%c", B[i]->c);
printf("\n");
}