Skip to content

Commit 3bbf973

Browse files
Luís ParraLuís Parra
Luís Parra
authored and
Luís Parra
committed
message
1 parent 58bbdd1 commit 3bbf973

File tree

2 files changed

+52
-84
lines changed

2 files changed

+52
-84
lines changed

Proj_Int.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66
#include <string.h>
77

88
int main(int argc, char *argv[]);
9-
void Read_input_file(char *filename);
9+
int **Read_input_file(FILE *fp_in, int **labyrinth, char *filename);
1010
void help(void);
1111
int mode_A1(int a, int b, int **labyrinth, int L, int C);
1212
int mode_A2(int a, int b, int **labyrinth, int L, int C);
1313
int mode_A3(int a, int b, int **labyrinth, int L, int C);
1414
int mode_A4(int a, int b, int **labyrinth, int L, int C);
1515
int mode_A5(int a, int b, int **labyrinth, int L, int C);
1616
int mode_A6(int a, int b);
17-
int choose_test(char *test_mode, int **labyrinth, int L, int C, int a, int b);
17+
int choose_test(char *test_mode, int **labyrinth, int L, int C, int a, int b, int c, int d);
1818
void print_table(int **labyrinth, int L, int C);
19-
void free_labyrinth(int **labyrinth, int C);
20-
void write_output(FILE *fp_out, int result);
19+
void free_labyrinth(int **labyrinth, int L, int C);
20+
void write_output_file(char *name,int result, char *filename);
2121
void str_paste(char *name, char *filename, int num_chars);
2222
int find_last_period(char *filename);
23-
int **allocate_table(int **labyrinth, int C, int L);
24-
char *allocate_outputname(char *out_name, char *in_name);
25-
void error(FILE *fp);
26-
FILE *open_outputfile(FILE *fp_out, char *filename);
27-
FILE *open_inputfile(FILE *fp_in, char *filename);
23+
int **allocate_table(int **labyrinth,int C, int L);
2824
#endif

Read.c

+47-75
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,82 @@
11
#include "Proj_Int.h"
22

33
/**
4-
* @brief
4+
* @brief Function that reads the labyrinth of from the given file and dinamically stores the information contained in it
55
*
66
* @param fp_in
7-
* @param labyrinth
87
* @param filename
8+
* @param test_mode
9+
* @return int**
910
*/
1011

11-
void Read_input_file(char *filename)
12+
int **Read_input_file(FILE *fp_in, int **labyrinth, char *filename)
1213
{
13-
// int **traversed_path = NULL;
14-
//int *size_traversed = NULL;
15-
int x = 0, y = 0, v = 0, count = 0, P = 0, result = 0; //P= number of grey/black cells, (x,y) = grey/black cells coordinates, v=value of respective cell
16-
int C = 0, L = 0, a = 0, b = 0, c = 0, d = 0; // C=columns, L=lines, (a,b)=coordinates of the cell we want to analyse, P= number of grey/black cells
17-
int **labyrinth = NULL;
18-
char test_mode[3];
19-
char *out_name = NULL; //name of the output file
14+
15+
int **traversed_path = NULL;
16+
int *size_traversed = NULL;
2017

21-
FILE *fp_in = NULL, *fp_out = NULL;
2218

23-
fp_in = open_inputfile(fp_in, filename);
2419

25-
out_name = allocate_outputname(out_name, filename);
26-
fp_out = open_outputfile(fp_out, out_name);
27-
while ((fscanf(fp_in, "%d %d", &L, &C)) == 2)
20+
21+
int x = 0, y = 0, v = 0, count = 0, P = 0, result = 0; //P= number of grey/black cells, (x,y) = grey/black cells coordinates, v=value of respective cell
22+
int C = 0, L = 0, a = 0, b = 0, c = 0, d = 0, num_char = 0; // C=columns, L=lines, (a,b)=coordinates of the cell we want to analyse, P= number of grey/black cells
23+
char *test_mode = (char *)calloc(3, sizeof(char));
24+
25+
//meter numa funcao
26+
num_char = find_last_period(filename);
27+
char *name = (char *)calloc((num_char + 5), sizeof(char));
28+
if (name == NULL)
2829
{
29-
if ((fscanf(fp_in, "%d %d %s", &a, &b, test_mode)) != 3)
30-
error(fp_in);
30+
fprintf(stderr, "Not enough memory available!\n");
31+
exit(5);
32+
}
33+
strncpy(name, filename, num_char);
34+
strcat(name, "sol1");
3135

36+
if ((fp_in = fopen(filename, "r")) == NULL) //inicio da leitura do ficheiro
37+
{
38+
printf("Error when reading the input file1.\n");
39+
fclose(fp_in);
40+
exit(EXIT_FAILURE);
41+
}
42+
while ((fscanf(fp_in, "%d %d", &L, &C)) != EOF)
43+
{
44+
if ((fscanf(fp_in, "%d %d %s", &a, &b, test_mode)) != 3)
45+
{
46+
printf("Error reading data from input file2.\n");
47+
exit(0);
48+
}
3249
if (strcmp(test_mode, "A6") == 0)
3350
if ((fscanf(fp_in, "%d %d", &c, &d)) != 2)
34-
error(fp_in);
35-
36-
labyrinth = allocate_table(labyrinth, C, L); // Dynamic allocation of the main labyrinth
51+
{
52+
printf("Error reading data from input file3.\n");
53+
exit(0);
54+
}
55+
// Dynamic allocation of the main labyrinth
56+
labyrinth = allocate_table(labyrinth, C, L);
3757

3858
if ((fscanf(fp_in, "%d", &P)) != 1)
3959
{
40-
free_labyrinth(labyrinth, C);
41-
error(fp_in);
60+
printf("Error reading data from input file.\n");
61+
exit(0);
4262
}
4363
while ((fscanf(fp_in, "%d %d %d", &x, &y, &v) == 3) && (count < P))
4464
{
4565
labyrinth[x - 1][y - 1] = v; //remember that the coordinate (0,0) represents the (1,1) cell
4666
count++;
4767
}
4868
//print_table(labyrinth, L, C);
49-
/* result = choose_test(test_mode, labyrinth, L, C, a, b, c, d, traversed_path, size_traversed);
69+
result = choose_test(test_mode, labyrinth, L, C, a, b, c, d);
5070
write_output_file(name, result, filename);
51-
free_labyrinth(labyrinth, L, C);*/
52-
result = choose_test(test_mode, labyrinth, L, C, a, b);
53-
write_output(fp_out, result);
54-
free_labyrinth(labyrinth, C);
71+
free_labyrinth(labyrinth, L, C);
5572
count = 0;
5673
}
57-
free(out_name);
5874
fclose(fp_in);
59-
fclose(fp_out);
60-
return;
75+
free(test_mode);
76+
return labyrinth;
6177
}
6278

63-
void free_labyrinth(int **labyrinth, int C)
79+
void free_labyrinth(int **labyrinth, int L, int C)
6480
{
6581
int i;
6682
for (i = 0; i < C; i++)
@@ -69,48 +85,4 @@ void free_labyrinth(int **labyrinth, int C)
6985
}
7086
free(labyrinth);
7187
return;
72-
}
73-
74-
char *allocate_outputname(char *out_name, char *in_name)
75-
{
76-
int num_char = 0;
77-
num_char = find_last_period(in_name);
78-
out_name = (char *)calloc((num_char + 5), sizeof(char));
79-
if (out_name == NULL)
80-
{
81-
fprintf(stderr, "Not enough memory available!\n");
82-
exit(5);
83-
}
84-
strncpy(out_name, in_name, num_char);
85-
strcat(out_name, "sol1");
86-
87-
return out_name;
88-
}
89-
90-
void error(FILE *fp)
91-
{
92-
printf("Error when reading the input file.\n");
93-
fclose(fp);
94-
exit(0);
95-
}
96-
97-
FILE *open_inputfile(FILE *fp_in, char *filename)
98-
{
99-
if ((fp_in = fopen(filename, "r")) == NULL)
100-
{
101-
printf("Error when reading the input file.\n");
102-
exit(1);
103-
}
104-
return fp_in;
105-
}
106-
107-
FILE *open_outputfile(FILE *fp_out, char *filename)
108-
{
109-
if ((fp_out = fopen(filename, "a")) == NULL)
110-
{
111-
printf("Error when writing the output file.\n");
112-
free(filename);
113-
exit(1);
114-
}
115-
return fp_out;
11688
}

0 commit comments

Comments
 (0)