Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
jobrod
/
week2 - Jordan brodie
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c0ab3042
authored
Feb 18, 2025
by
jobrod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update Task 1 -week 2 Jordan Brodie
parent
e03b7249
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
109 additions
and
126 deletions
Task 1 -week 2 Jordan Brodie
Task 1 -week 2 Jordan Brodie
View file @
c0ab3042
...
@@ -3,194 +3,177 @@
...
@@ -3,194 +3,177 @@
#include <string.h>
#include <string.h>
#define CHAR_LEN 20
#define CHAR_LEN 20
#define NUM_CARS 10
#define NUM_PEOPLE 10
#define NUM_OWNERS 5
/// Define a struct to store
car
information
/// Define a struct to store
personal
information
typedef struct
typedef struct
{
{
char mark[CHAR_LEN];
char firstName[CHAR_LEN];
char model[CHAR_LEN];
char lastName[CHAR_LEN];
char numberPlate[CHAR_LEN];
char personalCode[CHAR_LEN];
int yearOfIssue;
char matrixNumber[CHAR_LEN];
char cubature[CHAR_LEN];
} person_t;
int personalIdentificationNumber;
} car_t;
/// Define a struct to store car owner information
typedef struct
{
int personalIdentificationNumber;
char name[CHAR_LEN];
} owner_t;
/// Function prototypes
/// Function prototypes
int readCarData(FILE*, car_t[], int);
void fileNameFunction(char*, int);
int readOwnerData(FILE*, owner_t[], int);
FILE* openFileFunction(char*);
void findMostPowerfulCar(car_t[], int, car_t*);
int readData(FILE*, person_t[], int);
void findOldestCar(car_t[], int, car_t*);
void sortData(person_t[], int);
void findNewestCar(car_t[], int, car_t*);
void displaySortedData(person_t[], int);
void displayResults(car_t, car_t, car_t);
void writeOutput(person_t[], int, const char*);
void writeResultsToFile(car_t, car_t, car_t, const char*);
void printFileStatus(int);
void printErrorOpeningFile(const char*);
int main(void)
int main(void)
{
{
FILE *carFile, *ownerFile;
FILE *inputFile;
car_t cars[NUM_CARS];
char inputName[CHAR_LEN];
owner_t owners[NUM_OWNERS];
person_t persons[NUM_PEOPLE];
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
///
Attempt to open
the input file
ownerFile = fopen("owners.txt", "r"
);
inputFile = openFileFunction(inputName
);
if (
owner
File == NULL)
if (
input
File == NULL)
{
{
print
ErrorOpeningFile("owner data"
);
print
FileStatus(0
);
return 1;
return 1;
}
}
readOwnerData(ownerFile, owners, NUM_OWNERS);
printFileStatus(1);
fclose(ownerFile);
///
Find the most powerful, oldest, and newest car
///
Read data from the input file
findMostPowerfulCar(cars, numCars, &mostPowerfulCar);
int records = readData(inputFile, persons, NUM_PEOPLE);
findOldestCar(cars, numCars, &oldestCar);
f
indNewestCar(cars, numCars, &newestCar
);
f
close(inputFile
);
///
Display the results
///
Sort the data
displayResults(mostPowerfulCar, oldestCar, newestCar
);
sortData(persons, records
);
/// Write the results to the output file
/// Display the sorted data
writeResultsToFile(mostPowerfulCar, oldestCar, newestCar, "cars_output.txt");
displaySortedData(persons, records);
/// Write the sorted data to the output file
writeOutput(persons, records, "output.txt");
return 0;
return 0;
}
}
///
Read car data from the input file and store it in the cars array
///
Prompt the user for the file name
int readCarData(FILE *carFile, car_t cars[], int maxCars
)
void fileNameFunction(char *inputName, int attempt
)
{
{
char buffer[512];
printf("What is the file name, %d attempts?\n", attempt);
int counter = 0;
scanf("%s", inputName);
while (fgets(buffer, 512, carFile) && counter < maxCars)
}
/// Attempt to open the file and return the file pointer
FILE* openFileFunction(char *inputName)
{
FILE *inputFile;
int attempt = 3;
do
{
{
sscanf(buffer, "%s %s %s %d %s %d", cars[counter].mark, cars[counter].model,
fileNameFunction(inputName, attempt);
cars[counter].numberPlate, &cars[counter].yearOfIssue, cars[counter].cubature,
inputFile = fopen(inputName, "r");
&cars[counter].personalIdentificationNumber);
if (inputFile != NULL)
counter++;
{
}
return inputFile;
return counter;
}
attempt--;
} while (attempt != 0);
return NULL;
}
}
/// Read
owner data from the input file and store it in the owner
s array
/// Read
data from the input file and store it in the person
s array
int read
OwnerData(FILE *ownerFile, owner_t owners[], int maxOwner
s)
int read
Data(FILE *inputFile, person_t persons[], int maxRecord
s)
{
{
char buffer[512];
char buffer[512];
int counter = 0;
int counter = 0;
while (fgets(buffer, 512,
ownerFile) && counter < maxOwner
s)
while (fgets(buffer, 512,
inputFile) && counter < maxRecord
s)
{
{
sscanf(buffer, "%d %s", &owners[counter].personalIdentificationNumber, owners[counter].name);
sscanf(buffer, "%s %s %s %s", persons[counter].firstName, persons[counter].lastName,
persons[counter].personalCode, persons[counter].matrixNumber);
counter++;
counter++;
}
}
return counter;
return counter;
}
}
///
Find the most powerful car based on cubatur
e
///
Sort the data by personal cod
e
void
findMostPowerfulCar(car_t cars[], int numCars, car_t *mostPowerfulCar
)
void
sortData(person_t persons[], int records
)
{
{
*mostPowerfulCar = cars[0];
for (int i = 0; i < records - 1; i++)
for (int i = 1; i < numCars; i++)
{
{
if (strcmp(cars[i].cubature, mostPowerfulCar->cubature) > 0
)
for (int j = i + 1; j < records; j++
)
{
{
*mostPowerfulCar = cars[i];
if (strcmp(persons[i].personalCode, persons[j].personalCode) > 0)
{
person_t temp = persons[i];
persons[i] = persons[j];
persons[j] = temp;
}
}
}
}
}
}
}
///
Find the oldest car based on year of issue
///
Display the sorted data on the screen
void
findOldestCar(car_t cars[], int numCars, car_t *oldestCar
)
void
displaySortedData(person_t persons[], int records
)
{
{
*oldestCar = cars[0];
printf("\nNumber of records is %d\n", records);
for (int i = 1; i < numCars; i++)
printf("\nSorted Men:\n");
for (int i = 0; i < records; i++)
{
{
if (
cars[i].yearOfIssue < oldestCar->yearOfIssue
)
if (
persons[i].personalCode[0] == '1'
)
{
{
*oldestCar = cars[i]
;
printf("%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber)
;
}
}
}
}
}
/// Find the newest car based on year of issue
printf("\nSorted Women:\n");
void findNewestCar(car_t cars[], int numCars, car_t *newestCar)
for (int i = 0; i < records; i++)
{
*newestCar = cars[0];
for (int i = 1; i < numCars; i++)
{
{
if (
cars[i].yearOfIssue > newestCar->yearOfIssue
)
if (
persons[i].personalCode[0] == '2'
)
{
{
*newestCar = cars[i]
;
printf("%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber)
;
}
}
}
}
}
}
/// Display the results on the screen
/// Write the sorted data to an output file
void displayResults(car_t mostPowerfulCar, car_t oldestCar, car_t newestCar)
void writeOutput(person_t persons[], int records, const char *outputFileName)
{
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");
FILE *outputFile = fopen(outputFileName, "w");
if (outputFile == NULL)
if (outputFile == NULL)
{
{
print
ErrorOpeningFile("output
");
print
f("Error opening output file.\n
");
return;
return;
}
}
fprintf(outputFile, "Most Powerful Car:\n");
fprintf(outputFile, "Men:\n");
fprintf(outputFile, "Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
for (int i = 0; i < records; i++)
mostPowerfulCar.mark, mostPowerfulCar.model, mostPowerfulCar.numberPlate,
{
mostPowerfulCar.yearOfIssue, mostPowerfulCar.cubature, mostPowerfulCar.personalIdentificationNumber);
if (persons[i].personalCode[0] == '1')
{
fprintf(outputFile, "\nOldest Car:\n");
fprintf(outputFile, "%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
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, "\nWomen:\n");
fprintf(outputFile, "Mark: %s, Model: %s, Number Plate: %s, Year: %d, Cubature: %s, Owner ID: %d\n",
for (int i = 0; i < records; i++)
newestCar.mark, newestCar.model, newestCar.numberPlate, newestCar.yearOfIssue,
{
newestCar.cubature, newestCar.personalIdentificationNumber);
if (persons[i].personalCode[0] == '2')
{
fprintf(outputFile, "%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
}
}
fclose(outputFile);
fclose(outputFile);
}
}
/// Print
error opening file
message
/// Print
file status
message
void print
ErrorOpeningFile(const char *fileType
)
void print
FileStatus(int status
)
{
{
printf("Error opening %s file.\n", fileType);
if (status == 1)
{
printf("\nFile exists.\n\n");
}
else
{
printf("File does not exist.\n");
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment