//////////////////// /// Vote Counter /// /// Final Ver. /// //////////////////// #include #include #define CANDIDATES 3 char *majorCanNames[CANDIDATES]; unsigned int totalVotes; struct linkedCans; struct linkedCans { unsigned int tally; char *name; struct linkedCans *next; }; void newCan(struct linkedCans *canList, char *name) { if(canList) { struct linkedCans *current; current = canList; while(current->next) { current = current->next; } current = current->next = (struct linkedCans *) malloc(sizeof(struct linkedCans)); if(current) { current->name = name; current->next = NULL; current->tally = 0; } } } unsigned int returnOther(struct linkedCans *canList) { unsigned int otherVotes = 0; if(canList) { struct linkedCans *current; current = canList; while(current) { char isOther = 1; unsigned char i; for(i = 0; i < CANDIDATES; i++) { if(strcmp(current->name,majorCanNames[i]) == 0) { isOther = 0; break; } } if(isOther) { otherVotes += current->tally; } current = current->next; } } return otherVotes; } void tallyCan(struct linkedCans *canList, char *name) { totalVotes ++; if(canList) { struct linkedCans *current; current = canList; while(current->next && (strcmp(current->name,name) != 0) ) { current = current->next; } current->tally ++; } } unsigned int getTallyCan(struct linkedCans *canList, char *name) { // unsigned int otherVotes = 0; unsigned int canVotes = 0; if(canList) { struct linkedCans *current; current = canList; while(current) { // char isOther = 1; char isCan = 0; // for(unsigned char i = 0; i < CANDIDATES; i++) { if(strcmp(current->name,name) == 0) { // isOther = 0; isCan = 1; // break; } // } // if(isOther) { // otherVotes += current->tally; // } if(isCan) { canVotes += current->tally; break; } current = current->next; } } // return otherVotes; return canVotes; } int main (unsigned int argc, char *argv[]) { int Input; struct linkedCans tbl[2]; unsigned char i; tbl[0].name = "Other"; tbl[0].tally = 0; tbl[0].next = NULL; tbl[1].name = "0ther"; tbl[1].tally = 0; tbl[1].next = NULL; majorCanNames[0] = "Kerry"; majorCanNames[1] = "Bush"; majorCanNames[2] = "Nader"; for(i = 0; i < CANDIDATES; i++) { newCan(& tbl[(* (majorCanNames[i])) % 2], majorCanNames[i]); } while ((Input=getchar())!=EOF) { unsigned char vote=Input; if (!isspace(vote)){ switch(vote) { case 'K': { // case 'k': { tallyCan(& tbl[vote % 2], "Kerry"); break; } case 'B': { // case 'b': { tallyCan(& tbl[vote % 2], "Bush"); break; } case 'N': { // case 'n': { tallyCan(& tbl[vote % 2], "Nader"); break; } default: { tallyCan(& tbl[vote % 2], "Other"); break; } } } } for(i = 0; i < CANDIDATES; i++) { printf("%s: %d\n", majorCanNames[i], getTallyCan(& tbl[(* (majorCanNames[i])) % 2], majorCanNames[i])); } printf("Other: %d\n", returnOther(& tbl[0]) + returnOther(& tbl[1]) ); return 0; }