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