-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz.c
150 lines (137 loc) · 3.45 KB
/
fizzbuzz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
//
#include "fizzbuzz.h"
static int check_mod(int x, int y);
static int check_mod3(const void *s_t);
static int check_mod5(const void *s_t);
static void dbgprintf(const char *, ...);
const char fzbzStr[] = "fizzbuzz\n";
static rdPtrList_t checkNumberStandard[] = {&check_mod3, &check_mod5, NULL};
static int check_mod(int x, int y) { return x % y == 0; }
static int check_mod3(const void *s_t) {
return check_mod(((struct fizzbuzz_obj *)s_t)->value, 3) << FIZZ_bit;
}
static int check_mod5(const void *s_t) {
return check_mod(((struct fizzbuzz_obj *)s_t)->value, 5) << BUZZ_bit;
}
int FzBz_init(struct fizzbuzz_obj *obj, rdPtrList_t *checkNumber) {
// if (!((uintptr_t)obj | (uintptr_t)checkNumber)) {
// return -1;
// }
int err = 0;
// if (!obj) {
// err = -1;
// }
obj->value = 0;
obj->state = NUMSTATES;
obj->baseGame = checkNumber ? 0 : 1;
obj->historyLen = 16;
obj->historyUsed = 0;
obj->history = reallocarray(NULL, obj->historyLen, sizeof(int));
obj->checkNumber = checkNumber ? checkNumber : checkNumberStandard;
if (!obj->history) {
err = -2;
} else {
obj->printBufferSize = 32;
obj->buffFp = open_memstream(&(obj->printBuffer), &(obj->printBufferSize));
if (!obj->buffFp) {
err = -3;
free(obj->history);
}
}
return err;
}
int FzBz_destroy(struct fizzbuzz_obj *obj) {
int ret = fclose(obj->buffFp);
free(obj->history);
free(obj->printBuffer);
return ret;
}
int FzBz_check(struct fizzbuzz_obj *obj, int x) {
// multiple of 3 FIZZ
// mutliple of 5 BUZZ
// multiple of both FIZZBUZZ
// otherwise just the number
if (!obj) {
return -1;
}
obj->value = x;
obj->state = BASE;
if (obj->historyUsed > obj->historyLen) {
const size_t newLen = obj->historyLen * 2;
int *newHistory = reallocarray(obj->history, newLen, sizeof(int));
if (!newHistory) {
free(obj->history);
exit(-ENOMEM);
}
obj->history = newHistory;
obj->historyLen = newLen;
}
obj->history[obj->historyUsed++] = x;
dbgprintf("%d: ", x);
obj->value = x;
if (obj->baseGame) {
#if 1
size_t checkFnc = 0;
while (obj->checkNumber[checkFnc]) {
int res = (*obj->checkNumber[checkFnc])(obj);
dbgprintf("%d ", res);
obj->state |= res;
checkFnc++;
}
dbgprintf("\n");
#else
if (x % 3 == 0) obj->state |= FIZZ_bit;
if (x % 5 == 0) obj->state |= BUZZ_bit;
#endif
} else {
// similair as basegame, but define more outputs
free(obj->history);
exit(-ENOSYS);
}
return 0;
}
void FzBz_flush(struct fizzbuzz_obj *obj) {
fflush(obj->buffFp); // or flush
if (islower(obj->printBuffer[0])) {
obj->printBuffer[0] += ('A' - 'a');
}
fwrite(obj->printBuffer, sizeof(obj->printBuffer[0]), obj->printBufferSize,
stdout);
putchar('\n');
fflush(stdout);
}
void FzBz_print(const struct fizzbuzz_obj *obj) {
switch (obj->state) {
case BASE:
fprintf(obj->buffFp, "%d\n", obj->value);
break;
case FIZZ:
fprintf(obj->buffFp, "%.4s\n", fzbzStr);
break;
case BUZZ:
fputs(&fzbzStr[4], obj->buffFp);
break;
case FIZZBUZZ:
fputs(fzbzStr, obj->buffFp);
break;
default:
break;
}
}
static void dbgprintf(const char *fmt, ...) {
#if 0
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf(stderr, fmt, ap);
va_end(ap);
// return ret;
#else
#endif
}