author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:46:53 +0200 | |
changeset 26 | d227047a3e88 |
parent 9 | c3fecc82ade6 |
child 27 | 81a574d60c15 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
2 |
* test/demos/life.c |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
||
9 |
#define MAX 12 |
|
10 |
||
11 |
int read_file (char **); |
|
12 |
int init_life (char **); |
|
13 |
void free_life (char **); |
|
14 |
void show_life (char **); |
|
15 |
||
16 |
int read_file (char **arena) |
|
17 |
{ |
|
18 |
int i, j; |
|
19 |
char org; |
|
20 |
||
21 |
char file[80]; |
|
22 |
FILE *inp = NULL; |
|
23 |
||
24 |
while (inp == NULL) { |
|
25 |
printf ("Please enter the file name>> "); |
|
26 |
if (scanf ("%s", file) < 1) |
|
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
27 |
return -1; |
0 | 28 |
|
29 |
inp = fopen (file, "r"); |
|
30 |
} |
|
31 |
||
32 |
for (i = 0; i < MAX; i++) { |
|
33 |
printf ("Reading Line %3d: ", i); |
|
34 |
for (j = 0; j < MAX; j++) { |
|
35 |
if (feof (inp)) { |
|
36 |
printf ("EOF detected\n"); |
|
37 |
return fclose (inp); |
|
38 |
} |
|
39 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
40 |
if ((fscanf (inp, "%c ", &org)) < 1) |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
41 |
return -1; |
0 | 42 |
|
43 |
arena[i][j] = org; |
|
44 |
printf ("%c ", arena[i][j]); |
|
45 |
} |
|
46 |
printf ("\n"); |
|
47 |
} |
|
48 |
||
49 |
return fclose (inp); |
|
50 |
} |
|
51 |
||
52 |
int init_life (char **arena) |
|
53 |
{ |
|
54 |
int i, j; |
|
55 |
||
56 |
for (i = 0; i < MAX; i++) { |
|
57 |
if ((arena[i] = malloc (MAX + 1)) == NULL) |
|
58 |
return -1; |
|
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
59 |
for (j = 0; j < MAX; j++) |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
60 |
arena[i][j] = '.'; |
0 | 61 |
} |
62 |
||
63 |
for (i = 0; i < MAX; i++) { |
|
64 |
arena[0][i] = '-'; |
|
65 |
arena[MAX - 1][i] = '-'; |
|
66 |
arena[i][0] = '|'; |
|
67 |
arena[i][MAX - 1] = '|'; |
|
68 |
printf ("Initialize: %d\n", i); |
|
69 |
} |
|
70 |
||
71 |
if ((read_file (arena) == EOF)) { |
|
72 |
printf ("\nread_file: ERROR\n"); |
|
73 |
return -1; |
|
74 |
} |
|
75 |
||
76 |
return 0; |
|
77 |
} |
|
78 |
||
79 |
void show_life (char **arena) |
|
80 |
{ |
|
81 |
int i, j; |
|
82 |
||
83 |
for (i = 0; i < MAX; i++) { |
|
84 |
for (j = 0; j < MAX; j++) { |
|
85 |
printf ("%c", arena[i][j]); |
|
86 |
} |
|
87 |
printf ("\n"); |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
void free_life (char **arena) |
|
92 |
{ |
|
93 |
int i; |
|
94 |
||
95 |
if (arena == NULL) |
|
96 |
return; |
|
97 |
||
98 |
for (i = 0; i < MAX; i++) { |
|
99 |
if (arena[i] != NULL) |
|
100 |
free (arena[i]); |
|
101 |
} |
|
102 |
||
103 |
} |
|
104 |
||
105 |
int main (int argc, char **argv) |
|
106 |
{ |
|
107 |
char *arena[MAX]; |
|
108 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
109 |
if (argc > 2) |
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
110 |
printf ("Usage: %s\n", argv[0]); |
0 | 111 |
|
112 |
if (init_life (arena) == 0) |
|
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
113 |
show_life (arena); |
0 | 114 |
free_life (arena); |
115 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
116 |
return EXIT_SUCCESS; |
0 | 117 |
} |