/* floridavote.c */ /* (c) 2004 Philip Willoughby */ #include #include /* '@' is before 'A' in the ascii character set. These macros help us * treat vote letters as 1-26 instead of 65-90, which makes debugging * easier */ #define BUSH ('B' - '@') #define KERRY ('K' - '@') #define NADER ('N' - '@') /* To add a new candidate, give them a macro appropriate to their letter * in the form used above, and add them to the switch statement below */ int main (int argc, char **argv) { int l; /* I prefer to use one counter per candidate rather than a larger * array, as it is more space-efficient */ unsigned long BushVotes = 0; unsigned long KerryVotes = 0; unsigned long NaderVotes = 0; unsigned long OtherVotes = 0; while ((l = getchar()) != EOF) { if (isspace(l)) continue; /* Invalid form, go to next */ l -= '@'; /* Normalise the vote character to 1-26 */ switch (l) { case NADER: NaderVotes += 1; /* it's a vote for Mr Nader */ continue; /* Go to the next vote */ case KERRY: KerryVotes += 1; /* it's a vote for Mr Kerry */ continue; /* Go to the next vote */ case BUSH: BushVotes += l; /* it's a vote for Mr Bush */ continue; /* Go to the next vote */ } OtherVotes += 1; /* it's a vote for a no-hoper */ } /* print the votes out */ printf("Kerry %lu\n",KerryVotes); printf("Bush %lu\n",BushVotes); printf("Nader %lu\n",NaderVotes); printf("Other %lu\n",OtherVotes); return 0; }