-
Notifications
You must be signed in to change notification settings - Fork 0
/
fact.c
43 lines (33 loc) · 803 Bytes
/
fact.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
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
static bool terms(size_t const n) {
if (printf("(1") < 0)
return false;
for (size_t i = 0; i < n; ++i)
if (printf(" * %zu", i + 1) < 0)
return false;
if (printf(")") < 0)
return false;
return true;
}
static bool dirs(size_t const n) {
for (size_t i = 0; i <= n; ++i)
if (printf("#define BMM_FACT_%zu() ", i) < 0 ||
!terms(i) ||
printf("\n") < 0)
return false;
return true;
}
int main(int const argc, char **const argv) {
if (argc != 2)
return EXIT_FAILURE;
unsigned long int n = strtoul(argv[1], NULL, 10);
if (n == ULONG_MAX)
return EXIT_FAILURE;
if (!dirs((size_t) n))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}