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