Commit c0ab3042 by jobrod

Update Task 1 -week 2 Jordan Brodie

parent e03b7249
Showing with 107 additions and 124 deletions
...@@ -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 /// Attempt to open the input file
carFile = fopen("cars.txt", "r"); inputFile = openFileFunction(inputName);
if (carFile == NULL) if (inputFile == NULL)
{ {
printErrorOpeningFile("car data"); printFileStatus(0);
return 1; return 1;
} }
int numCars = readCarData(carFile, cars, NUM_CARS); printFileStatus(1);
fclose(carFile);
/// Read owner data from the input file /// Read data from the input file
ownerFile = fopen("owners.txt", "r"); int records = readData(inputFile, persons, NUM_PEOPLE);
if (ownerFile == NULL)
{
printErrorOpeningFile("owner data");
return 1;
}
readOwnerData(ownerFile, owners, NUM_OWNERS);
fclose(ownerFile);
/// Find the most powerful, oldest, and newest car fclose(inputFile);
findMostPowerfulCar(cars, numCars, &mostPowerfulCar);
findOldestCar(cars, numCars, &oldestCar);
findNewestCar(cars, numCars, &newestCar);
/// 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 owners array /// Read data from the input file and store it in the persons array
int readOwnerData(FILE *ownerFile, owner_t owners[], int maxOwners) int readData(FILE *inputFile, person_t persons[], int maxRecords)
{ {
char buffer[512]; char buffer[512];
int counter = 0; 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++; counter++;
} }
return counter; return counter;
} }
/// Find the most powerful car based on cubature /// Sort the data by personal code
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++) {
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 /// 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)
{ {
printErrorOpeningFile("output"); printf("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 printErrorOpeningFile(const char *fileType) 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