// 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 char Name[PITCHERS][25]; //Array of 20 (PITCHERS) names, each name up to 25 characters float ERA[PITCHERS]; //ERA for each Pitcher int Wins[PITCHERS]; //Wins for each Pitcher int Losses[PITCHERS]; //Wins for each Pitcher int Appearances[PITCHERS]; //Wins for each Pitcher int Saves[PITCHERS]; //Wins for each Pitcher float InningsPitched[PITCHERS]; //Wins for each Pitcher int Hits[PITCHERS]; //Wins for each Pitcher int Runs[PITCHERS]; //Wins for each Pitcher int Homeruns[PITCHERS]; //Wins for each Pitcher int AtBats[PITCHERS]; //Wins for each Pitcher float BattingAverage[PITCHERS]; //Wins for each Pitcher int pitchers = 0; //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; number_of_pitchers = get_data_from_file(); 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 (( fptr = fopen("WCPitchingStats.txt", "r") ) == NULL) printf("Bad File or File Name\n\n"); else { while(!feof(fptr)) { fscanf(fptr, "%s%f%d%d%d%d%f%d%d%d%d%f", Name[i],&ERA[i],&Wins[i],&Losses[i],&Appearances[i], &Saves[i],&InningsPitched[i],&Hits[i],&Runs[i],&Homeruns[i],&AtBats[i],&BattingAverage[i]); i++; } } fclose(fptr); printf("Number of Pitchers is %d\n\n",i-2); return(i-2); } void print_menu_screen(void) { 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; char name_of_max_hr_pitcher[25] = "Nobody"; for(i = 1; i< p; i++) if (Homeruns[i] > maxhr) { maxhr = Homeruns[i]; max_hr_pitcher = i; strncpy(name_of_max_hr_pitcher, Name[i], 25); } printf("\n\nThe Maximum Number of Home Runs was %d by %s\n\n", maxhr, name_of_max_hr_pitcher); return; } void get_best_ERA(int p) { int i, best_era_pitcher; float best_era = 99999.9; char name_of_best_era_pitcher[25] = "Nobody"; for(i = 1; i< p; i++) if (ERA[i] < best_era) { best_era = ERA[i]; best_era_pitcher = i; strncpy(name_of_best_era_pitcher, Name[i], 25); } printf("\n\nThe Best ERA was %f by %s\n\n", best_era, name_of_best_era_pitcher); return; } void get_best_WperIP_ratio(int p) { int i, best_WperIP_pitcher; float best_WperIP_ratio = 0.0; char name_of_best_WperIP_ratio_pitcher[25] = "Nobody"; for(i = 1; i< p; i++) if ((float)Wins[i]/InningsPitched[i] > best_WperIP_ratio) { best_WperIP_ratio = (float)Wins[i]/InningsPitched[i]; best_WperIP_pitcher = i; strncpy(name_of_best_WperIP_ratio_pitcher, Name[i], 25); } printf("\n\nThe Best W/L ratio was %f by %s\n\n", best_WperIP_ratio, name_of_best_WperIP_ratio_pitcher); return; }