-
Notifications
You must be signed in to change notification settings - Fork 0
/
example01.c
210 lines (172 loc) · 5.36 KB
/
example01.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 25
/*
* Past examination 18/02/2016 (version D)
* today date: 12/03/2021
* my signature: 49dbffdad5428e0a32ba850a6e5e0d45a550c70ef110a748058feb78590ca4dd
*/
typedef struct {
unsigned row;
unsigned column;
float weight;
} Find;
unsigned importMatrix(FILE*, float***);
unsigned importFinds(FILE*, Find**);
unsigned recoverFinds(Find*, float**, float, float, unsigned, unsigned);
void printUsage();
void my_open(FILE**, char*);
void *my_malloc(size_t);
void free_all(Find*, float**, unsigned);
int main(int argc, char **argv) {
Find *finds;
float **map, maxPressure, maxWeight;
unsigned numberOfFinds, N;
FILE *mapFile = NULL, *findsFile = NULL;
char mapName[MAX_LEN], findName[MAX_LEN];
if(argc >= 5){
strcpy_s(mapName, _countof(mapName) * sizeof(char), argv[1]);
strcpy_s(findName, _countof(findName) * sizeof(char), argv[2]);
sscanf_s(argv[3], "%f", &maxPressure);
sscanf_s(argv[4], "%f", &maxWeight);
}else{
printUsage();
exit(EXIT_SUCCESS);
}
// Open the files
my_open(&mapFile, mapName);
my_open(&findsFile, findName);
// Import the map
if((N = importMatrix(mapFile, &map))) {
printf("A %uX%u map imported\n", N, N);
} else {
printf("Can't import the map. Abort.\n");
exit(EXIT_FAILURE);
}
// Import the finds
if((numberOfFinds = importFinds(findsFile, &finds))) {
printf("%u finds in the area\n", numberOfFinds);
} else {
printf("No finds in the area\n");
}
// Start the recovering operation
if (numberOfFinds) {
printf("\n%u finds recovered\n", recoverFinds(finds, map, maxPressure, maxWeight, numberOfFinds, N));
}
free_all(finds, map, N);
exit(EXIT_SUCCESS);
}
void printUsage(){
printf("\nUsage: ./program mapName findsName maxPressure maxWeight\n\n");
}
unsigned importMatrix(FILE* file, float ***matrix){
unsigned N = 1;
float **ptr;
int c;
// Get the size
rewind(file);
while ((c = fgetc(file)) != 10 || c == EOF){
if (c == 32){
N++;
}
}
rewind(file);
// Allocate the memory
ptr = (float **) my_malloc(sizeof(float*) * N);
*matrix = ptr;
for (int i = 0; i < N; ++i) {
ptr[i] = (float *) my_malloc(sizeof(float) * N);
}
// Import the values
for (int i = 0; i < N; ++i) {
if (!fscanf_s(file, "%f%*c", &ptr[i / N][i % N])){
printf("\nMap file with a wrong format. Abort.");
exit(EXIT_FAILURE);
}
}
return N;
}
unsigned importFinds(FILE *file, Find **finds){
unsigned N = 1;
int c;
Find *ptr;
// Get the size
rewind(file);
while ((c = fgetc(file)) != EOF){
if (c == 10) N++;
}
rewind(file);
// Allocate the memory
ptr = (Find *) my_malloc(sizeof(Find) * N);
*finds = ptr;
// Import the values
for (int i = 0; i < N; ++i) {
if (fscanf_s(file, "%u%*c%u%*c%f%*c", &ptr[i].row, &ptr[i].column, &ptr[i].weight) != 3){
printf("\nFinds file with a wrong format. Abort.");
exit(EXIT_FAILURE);
}
}
return N;
}
unsigned recoverFinds(Find *finds,
float **map,
float maxPressure,
float maxWeight,
unsigned numberOfFinds,
unsigned N){
unsigned dives = 1;
unsigned findsCaught = 0;
float actualWeight = 0;
for(unsigned i = 0; i < numberOfFinds; i++){
printf("\n");
// Check if the find's weight isn't over the maximum weight transportable
if (finds[i].weight > maxWeight) {
printf("Error: too much weight\n\tDive:\t%u\n\tFind:\t%u\n\tWeight:\t%.2f\n", dives, i, finds[i].weight);
goto end;
}
// Check if the find is in the map
if (finds[i].column >= N || finds[i].row >= N){
printf("Error: out of the sea\n\tDive:\t%u\n\tFind:\t%u\n\tX:\t%u\n\tY:\t%u\n",dives, i, finds[i].column, finds[i].row);
goto end;
}
// Check the pressure
if (map[ finds[i].row ][ finds[i].column ] > maxPressure){
printf("Error: too much pressure\n\tDive:\t\t%u\n\tFind:\t\t%u\n\tPressure:\t%.2f\n", dives, i, map[ finds[i].row ][ finds[i].column ]);
goto end;
}
actualWeight += finds[i].weight;
// If we are over the max weight, empty the cargo and carry again the find
if (actualWeight > maxWeight) {
actualWeight = finds[i].weight;
printf("\nNew dive\n");
dives++;
}
findsCaught++;
printf("Find caught\n\tDive:\t\t%d\n\tActual weight:\t%.2f\n\tFind:\t\t%d\n\tX:\t\t%d\n\tY:\t\t%d\n",
dives, actualWeight, i, finds[i].column, finds[i].row);
end:;
}
return findsCaught;
}
void my_open(FILE **fp, char *filename){
if(fopen_s(fp, filename, "r")) {
exit(EXIT_FAILURE);
}
}
void *my_malloc(size_t size){
void *ptr = malloc(size);
if (ptr == NULL){
printf("\nMemory allocation error. Abort.");
exit(EXIT_FAILURE);
}
return ptr;
}
void free_all(Find *finds, float **map, unsigned N){
float **ptr = map;
for (int i = 0; i < N; ++i, ptr++) {
free(*ptr);
}
free(map);
free(finds);
}