diff --git a/README.md b/README.md index 30cfebf..bf09309 100644 --- a/README.md +++ b/README.md @@ -392,18 +392,20 @@ The main modifications are: * Replacing types to build it with Visual Studio. * Some bug fixes. -And special thanks to the following products for useful test codes. +And special thanks to the following products for useful test & sample codes. * Test Code * [lacc][] (MIT License) * [8cc][] (MIT License) * [qcc][] (MIT License) * [picoc][] (BSD License) + * [algo-c][] (CC0) - in Japanese [lacc]: https://github.com/larmel/lacc/ [8cc]: https://github.com/rui314/8cc/ [qcc]: https://github.com/maekawatoshiki/qcc/ [picoc]: https://github.com/jpoirier/picoc/ +[algo-c]: https://github.com/okumuralab/algo-c About library, so many thanks to the following amazing products. diff --git a/samples/algo-c/endian.c b/samples/algo-c/endian.c new file mode 100644 index 0000000..54d4999 --- /dev/null +++ b/samples/algo-c/endian.c @@ -0,0 +1,14 @@ +#include +#include + +int main(void) +{ + int i = 1; + if (*((char *)&i)) + printf("little-endian\n"); + else if (*((char *)&i + (sizeof(int) - 1))) + printf("big-endian\n"); + else + printf("unknown\n"); + return 0; +} diff --git a/samples/algo-c/knight.c b/samples/algo-c/knight.c new file mode 100644 index 0000000..8be6a2f --- /dev/null +++ b/samples/algo-c/knight.c @@ -0,0 +1,44 @@ +#include +#include + +#define N 5 + +int board[N + 4][N + 4], + dx[8] = { 2, 1,-1,-2,-2,-1, 1, 2 }, + dy[8] = { 1, 2, 2, 1,-1,-2,-2,-1 }; + +void printboard(void) +{ + int i, j; + static solution = 0; + + printf("\nSolution #%d\n", ++solution); + for (i = 2; i <= N + 1; i++) { + for (j = 2; j <= N + 1; j++) printf("%4d", board[i][j]); + printf("\n"); + } +} + +void try(int x, int y) +{ + int i; + static int count = 0; + + if (board[x][y] != 0) return; + board[x][y] = ++count; + if (count == N * N) printboard(); + else for (i = 0; i < 8; i++) try(x + dx[i], y + dy[i]); + board[x][y] = 0; count--; +} + +int main(void) +{ + int i, j; + + for (i = 0; i <= N + 3; i++) + for (j = 0; j <= N + 3; j++) board[i][j] = 1; + for (i = 2; i <= N + 1; i++) + for (j = 2; j <= N + 1; j++) board[i][j] = 0; + try(2, 2); + return 0; +} diff --git a/samples/algo-c/life.c b/samples/algo-c/life.c new file mode 100644 index 0000000..93f4160 --- /dev/null +++ b/samples/algo-c/life.c @@ -0,0 +1,35 @@ +#include +#include + +#define N 22 +#define M 78 +#define ALLDISP 1 +#define LAST 1000 +char a[N + 2][M + 2], b[N + 2][M + 2]; + +int main(void) +{ + int i, j, g; + + a[N/2][M/2] = a[N/2-1][M/2] = a[N/2+1][M/2] + = a[N/2][M/2-1] = a[N/2-1][M/2+1] = 1; + for (g = 1; g <= LAST; g++) { + if (ALLDISP || g == LAST) printf("Generation %4d\n", g); + for (i = 1; i <= N; i++) { + for (j = 1; j <= M; j++) + if (a[i][j]) { + if (ALLDISP || g == LAST) printf("*"); + b[i-1][j-1]++; b[i-1][j]++; b[i-1][j+1]++; + b[i ][j-1]++; b[i ][j+1]++; + b[i+1][j-1]++; b[i+1][j]++; b[i+1][j+1]++; + } else if (ALLDISP || g == LAST) printf("."); + if (ALLDISP || g == LAST) printf("\n"); + } + for (i = 0; i <= N + 1; i++) + for (j = 0; j <= M + 1; j++) { + if (b[i][j] != 2) a[i][j] = (b[i][j] == 3); + b[i][j] = 0; + } + } + return 0; +} diff --git a/samples/algo-c/magic4.c b/samples/algo-c/magic4.c new file mode 100644 index 0000000..a11fbd9 --- /dev/null +++ b/samples/algo-c/magic4.c @@ -0,0 +1,35 @@ +#include +#include +#define B(x) if (ok[x]) { ok[x]=0; +#define E(x) } ok[x]=1; +#define forall(x) for (x = 1; x <= 16; x++) +#define FORMAT \ + "%4d%4d%4d%4d\n%4d%4d%4d%4d\n%4d%4d%4d%4d\n%4d%4d%4d%4d\n" +int main(void) +{ + int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, + x, count = 0, + ok11[40], *ok = ok11 + 11; /* ok[-11..28] */ + + for (x = -11; x <= 0; x++) ok[x] = 0; + for (x = 1; x <= 16; x++) ok[x] = 1; + for (x = 17; x <= 28; x++) ok[x] = 0; + forall(a) { ok[a] = 0; + for (d = a + 1; d <= 16; d++) { ok[d] = 0; + for (m = d + 1; m <= 16; m++) { ok[m] = 0; + p = 34 - a - d - m; + if (ok[p] && a < p) { ok[p] = 0; + forall(b) B(b) c = 34 - a - b - d; B(c) + forall(f) B(f) k = 34 - a - f - p; B(k) + forall(g) B(g) j = 34 - d - g - m; + B(j) n = 34 - b - f - j; B(n) o = 34 - c - g - k; B(o) + forall(e) B(e) i = 34 - a - e - m; + B(i) h = 34 - e - f - g; B(h) l = 34 - i - j - k; + if (ok[l]) { + printf("Solution #%d\n", ++count); + printf(FORMAT, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p); + E(h) E(i) E(e) E(o) E(n) E(j) E(g) E(k) + E(f) E(c) E(b) E(p) E(m) E(d) E(a) + } + return 0; +} diff --git a/samples/algo-c/maze.c b/samples/algo-c/maze.c new file mode 100644 index 0000000..2ed970a --- /dev/null +++ b/samples/algo-c/maze.c @@ -0,0 +1,68 @@ +#include +#include +#include + +#define XMAX 80 +#define YMAX 24 +#define MAXSITE (XMAX * YMAX / 4) +char map[XMAX + 1][YMAX + 1]; +int nsite = 0; +int xx[MAXSITE], yy[MAXSITE]; +int dx[4] = { 2, 0, -2, 0 }; +int dy[4] = { 0, 2, 0, -2 }; +int dirtable[24][4] = { + 0,1,2,3, 0,1,3,2, 0,2,1,3, 0,2,3,1, 0,3,1,2, 0,3,2,1, + 1,0,2,3, 1,0,3,2, 1,2,0,3, 1,2,3,0, 1,3,0,2, 1,3,2,0, + 2,0,1,3, 2,0,3,1, 2,1,0,3, 2,1,3,0, 2,3,0,1, 2,3,1,0, + 3,0,1,2, 3,0,2,1, 3,1,0,2, 3,1,2,0, 3,2,0,1, 3,2,1,0 }; + +void add(int i, int j) +{ + xx[nsite] = i; yy[nsite] = j; nsite++; +} + +int select(int *i, int *j) +{ + int r; + + if (nsite == 0) return 0; + nsite--; r = (int)(nsite * (rand() / (RAND_MAX + 1.0))); + *i = xx[r]; xx[r] = xx[nsite]; + *j = yy[r]; yy[r] = yy[nsite]; return 1; +} + +int main(void) +{ + int i, j, i1, j1, d, t, *tt; + + srand((unsigned)time(NULL)); + for (i = 0; i <= XMAX; i++) + for (j = 0; j <= YMAX; j++) map[i][j] = 1; + for (i = 3; i <= XMAX - 3; i++) + for (j = 3; j <= YMAX - 3; j++) map[i][j] = 0; + map[2][3] = 0; map[XMAX - 2][YMAX - 3] = 0; + for (i = 4; i <= XMAX - 4; i += 2) { + add(i, 2); add(i, YMAX - 2); + } + for (j = 4; j <= YMAX - 4; j += 2) { + add(2, j); add(XMAX - 2, j); + } + while (select(&i, &j)) { + for ( ; ; ) { + tt = dirtable[(int)(24 * (rand() / (RAND_MAX + 1.0)))]; + for (d = 3; d >= 0; d--) { + t = tt[d]; i1 = i + dx[t]; j1 = j + dy[t]; + if (map[i1][j1] == 0) break; + } + if (d < 0) break; + map[(i + i1) / 2][(j + j1) / 2] = 1; + i = i1; j = j1; map[i][j] = 1; add(i, j); + } + } + for (j = 2; j <= YMAX - 2; j++) { + for (i = 2;i <= XMAX - 2; i++) + if (map[i][j]) putchar('X'); else putchar(' '); + putchar('\n'); + } + return 0; +} diff --git a/samples/algo-c/nqueen.c b/samples/algo-c/nqueen.c new file mode 100644 index 0000000..04c6723 --- /dev/null +++ b/samples/algo-c/nqueen.c @@ -0,0 +1,45 @@ +#include +#include + +#define N 8 +int a[N], b[2 * N - 1], c[2 * N - 1], x[N]; + +void found(void) +{ + int i, j; + static solution = 0; + + printf("\nSolution #%d\n", ++solution); + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + if (x[i] == j) printf(" Q"); + else printf(" ."); + printf("\n"); + } +} + +void try(int i) +{ + int j; + + for (j = 0; j < N; j++) + if (a[j] && b[i + j] && c[i - j + N - 1]) { + x[i] = j; + if (i < N - 1) { + a[j] = b[i + j] = c[i - j + N - 1] = 0; + try(i + 1); + a[j] = b[i + j] = c[i - j + N - 1] = 1; + } else found(); + } +} + +int main(void) +{ + int i; + + for (i = 0; i < N; i++) a[i] = 1; + for (i = 0; i < 2 * N - 1; i++) b[i] = 1; + for (i = 0; i < 2 * N - 1; i++) c[i] = 1; + try(0); + return 0; +}