C语言 制表函数不适用于多个候选消除和多个首选项



我有一个C文件,我正在工作,这是基于运行的系统。但是,我遇到了一些问题。

我的制表函数只有在每轮有一名候选人被淘汰时才能将每位候选人的正确票数制表,但当多人被淘汰或处理多个偏好时,无法将每位候选人的正确票数制表。

这是我的代码。

#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];
// Candidates have name, vote count, eliminated status
typedef struct
{
string name;
int votes;
bool eliminated;
}
candidate;
// Array of candidates
candidate candidates[MAX_CANDIDATES];
// Numbers of voters and candidates
int voter_count;
int candidate_count;
// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: runoff [candidate ...]n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX_CANDIDATES)
{
printf("Maximum number of candidates is %in", MAX_CANDIDATES);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
candidates[i].eliminated = false;
}
voter_count = get_int("Number of voters: ");
if (voter_count > MAX_VOTERS)
{
printf("Maximum number of voters is %in", MAX_VOTERS);
return 3;
}
// Keep querying for votes
for (int i = 0; i < voter_count; i++)
{
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
// Record vote, unless it's invalid
if (!vote(i, j, name))
{
printf("Invalid vote.n");
return 4;
}
}
printf("n");
}
// Keep holding runoffs until winner exists
while (true)
{
// Calculate votes given remaining candidates
tabulate();
// Check if election has been won
bool won = print_winner();
if (won)
{
break;
}
// Eliminate last-place candidates
int min = find_min();
bool tie = is_tie(min);
// If tie, everyone wins
if (tie)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
printf("%sn", candidates[i].name);
}
}
break;
}
// Eliminate anyone with minimum number of votes
eliminate(min);
// Reset vote counts back to zero
for (int i = 0; i < candidate_count; i++)
{
candidates[i].votes = 0;
}
}
return 0;
}
// Record preference if vote is valid (done and checked)
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i++)
{
if(strcmp(candidates[i].name, name) == 0)
{
preferences[voter][rank] = i;
return true;
}
}
return false;
}
// Tabulate votes for non-eliminated candidates (done)
void tabulate(void)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
for (int j = 0; j < voter_count; j++)
{
if (!candidates[i].eliminated)
{
if (preferences[j][0] == i)
{
candidates[i].votes++;
}
}
}
}
}
// Print the winner of the election, if there is one (done)
bool print_winner(void)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
float percent = ((float) candidates[i].votes / (float) voter_count);
if (percent > 0.5 && !candidates[i].eliminated)
{
printf("%sn", candidates[i].name);
return true;
}
}
return false;
}
// Return the minimum number of votes any remaining candidate has (done)
int find_min(void)
{
// TODO
int min = 0;
for (int i = 0; i < candidate_count; i++)
{
if(!candidates[i].eliminated)
{
min = candidates[i].votes;
break;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes < min && !candidates[i].eliminated)
{
min = candidates[i].votes;
}
}
return min;
}
// Return true if the election is tied between all candidates, false otherwise (done)
bool is_tie(int min)
{
// TODO
int count_votes = 0;
int count_false = 0;
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
count_false++;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
count_votes = count_votes + candidates[i].votes;
}
}
double average = (double) count_votes / (double) count_false;

for(int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == average && !candidates[i].eliminated)
{
return true;
}
}
return false;
}
// Eliminate the candidate (or candidates) in last place (done)
void eliminate(int min)
{
// TODO
int candidate_index;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == min)
{
candidate_index = i;
candidates[i].eliminated = true;
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}
for (int i = 0; i < voter_count; i++)
{
if(preferences[i][0] == candidate_index)
{
for (int j = 1; j < candidate_count; j++)
{
preferences[i][j-1] = preferences[i][j];
}
}
}

return;
}

代码应该能够处理多个候选人淘汰和多个首选项,就像在eliminate()中一样,我已经对其进行了编码,使第一首选项preferences[voter_count][0]始终是最新的,并且永远不会有被淘汰的候选人的投票作为第一首选项。然而,当我使用cs50检查器检查我的工作时,它声称我的函数不能处理多个消除,也不能处理多个首选项。

经过思考和尝试其他方法后,我在tabulate()中想出了一个循环来计算选票,这样如果选民的第一个偏好被淘汰,将会有另一个循环来查找选民的下一个未被淘汰的候选人偏好。cs50 check抛出了与之前相同的错误,现在补充说,当只有一个人被淘汰时,我无法计算出每个人的正确票数。

代码如下:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];
// Candidates have name, vote count, eliminated status
typedef struct
{
string name;
int votes;
bool eliminated;
}
candidate;
// Array of candidates
candidate candidates[MAX_CANDIDATES];
// Numbers of voters and candidates
int voter_count;
int candidate_count;
// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: runoff [candidate ...]n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX_CANDIDATES)
{
printf("Maximum number of candidates is %in", MAX_CANDIDATES);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
candidates[i].eliminated = false;
}
voter_count = get_int("Number of voters: ");
if (voter_count > MAX_VOTERS)
{
printf("Maximum number of voters is %in", MAX_VOTERS);
return 3;
}
// Keep querying for votes
for (int i = 0; i < voter_count; i++)
{
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
// Record vote, unless it's invalid
if (!vote(i, j, name))
{
printf("Invalid vote.n");
return 4;
}
}
printf("n");
}
// Keep holding runoffs until winner exists
while (true)
{
// Calculate votes given remaining candidates
tabulate();
// Check if election has been won
bool won = print_winner();
if (won)
{
break;
}
// Eliminate last-place candidates
int min = find_min();
bool tie = is_tie(min);
// If tie, everyone wins
if (tie)
{
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
printf("%sn", candidates[i].name);
}
}
break;
}
// Eliminate anyone with minimum number of votes
eliminate(min);
// Reset vote counts back to zero
for (int i = 0; i < candidate_count; i++)
{
candidates[i].votes = 0;
}
}
return 0;
}
// Record preference if vote is valid (done and checked)
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i++)
{
if(strcmp(candidates[i].name, name) == 0)
{
preferences[voter][rank] = i;
return true;
}
}
return false;
}
// Tabulate votes for non-eliminated candidates (done)
void tabulate(void)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
for (int j = 0; j < voter_count; j++)
{
if (!candidates[i].eliminated)
{
if (preferences[j][0] == i)
{
candidates[i].votes++;
}
}
else
{
for (int k = 1; k < candidate_count; k++)
{
int e = preferences[j][k];
if (!candidates[e].eliminated)
{
candidates[e].votes++;
break;
}
}
}
}
}
}
// Print the winner of the election, if there is one (done)
bool print_winner(void)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
float percent = ((float) candidates[i].votes / (float) voter_count);
if (percent > 0.5 && !candidates[i].eliminated)
{
printf("%sn", candidates[i].name);
return true;
}
}
return false;
}
// Return the minimum number of votes any remaining candidate has (done)
int find_min(void)
{
// TODO
int min = 0;
for (int i = 0; i < candidate_count; i++)
{
if(!candidates[i].eliminated)
{
min = candidates[i].votes;
break;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes < min && !candidates[i].eliminated)
{
min = candidates[i].votes;
}
}
return min;
}
// Return true if the election is tied between all candidates, false otherwise (done)
bool is_tie(int min)
{
// TODO
int count_votes = 0;
int count_false = 0;
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
count_false++;
}
}
for (int i = 0; i < candidate_count; i++)
{
if (!candidates[i].eliminated)
{
count_votes = count_votes + candidates[i].votes;
}
}
double average = (double) count_votes / (double) count_false;

for(int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == average && !candidates[i].eliminated)
{
return true;
}
}
return false;
}
// Eliminate the candidate (or candidates) in last place (done)
void eliminate(int min)
{
// TODO
int candidate_index;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == min)
{
candidate_index = i;
candidates[i].eliminated = true;
}
}
return;
}

我自己测试了代码,仍然不能弄清楚为什么修改后的代码不能处理单个消除,特别是因为我用一个简单的用例测试了两个代码,而一个候选被消除,它给出了预期的响应。

有人能看到如果我做错了什么,并给我指针如何得到关于这个问题?

我也有同样的问题…

我通过改变我的表格函数来解决这个问题。

在我的例子中,我忘记在循环内连接候选变量。在新的解决方案中,我生成了一个嵌套的for。

void tabulate(void)
{
int i = 0;
int j = 0;
for (i = 0; i < voter_count; i++)
{
for (j = 0; j < candidate_count; j++)
{
int id = preferences[i][j];
if (!candidates[id].eliminated)
{
candidates[id].votes++;
break;
}
}
}
}

最新更新