/* ------------------- * Vote Counter * Matthew Eastman * meastm1@lsu.edu * ------------------- */ #define FAKEIT #include #include #include #include #define TRUE (1 == 1) #define FALSE (0 == 1) #define REQUIRE_ELECTION_DAY FALSE /* november is 10 months past january */ #define NOVEMBER 10 /* tuesday is 2 days past sunday */ #define TUESDAY 2 /* election days fall between the 2nd and the 8th because they fall on the * first tuesday after the first monday of the month */ #define FIRST_VALID_DOM 2 #define LAST_VALID_DOM 8 int isElectionDay(time_t *now); int main () { int input; time_t now; unsigned long bush = 0, kerry = 0, nader = 0, other = 0; /* lookup the current time and make sure that it's an election day * (if it needs to be) */ time(&now); if (!isElectionDay(&now) && REQUIRE_ELECTION_DAY) { fprintf(stderr, "Error: today is not the day of the election\n"); exit(EXIT_FAILURE); } /* read each vote and increment the count for that candidate */ while (EOF != (input = getchar())) { /* make sure that we don't have a pregnant chad */ if (!isspace(input)) { if ('B' == input) bush++; else if ('K' == input) kerry++; else if ('N' == input) nader++; else other++; } } /* display the results */ printf("Kerry %lu\n", kerry); printf("Bush %lu\n", bush); printf("Nader %lu\n", nader); printf("Other %lu\n", other); /* ok; we're done */ return EXIT_SUCCESS; } int isElectionDay(time_t *givenTime) { struct tm givenDay; time_t currentTime; struct tm currentDay; struct tm tempDay; int good = TRUE; /* once any tests fail, drop this to false */ /* make sure we're using pacific time */ putenv("TZ=US/Pacific"); tzset(); /* start off by retrieving the date information we were given */ localtime_r(givenTime, &givenDay); /* if the month is november, the day of the week is tuesday, and the * date is between the 2nd and 8th (inclusive), it's an election day */ if ((NOVEMBER == givenDay.tm_mon) && (TUESDAY == givenDay.tm_wday) && (FIRST_VALID_DOM <= givenDay.tm_mday) && (LAST_VALID_DOM >= givenDay.tm_mday)) { /* it might be arguable as to which day midnight belongs, so we'll * test again to see what day yester-second belongs to */ givenTime--; localtime_r(givenTime, &tempDay); if (FIRST_VALID_DOM > tempDay.tm_mday) good = FALSE; /* now restore givenTime in case calling function wants to reuse it */ *givenTime = mktime(&givenDay); /* now make sure we have the right year */ time(¤tTime); localtime_r(¤tTime, ¤tDay); if (givenDay.tm_year != currentDay.tm_year) good = FALSE; } else { good = FALSE; } return good; }