/////////////////////////////// /// Vote5 /// /// David A. Wheeler /// /// http://www.dwheeler.com /// /////////////////////////////// #include #include #include // During compilation, use "-DFANCYFORMAT=1" // if you want fancy formatting, for example, label the top as the // "Unofficial" or "OFFICIAL" result depending on the date. int main () { int Input; unsigned long total=0; unsigned long Tally[256]={0}; time_t current_time; // Used for header, if any time_t election_time; // When are election counts valid? int is_official = 0; // By default, assume it's not official. #ifdef FANCYFORMAT const int fancyformat = TRUE; #else const int fancyformat = 0; #endif struct tm election_time_struct = { // man ctime(3) for more. 0, // tm_sec - seconds 0, // tm_min - minutes 0, // tm_hour - hours 0, // tm_mday - day of the month 10, // tm_mon - month; 0=January, 10=November (not 11). 104, // tm_year - year; # years since 1900. 104 = 2004AD. 2, // tm_wday - day of the week; Tuesday = 2 (ignored) 0, // tm_yday - day in the year (ignored) 0, // tm_isdst - daylight saving time }; time(¤t_time); election_time = mktime(&election_time_struct); // To print out current time, do this: // printf("Time = %s\n", asctime(&election_time_struct)); is_official = ( current_time >= election_time ); while ((Input=getchar())!=EOF) { unsigned char Vote=Input; if (!isspace(Vote)){ Tally[Input]+=1; total+=1; } } if (is_official) { if (fancyformat) { printf("OFFICIAL RESULTS\n"); printf(" KERRY: %d\n",Tally['K']); printf(" BUSH: %d\n",Tally['B']); printf(" NADER: %d\n",Tally['N']); printf(" OTHER: %d\n",total-Tally['K']-Tally['B']-Tally['N']); } else { printf("Kerry: %d\n",Tally['K']); printf("Bush: %d\n",Tally['N']); printf("Nader: %d\n",Tally['B']); printf("Other: %d\n",total-Tally['K']-Tally['B']-Tally['N']); } } else { if (fancyformat) { printf("Unofficial Results\n"); printf(" Kerry: %d\n",Tally['K']); printf(" Bush: %d\n",Tally['B']); printf(" Nader: %d\n",Tally['N']); printf(" Other: %d\n",total-Tally['K']-Tally['B']-Tally['N']); } else { printf("Kerry: %d\n",Tally['K']); printf("Bush: %d\n",Tally['B']); printf("Nader: %d\n",Tally['N']); printf("Other: %d\n",total-Tally['K']-Tally['B']-Tally['N']); } } return 0; }