bool vote(int voter, int rank, string name)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(name, candidates[i].name) == 0)
{
preferences[voter][rank] = i;
// printf("%i\n", preferences[voter][rank]);
return true;
}
}
return false;
}
这里最关键的是给preferences[][] = i .
i代表了一个候选人。
void tabulate(void)
{
// TODO
for (int i = 0; i < voter_count; i++)
{
for (int j = 0; j < candidate_count; j++)
{
if (candidates[preferences[i][j]].eliminated == false)
{
candidates[preferences[i][j]].votes++;
break;
}
}
}
return;
}
为还在候选序列的候选人统计得票数。关键点是将preferences[][]当成candidates[]的参数。