Task 1.0 -week 2 Jordan Brodie
6.2 KB
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
#include <stdio.h>
#include <string.h>
#define CHAR_LEN 20
#define NUM_CARS 10
#define NUM_OWNERS 5
/// Define a struct to store car information
typedef struct
{
char mark[CHAR_LEN];
char model[CHAR_LEN];
char numberPlate[CHAR_LEN];
int yearOfIssue;
char cubature[CHAR_LEN];
int personalIdentificationNumber;
} car_t;
/// Define a struct to store car owner information
typedef struct
{
int personalIdentificationNumber;
char name[CHAR_LEN];
} owner_t;
/// Function prototypes
int readCarData(FILE*, car_t[], int);
int readOwnerData(FILE*, owner_t[], int);
void findMostPowerfulCar(car_t[], int, car_t*);
void findOldestCar(car_t[], int, car_t*);
void findNewestCar(car_t[], int, car_t*);
void displayResults(car_t, car_t, car_t);
void writeResultsToFile(car_t, car_t, car_t, const char*);
void printErrorOpeningFile(const char*);
int main(void)
{
FILE *carFile, *ownerFile;
car_t cars[NUM_CARS];
owner_t owners[NUM_OWNERS];
car_t mostPowerfulCar, oldestCar, newestCar;
/// Read car data from the input file
carFile = fopen("cars.txt", "r");
if (carFile == NULL)
{
printErrorOpeningFile("car data");
return 1;
}
int numCars = readCarData(carFile, cars, NUM_CARS);
fclose(carFile);
/// Read owner data from the input file
ownerFile = fopen("owners.txt", "r");
if (ownerFile == NULL)
{
printErrorOpeningFile("owner data");
return 1;
}
readOwnerData(ownerFile, owners, NUM_OWNERS);
fclose(ownerFile);
/// Find the most powerful, oldest, and newest car
findMostPowerfulCar(cars, numCars, &mostPowerfulCar);
findOldestCar(cars, numCars, &oldestCar);
findNewestCar(cars, numCars, &newestCar);
/// Display the results
displayResults(mostPowerfulCar, oldestCar, newestCar);
/// Write the results to the output file
writeResultsToFile(mostPowerfulCar, oldestCar, newestCar, "cars_output.txt");
return 0;
}
/// Read car data from the input file and store it in the cars array
int readCarData(FILE *carFile, car_t cars[], int maxCars)
{
char buffer[512];
int counter = 0;
while (fgets(buffer, 512, carFile) && counter < maxCars)
{
sscanf(buffer, "%s %s %s %d %s %d", cars[counter].mark, cars[counter].model,
cars[counter].numberPlate, &cars[counter].yearOfIssue, cars[counter].cubature,
&cars[counter].personalIdentificationNumber);
counter++;
}
return counter;
}
/// Read owner data from the input file and store it in the owners array
int readOwnerData(FILE *ownerFile, owner_t owners[], int maxOwners)
{
char buffer[512];
int counter = 0;
while (fgets(buffer, 512, ownerFile) && counter < maxOwners)
{
sscanf(buffer, "%d %s", &owners[counter].personalIdentificationNumber, owners[counter].name);
counter++;
}
return counter;
}
/// Find the most powerful car based on cubature
void findMostPowerfulCar(car_t cars[], int numCars, car_t *mostPowerfulCar)
{
*mostPowerfulCar = cars[0];
for (int i = 1; i < numCars; i++)
{
if (strcmp(cars[i].cubature, mostPowerfulCar->cubature) > 0)
{
*mostPowerfulCar = cars[i];
}
}
}
/// Find the oldest car based on year of issue
void findOldestCar(car_t cars[], int numCars, car_t *oldestCar)
{
*oldestCar = cars[0];
for (int i = 1; i < numCars; i++)
{
if (cars[i].yearOfIssue < oldestCar->yearOfIssue)
{
*oldestCar = cars[i];
}
}
}
/// Find the newest car based on year of issue
void findNewestCar(car_t cars[], int numCars, car_t *newestCar)
{
*newestCar = cars[0];
for (int i = 1; i < numCars; i++)
{
if (cars[i].yearOfIssue > newestCar->yearOfIssue)
{
*newestCar = cars[i];
}
}
}
/// Display the results on the screen
void displayResults(car_t mostPowerfulCar, car_t oldestCar, car_t newestCar)
{
printf("Most Powerful Car:\n");
printf("Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
mostPowerfulCar.mark, mostPowerfulCar.model, mostPowerfulCar.numberPlate,
mostPowerfulCar.yearOfIssue, mostPowerfulCar.cubature, mostPowerfulCar.personalIdentificationNumber);
printf("\nOldest Car:\n");
printf("Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
oldestCar.mark, oldestCar.model, oldestCar.numberPlate, oldestCar.yearOfIssue,
oldestCar.cubature, oldestCar.personalIdentificationNumber);
printf("\nNewest Car:\n");
printf("Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
newestCar.mark, newestCar.model, newestCar.numberPlate, newestCar.yearOfIssue,
newestCar.cubature, newestCar.personalIdentificationNumber);
}
/// Write the results to an output file
void writeResultsToFile(car_t mostPowerfulCar, car_t oldestCar, car_t newestCar, const char *outputFileName)
{
FILE *outputFile = fopen(outputFileName, "w");
if (outputFile == NULL)
{
printErrorOpeningFile("output");
return;
}
fprintf(outputFile, "Most Powerful Car:\n");
fprintf(outputFile, "Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
mostPowerfulCar.mark, mostPowerfulCar.model, mostPowerfulCar.numberPlate,
mostPowerfulCar.yearOfIssue, mostPowerfulCar.cubature, mostPowerfulCar.personalIdentificationNumber);
fprintf(outputFile, "\nOldest Car:\n");
fprintf(outputFile, "Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
oldestCar.mark, oldestCar.model, oldestCar.numberPlate, oldestCar.yearOfIssue,
oldestCar.cubature, oldestCar.personalIdentificationNumber);
fprintf(outputFile, "\nNewest Car:\n");
fprintf(outputFile, "Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
newestCar.mark, newestCar.model, newestCar.numberPlate, newestCar.yearOfIssue,
newestCar.cubature, newestCar.personalIdentificationNumber);
fclose(outputFile);
}
/// Print error opening file message
void printErrorOpeningFile(const char *fileType)
{
printf("Error opening %s file.\n", fileType);
}