// Statistics.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "string.h" #define PITCHERS 20 #define NO 0 #define YES 1 //Globally Defined Variables, in a STRUCTURE struct pitcher_stats{ char Name[25]; float ERA; int Wins; int Losses; int Appearances; int Saves; float InningsPitched; int Hits; int Runs; int Homeruns; int AtBats; float BattingAverage; }; pitcher_stats Pitcher[25]; //Array of 25 "pitcher_status" Structures int pitchers = 0; //There are no pitchers until we read data from a file... //Function Prototype Statements int get_data_from_file(void); void print_menu_screen(void); void get_most_home_runs(int p); void get_best_ERA(int p); void get_best_WperIP_ratio(int p); int _tmain(int argc, _TCHAR* argv[]) { int option, continue_key; char c, cc; int number_of_pitchers; //Get the Pitching Stat's from the data file number_of_pitchers = get_data_from_file(); //Process the user's menu choices... do{ print_menu_screen(); scanf("%d", &option); switch(option){ case 1: get_most_home_runs(number_of_pitchers); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 2: get_best_ERA(number_of_pitchers); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 3: get_best_WperIP_ratio(number_of_pitchers); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 0: break; default: break; } } while (option != 0); return (0); } int get_data_from_file(void){ FILE *fptr; int i = 1; //If the file isn't there, or has a bad format, don't read data from it if (( fptr = fopen("WCPitchingStats.txt", "r") ) == NULL) printf("Bad File or File Name\n\n"); //...but if the file opens without errors, read the data from it until the End Of File (EOF) else { while(!feof(fptr)) { fscanf(fptr, "%s%f%d%d%d%d%f%d%d%d%d%f", Pitcher[i].Name, &Pitcher[i].ERA, &Pitcher[i].Wins, &Pitcher[i].Losses, &Pitcher[i].Appearances, &Pitcher[i].Saves, &Pitcher[i].InningsPitched, &Pitcher[i].Hits, &Pitcher[i].Runs, &Pitcher[i].Homeruns, &Pitcher[i].AtBats, &Pitcher[i].BattingAverage); i++; } } fclose(fptr); return(i-2); } void print_menu_screen(void) { //This is the screen display for the menu. Print it to the screen. int i; for(i=1; i<40; i++) printf("\n"); printf("Menu of a lot of Statistics functions...\n\n"); printf("Option Action\n"); printf("------ ------\n"); printf(" 1 What was the most homeruns \n"); printf(" 2 Get Best ERA \n"); printf(" 3 Get Best Wins per Innings ratio \n"); printf(" 0 End This Program \n\n\n"); printf("Enter option 1 ... 2, or 0 to STOP\n\n"); return; } void get_most_home_runs(int p) { int i, maxhr=0, max_hr_pitcher; // Starting from assuming the Maximum number of Homeruns is 0, // check the homerun stats for each pitcher. If he has one greater // than the current maximum value, raise this maximum value to his // number of homeruns ... he's has the new maximum. for(i = 1; i< p; i++) if (Pitcher[i].Homeruns > maxhr) { maxhr = Pitcher[i].Homeruns; //Update the Maximum max_hr_pitcher = i; //Update the index of the maximum Homerun Pitcher } printf("\n\nThe Maximum Number of Home Runs was %d by %s\n\n", maxhr, Pitcher[max_hr_pitcher].Name); return; } void get_best_ERA(int p) { int i, best_era_pitcher; float best_era = 99999.9; // Starting from assuming the Minimum ERA is 99999.9 (ie: very high), // check the ERA stats for each pitcher. If he has an ERA less than // than the current minimum value, lower this minimum value to his // ERA ... he's has the new minimum. for(i = 1; i< p; i++) if (Pitcher[i].ERA < best_era) { best_era = Pitcher[i].ERA; //Update the minimum ERA best_era_pitcher = i; //Update the index of the minimum ERA pitcher } printf("\n\nThe Best ERA was %f by %s\n\n", best_era, Pitcher[best_era_pitcher].Name); return; } void get_best_WperIP_ratio(int p) { int i, best_WperIP_pitcher; float best_WperIP_ratio = 0.0; // Starting from assuming the Maximum number of Wins/Innings is 0, // check the Wins and Innings Pitched stats for each pitcher, and compute their ratio. // If the current pitcher has one greater than the current maximum value, // raise this maximum value to his Wins/Innings ratio. for(i = 1; i< p; i++) if ((float)Pitcher[i].Wins/Pitcher[i].InningsPitched > best_WperIP_ratio) { best_WperIP_ratio = (float)Pitcher[i].Wins / Pitcher[i].InningsPitched; //Update the maximum W/Inn Ratio best_WperIP_pitcher = i; //Update the index of the best Wins/Innings pitcher } printf("\n\nThe Best W/L ratio was %f by %s\n\n", best_WperIP_ratio, Pitcher[best_WperIP_pitcher].Name); return; }