Bonjour, je débute en c, et j'aimerai avoir vos remarques sur le code si dessous.

Il fonctionne comme je veux.

Ce code trouve le plus grand carré possible dans la forme de grille. Je vous laisse essayer

Si vous voyez des points a optimiser, ou des bugs n’hésitez pas a me le dire

Main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include<stdio.h>
#include<stdlib.h>
#include"gestion.h"
#include"fichier.h"
#include"cherche_carre.h"
 
int main(int argc, char **argv)
{
	FILE *fichier;
	int tailleCarre;
 
	if ((fichier = ouvrirFichier()) == NULL)
	{
		pause();
		return EXIT_FAILURE;
	}
 
	if ((tailleCarre = placeCarre(fichier)) > 0)
		printf("\n\nCarre trouve de %d * %d.\n\n", tailleCarre, tailleCarre);
	else
		printf("\n\nPas de carre trouve.\n\n");
 
	fermerFichier(fichier);
 
	pause();
	return EXIT_SUCCESS;
}
gestion.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
#include"gestion.h"
 
void	pause(void)
{
	printf("Appuyer sur entree pour continuer...");
	getchar();
}
gestion.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
#ifndef GESTION
 
#define GESTION
 
#include<stdio.h>
#include<stdlib.h>
 
void	pause(void);
 
#endif
fichier.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef FICHIER
 
#define FICHIER
 
#include<stdio.h>
#include<stdlib.h>
 
#define NOM_FICHIER "Grille.txt"
 
FILE*	ouvrirFichier(void);
void	afficherFichier(FILE* fichier);
void	fermerFichier(FILE* fichier);
 
#endif
fichier.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define _CRT_SECURE_NO_WARNINGS
#include"fichier.h"
 
FILE*	ouvrirFichier(void)
{
	FILE* fichier;
 
	if ((fichier = fopen(NOM_FICHIER, "r")) == NULL)
	{
		printf("Erreur : Ouverture de %s.\n", NOM_FICHIER);
		return NULL;
	}
 
	return fichier;
}
 
void	fermerFichier(FILE* fichier)
{
	fclose(fichier);
	fichier = NULL;
}
cherche_carre.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include"cherche_carre.h"
 
static void		dessineCarre(char** carre, const int position, const int tailleCarre, const int limite_Y)
{
	int _x;
	int _y;
	int decalage_X;
	int decalage_Y;
 
	_x = position / limite_Y;
	_y = position % limite_Y;
 
	decalage_X = 0;
 
	while (decalage_X < tailleCarre)
	{
		decalage_Y = 0;
 
		while (decalage_Y < tailleCarre)
		{
			carre[_x + decalage_X][_y + decalage_Y] = CARRE;
 
			decalage_Y++;
		}
 
		decalage_X++;
	}
}
 
static int		trouveBloc(char** carre, const int position, const int limite_X, const int limite_Y)
{
	int _x;
	int _y;
	int decalage;
	int loop[2];
 
	_x = position / limite_Y;
	_y = position % limite_Y;
 
	if (!(carre[_x][_y] == POSSIBLE))
		return 0;
 
	decalage = 1;
 
	while (_x + decalage < limite_X && _y + decalage < limite_Y)
	{
		if (_x + decalage)
		{
			if (carre[_x + decalage][_y] == BLOC || carre[_x + decalage][_y] == IGNORE || carre[_x + decalage][_y] == INIT)
				return decalage;
		}
 
		loop[0] = 1;
 
		while (loop[0] <= decalage)
		{
			if (carre[_x + decalage][_y + loop[0]] == BLOC || carre[_x + decalage][_y + loop[0]] == IGNORE || carre[_x + decalage][_y + loop[0]] == INIT)
				return decalage;
 
			loop[0]++;
		}
 
		loop[0]--;
		loop[1] = 1;
 
		while (loop[1] <= decalage)
		{
			if (carre[_x + decalage - loop[1]][_y + loop[0]] == BLOC || carre[_x + decalage - loop[1]][_y + loop[0]] == IGNORE || carre[_x + decalage - loop[1]][_y + loop[0]] == INIT)
				return decalage;
 
			loop[1]++;
		}
 
		decalage++;
	}
 
	return decalage;
}
 
static int		chercherCarre(char** carre, int x, int y)
{
	int position;
	int tailleCarre;
	int decalage;
	int fin;
	int memoire;
 
	position = 0;
	decalage = tailleCarre = 0;
	fin = x * y;
 
	while (position < fin)
	{
		decalage = 1;
 
		decalage = trouveBloc(carre, position, x, y);
 
		if (tailleCarre < decalage)
		{
			memoire = position;
			tailleCarre = decalage;
		}
 
		position++;
	}
 
	if(tailleCarre > 0)
		dessineCarre(carre, memoire, tailleCarre, y);
 
	return tailleCarre;
}
 
static void		initCarre(char** carre, int x, int y)
{
	int _y;
 
	while (--x >= 0)
	{
		_y = y;
 
		while (--_y >= 0)
			carre[x][_y] = INIT;
	}
}
 
static bool		attribuValCarre(char** carre, int x, int y, FILE* fichier)
{
	int _y;
	int _x;
	int c;
 
	_x = 0;
 
	fseek(fichier, 0, SEEK_SET);
 
	while (_x < x)
	{
		_y = 0;
 
		while (_y < y)
		{
			switch ((c = fgetc(fichier)))
			{
 
			case BLOC:
				carre[_x][_y] =  BLOC;
				break;
			case IGNORE:
				carre[_x][_y] = IGNORE;
				break;
			case EOF:
				break;
			case '\n':
				break;
			default:
				carre[_x][_y] =  POSSIBLE;
				break;
			}
 
			if (c == '\n' || c == EOF)
				break;
 
			_y++;
		}
 
		_x++;
 
		if (c != '\n' && c != EOF)
			c = fgetc(fichier);
	}
 
	return (c == EOF) ? true : false;
}
 
static void		my_free(void *adresse)
{
	free(adresse);
	adresse = NULL;
}
 
static void		freeCarre(char** carre, int x)
{
	while (--x >= 0)
		my_free(carre[x]);
 
	my_free(carre);
}
 
static bool		valX_Y_Carre(FILE *fichier, int* x, int* y)
{
	int memoire;
	int c;
 
	(*y) = (*x) = c = memoire = 0;
 
	fseek(fichier, 0, SEEK_SET);
 
	while ((c = getc(fichier)) != EOF)
	{
		if (c == '\n')
		{
			if ((*y) < memoire)
				(*y) = memoire;
 
			(*x)++;
 
			memoire = -1;
		}
 
		memoire++;
	}
 
	if ((*y) < memoire)
		(*y) = memoire;
 
	(*x)++;
 
	if (y <= 0)
	{
		printf("Erreur : Fichier vide.\n");
		return false;
	}
 
	return true;
}
 
static char**	creeCarre(FILE* fichier, int *x, int *y)
{
	char** carre;
	int loop;
 
	carre = NULL;
 
	if (valX_Y_Carre(fichier, x, y) == false)
		return NULL;
 
	if ((carre = malloc(sizeof(*carre) * (*x))) == NULL)
	{
		printf("Erreur : Allocation memoire carre X.\n");
		return NULL;
	}
 
	loop = 0;
 
	while (loop < (*x))
		if ((carre[loop++] = malloc(sizeof(**carre) * (*y))) == NULL)
		{
			freeCarre(carre, --loop);
			printf("Erreur : Allocation memoire carre Y %d.\n", loop);
			return NULL;
		}
 
	initCarre(carre, (*x), (*y));
 
	if (attribuValCarre(carre, (*x), (*y), fichier) == false)
		return NULL;
 
	return carre;
}
 
int				placeCarre(FILE* fichier)
{
	int tailleCarre;
	int x;
	int y;
	char **carre;
 
	if ((carre = creeCarre(fichier, &x, &y)) == NULL)
		return 0;
 
	afficherCarre(carre, x, y);
 
	if ((tailleCarre = chercherCarre(carre, x, y)) > 0)
	{
		putc('\n', stdout);
 
		afficherCarre(carre, x, y);
	}
 
	freeCarre(carre, x);
 
	return tailleCarre;
}
 
void			afficherCarre(char** carre, int x, int y)
{
	int _y;
	int _x;
 
	_x = 0;
 
	while (_x < x)
	{
		_y = 0;
 
		while (_y < y)
			printf("%c", carre[_x][_y++]);
 
		_x++;
		putchar('\n');
	}
}
cherche_carre.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CARRE
 
#define CARRE '0'
 
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
 
#define BLOC 'x'
#define POSSIBLE '.'
#define IGNORE ' '
#define INIT '\0'
 
int		placeCarre(FILE* fichier);
void	afficherCarre(char** carre, int x, int y);
 
#endif