#include <stdio.h>
#include <string.h>
struct Candidate {
char name[50];
int votes;
};
struct HashTable {
struct Candidate candidates[1000];
};
int hashFunction(const char* name) {
return strlen(name) % 1000;
}
void vote(struct HashTable* hashTable, const char* candidateName) {
int index = hashFunction(candidateName);
if (hashTable->candidates[index].votes > 0 && strcmp(hashTable->candidates[index].name, candidateName) == 0) {
printf("重复投票:%s\n", candidateName);
} else {
strcpy(hashTable->candidates[index].name, candidateName);
hashTable->candidates[index].votes++;
printf("投票成功:%s\n", candidateName);
}
}
void printResults(const struct HashTable* hashTable) {
printf("投票结果:\n");
for (int i = 0; i < 1000; i++) {
if (hashTable->candidates[i].votes > 0) {
printf("%s: %d票\n", hashTable->candidates[i].name, hashTable->candidates[i].votes);
}
}
}
int main() {
struct HashTable election;
memset(&election, 0, sizeof(election));
vote(&election, "C1");
vote(&election, "C2");
vote(&election, "C2");
vote(&election, "C3");
printResults(&election);
return 0;
}