/*------------------------------------------------------------------------------ * problem003.c: Visualize the playcalling **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #define _ISOC99_SOURCE #include #include #include #include /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define NPLAYTYPES 2 #define NDIRECTIONS 3 #define NYARDAGES 10 const char *PLAYTYPES[NPLAYTYPES] = { "Run", "Pass" }; const char *DIRECTIONS[NDIRECTIONS] = { "Left", "Middle", "Right" }; const char *YARDAGES[NYARDAGES] = { "00 - 09", "10 - 19", "20 - 29", "30 - 39", "40 - 49", "50 - 59", "60 - 69", "70 - 79", "80 - 89", "90 - 99" }; #define BUFFER_SIZE 8 #define BAR_WIDTH 60.0 /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ int get_string_index(const char *s, const char **sv, int ns) { int i; for (i = 0; i < ns; i++) if (strcmp(s, sv[i]) == 0) return i; return -1; } /*----------------------------------------------------------------------------*/ #define get_playtype(s) get_string_index((s), PLAYTYPES, NPLAYTYPES) #define get_directions(s) get_string_index((s), DIRECTIONS, NDIRECTIONS) #define chomp(s) (s)[strlen(s) - 1] = 0; /*----------------------------------------------------------------------------*/ void print_histogram(const char *title, const char *fields[], int *values, int nfields, int nplays) { int i, x; printf("%s:\n", title); for (i = 0; i < nfields; i++) { printf(" %-8s|", fields[i]); for (x = 0; x < (int)round(values[i]*BAR_WIDTH/nplays); x++) putchar('x'); for (; x < BAR_WIDTH; x++) putchar(' '); puts("|"); } } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { int playtypes[NPLAYTYPES]; int directions[NDIRECTIONS]; int yardages[NYARDAGES]; int nplays; char playtype[BUFFER_SIZE]; char direction[BUFFER_SIZE]; double yardage; memset(playtypes, 0, sizeof(int)*NPLAYTYPES); memset(directions, 0, sizeof(int)*NDIRECTIONS); memset(yardages, 0, sizeof(int)*NYARDAGES); nplays = 0; while (scanf("%s %s %lf", (char*)&playtype, direction, &yardage) == 3) { chomp(playtype); chomp(direction); playtypes[get_playtype(playtype)]++; directions[get_directions(direction)]++; if (yardage < 0.0) yardage = 0; yardages[(int)floor(yardage) / 10]++; nplays++; } print_histogram("Play Type", PLAYTYPES, playtypes, NPLAYTYPES, nplays); putchar('\n'); print_histogram("Direction", DIRECTIONS, directions, NDIRECTIONS, nplays); putchar('\n'); print_histogram("Yardage", YARDAGES, yardages, NYARDAGES, nplays); return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=c **----------------------------------------------------------------------------*/