Commit c0ab3042 by jobrod

Update Task 1 -week 2 Jordan Brodie

parent e03b7249
Showing with 107 additions and 124 deletions
......@@ -3,194 +3,177 @@
#include <string.h>
#define CHAR_LEN 20
#define NUM_CARS 10
#define NUM_OWNERS 5
#define NUM_PEOPLE 10
/// Define a struct to store car information
/// Define a struct to store personal 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;
char firstName[CHAR_LEN];
char lastName[CHAR_LEN];
char personalCode[CHAR_LEN];
char matrixNumber[CHAR_LEN];
} person_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*);
void fileNameFunction(char*, int);
FILE* openFileFunction(char*);
int readData(FILE*, person_t[], int);
void sortData(person_t[], int);
void displaySortedData(person_t[], int);
void writeOutput(person_t[], int, const char*);
void printFileStatus(int);
int main(void)
{
FILE *carFile, *ownerFile;
car_t cars[NUM_CARS];
owner_t owners[NUM_OWNERS];
car_t mostPowerfulCar, oldestCar, newestCar;
FILE *inputFile;
char inputName[CHAR_LEN];
person_t persons[NUM_PEOPLE];
/// Read car data from the input file
carFile = fopen("cars.txt", "r");
if (carFile == NULL)
/// Attempt to open the input file
inputFile = openFileFunction(inputName);
if (inputFile == NULL)
{
printErrorOpeningFile("car data");
printFileStatus(0);
return 1;
}
int numCars = readCarData(carFile, cars, NUM_CARS);
fclose(carFile);
printFileStatus(1);
/// 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);
/// Read data from the input file
int records = readData(inputFile, persons, NUM_PEOPLE);
/// Find the most powerful, oldest, and newest car
findMostPowerfulCar(cars, numCars, &mostPowerfulCar);
findOldestCar(cars, numCars, &oldestCar);
findNewestCar(cars, numCars, &newestCar);
fclose(inputFile);
/// Display the results
displayResults(mostPowerfulCar, oldestCar, newestCar);
/// Sort the data
sortData(persons, records);
/// Write the results to the output file
writeResultsToFile(mostPowerfulCar, oldestCar, newestCar, "cars_output.txt");
/// Display the sorted data
displaySortedData(persons, records);
/// Write the sorted data to the output file
writeOutput(persons, records, "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)
/// Prompt the user for the file name
void fileNameFunction(char *inputName, int attempt)
{
char buffer[512];
int counter = 0;
while (fgets(buffer, 512, carFile) && counter < maxCars)
printf("What is the file name, %d attempts?\n", attempt);
scanf("%s", inputName);
}
/// 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,
cars[counter].numberPlate, &cars[counter].yearOfIssue, cars[counter].cubature,
&cars[counter].personalIdentificationNumber);
counter++;
fileNameFunction(inputName, attempt);
inputFile = fopen(inputName, "r");
if (inputFile != NULL)
{
return inputFile;
}
return counter;
attempt--;
} while (attempt != 0);
return NULL;
}
/// Read owner data from the input file and store it in the owners array
int readOwnerData(FILE *ownerFile, owner_t owners[], int maxOwners)
/// Read data from the input file and store it in the persons array
int readData(FILE *inputFile, person_t persons[], int maxRecords)
{
char buffer[512];
int counter = 0;
while (fgets(buffer, 512, ownerFile) && counter < maxOwners)
while (fgets(buffer, 512, inputFile) && counter < maxRecords)
{
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++;
}
return counter;
}
/// Find the most powerful car based on cubature
void findMostPowerfulCar(car_t cars[], int numCars, car_t *mostPowerfulCar)
/// Sort the data by personal code
void sortData(person_t persons[], int records)
{
*mostPowerfulCar = cars[0];
for (int i = 1; i < numCars; i++)
for (int i = 0; i < records - 1; i++)
{
for (int j = i + 1; j < records; j++)
{
if (strcmp(cars[i].cubature, mostPowerfulCar->cubature) > 0)
if (strcmp(persons[i].personalCode, persons[j].personalCode) > 0)
{
*mostPowerfulCar = cars[i];
person_t temp = persons[i];
persons[i] = persons[j];
persons[j] = temp;
}
}
}
}
/// Find the oldest car based on year of issue
void findOldestCar(car_t cars[], int numCars, car_t *oldestCar)
/// Display the sorted data on the screen
void displaySortedData(person_t persons[], int records)
{
*oldestCar = cars[0];
for (int i = 1; i < numCars; i++)
printf("\nNumber of records is %d\n", records);
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
void findNewestCar(car_t cars[], int numCars, car_t *newestCar)
{
*newestCar = cars[0];
for (int i = 1; i < numCars; i++)
printf("\nSorted Women:\n");
for (int i = 0; i < records; 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
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)
/// Write the sorted data to an output file
void writeOutput(person_t persons[], int records, const char *outputFileName)
{
FILE *outputFile = fopen(outputFileName, "w");
if (outputFile == NULL)
{
printErrorOpeningFile("output");
printf("Error opening output file.\n");
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, "Men:\n");
for (int i = 0; i < records; i++)
{
if (persons[i].personalCode[0] == '1')
{
fprintf(outputFile, "%s %s %s %s\n", persons[i].firstName, persons[i].lastName, persons[i].personalCode, persons[i].matrixNumber);
}
}
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);
fprintf(outputFile, "\nWomen:\n");
for (int i = 0; i < records; i++)
{
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);
}
/// Print error opening file message
void printErrorOpeningFile(const char *fileType)
/// Print file status message
void printFileStatus(int status)
{
printf("Error opening %s file.\n", fileType);
if (status == 1)
{
printf("\nFile exists.\n\n");
}
else
{
printf("File does not exist.\n");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment