/*
* FILE : m4.cpp
* PROJECT : SENG1000 - Major #4
* PROGRAMMER : Aryan Marediya
* FIRST VERSION : 2023-11-29
* DESCRIPTION : The program calulates the program to generate sport team win-loss records from game results in files..
*/
#include
#include
#include
#pragma warning(disable: 4996)
//Function prototypes
int parseLine(char* gameResult, char* opponentName, int* primaryScore, int* opponentScore);
int processGames(char* filename);
int main() {
FILE* teamsFile = fopen("teams.txt", "r");
if (teamsFile == NULL) {
perror("Error opening teams.txt");
return 1;
}
char teamFilename[100] = { 0 };
while (fgets(teamFilename, sizeof(teamFilename), teamsFile) != NULL) {
//Remove the newline character at the end
int len = strlen(teamFilename);
if (len > 0 && teamFilename[len - 1] == '\n') {
teamFilename[len - 1] = '\0';
}
//Process games for each team
int status = processGames(teamFilename);
if (status != 0) {
printf("Error processing %s\n", teamFilename);
}
printf("\n");
}
fclose(teamsFile);
return 0;
}
//
// FUNCTION : parseLine
// DESCRIPTION : Parse a line containing game results and extract opponent name, primary score, and opponent score.
//
// PARAMETERS :
// gameResult: String containing the game result to be parsed.
// opponentName: Output parameter to store the opponent's name.
// primaryScore: Output parameter to store the primary team's score.
// opponentScore: Output parameter to store the opponent's score.
//
// RETURNS :
// Returns 0 on success, 1 on failure.
//
int parseLine(char* gameResult, char* opponentName, int* primaryScore, int* opponentScore) {
char* comma = gameResult;
while (*comma != ',' && *comma != '\0') {
comma++;
}
if (*comma != ',') {
printf("Error: Missing opponent name\n");
return 1;
}
//Copy opponent name
strncpy(opponentName, gameResult, comma - gameResult);
opponentName[comma - gameResult] = '\0';
char* dash = comma + 1;
while (*dash != '-' && *dash != '\0') {
dash++;
}
if (*dash != '-') {
printf("Error: Missing primary score\n");
return 1;
}
// Parse primary score
*primaryScore = atoi(comma + 1);
// Find the newline position
char* newline = dash + 1;
while (*newline != '\n' && *newline != '\0') {
newline++;
}
if (*newline != '\n') {
printf("Error: Missing opponent score\n");
return 1;
}
// Parse opponent score
*opponentScore = atoi(dash + 1);
return 0;
}
//
// FUNCTION : processGames
// DESCRIPTION : Process a file containing game results, calculate statistics, and print the season result.
//
// PARAMETERS :
// filename: Name of the file containing game results.
//
// RETURNS :
// Returns 0 on success, 1 on failure.
//
int processGames(char* filename) {
FILE* gameFile = fopen(filename, "r");
if (gameFile == NULL) {
perror("Error opening game file");
return 1;
}
printf("Processing %s:\n", filename);
int wins = 0, losses = 0, ties = 0;
char gameResult[100] = { '\0' }; //Initialize with null characters
while (fgets(gameResult, sizeof(gameResult), gameFile) != NULL) {
//Remove the newline character at the end
int len = strlen(gameResult);
if (len > 0 && gameResult[len - 1] == '\n') {
gameResult[len - 1] = '\0';
}
char opponentName[50] = { '\0' }; //Initialize with null characters
int primaryScore = 0, opponentScore = 0;
int status = parseLine(gameResult, opponentName, &primaryScore, &opponentScore);
if (status == 0) {
printf("\t");
//Determine and print the outcome of each game
if (primaryScore > opponentScore) {
printf("%s beat %s %d-%d\n", filename, opponentName, primaryScore, opponentScore);
wins++;
}
else if (primaryScore < opponentScore) {
printf("%s lost to %s %d-%d\n", filename, opponentName, primaryScore, opponentScore);
losses++;
}
else {
printf("%s and %s tied at %d\n", filename, opponentName, primaryScore);
ties++;
}
}
}
//Print season result if there are recorded games
if (wins + losses + ties > 0) {
double winningPercentage = (2.0 * wins + ties) / (2.0 * (wins + losses + ties));
printf("Season result for %s: %.3f (%d-%d-%d)\n", filename, winningPercentage, wins, losses, ties);
}
fclose(gameFile);
return 0;
}
0 Comments