我已经尝试了多种方法来执行bool is_tie函数中的代码;但是,我似乎总是在check50中收到相同的错误。
就是说,我使用下面的功能运行该程序,使用的是最高候选人,10个投票者和7个淘汰候选人或2个候选人,没有任何问题,程序会打印出2个并列的候选人,而没有任何问题。
我已经通过debug50运行了代码,在消除了另外两个后,通过显示并列候选的名称,它似乎可以正常工作;但是,在check50工具中,以上错误继续出现。
我还使用了printf函数来显示函数中的所有值,而且它们似乎都正确。
我还在下面的is_tie函数中使用过其他{istie = false;};但是,我在下面收到的错误与在错误下方得到的代码相同。
** :( is_tie在某些候选人被淘汰后检测到平局-is_tie没有返回true **
bool is_tie(int min)
{
int ties = 0;
bool istie = false;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == min && candidates[i].eliminated == false)
{
ties++;
int elim_cands = candidate_count - candidates_out;
if (ties == candidate_count || ties == elim_cands)
{
istie = true;
break;
}
else
{
istie = false;
}
}
}
return istie;
}
简单地说,我已经能够使用下面的代码消除错误;但是,我只收到这两个错误。
:(当未绑定选举时,is_tie返回falseis_tie没有返回false:( is_tie当只有一些候选者被捆绑时返回falseis_tie没有返回false
bool is_tie(int min)
{
int ties = 0;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == min && candidates[i].eliminated == false)
{
ties++;
return true;
}
}
return false;
}
[如果您有任何想法,请告诉我可以尝试的方法。我整天都在处理此错误,尽管该程序对我有用,但我无法提交该错误。您的想法将不胜感激。
所有其他功能均正常工作,问题集中的主要代码与最初提供的代码相同。我做到了但是,请添加一个名为候选人数的全局变量,以跟踪已淘汰的候选人数。
这里是主要程序
#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;
int candidates_out = 0;
// 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 %i\n", 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 %i\n", 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("%s\n", 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;
}
** SPOLER ALERT-以下是与程序相关的功能代码:
[布尔投票(int选民,int等级,字符串名称)(void)制表(void)bool print_winner(void)bool is_tie(int min)int find_min(void)消除(int min)
bool vote(int voter, int rank, string name)
{
for (int number = 0; number < candidate_count; number++)
{
if (strcmp(name, candidates[number].name) == 0)
{
preferences[voter][rank] = number;
return true;
}
}
return false;
}
// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
for (int j = 0; j < 1; j++) //loop to record each voters rank 1
{
for (int i = 0; i < voter_count; i++)
{
int number = preferences[i][j];
if (candidates[number].eliminated == false) //if candidate is not eliminated, add 1 to candidates votes
{
candidates[number].votes += 1;
j = 0;
}
else //if candidate is eliminated, go to the same voters next preference
{
i--;
j++;
}
}
}
}
// Print the winner of the election, if there is one
bool print_winner(void)
{
float majority = (float)voter_count / 2;
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > majority)
{
printf("%s\n", candidates[i].name);
return true;
}
}
return false;
}
// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
int min = candidates[0].votes;
for (int i = 1; i < candidate_count; i++)
{
if (min > candidates[i].votes && candidates[i].eliminated == false)
{
min = candidates[i].votes;
int Position = i;
}
}
return min;
}
// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes == min)
{
candidates[i].eliminated = true;
candidates_out++;
}
}
}
问题的根源:
添加一个称为候选人的全局变量,以跟踪已淘汰候选人的数量。
来自the spec:
。在商业环境中,不这样做可能会转化为真钱。您不应该在
plurality.c
中修改其他任何内容>check50使用它自己的魔法来运行测试并验证结果。 IMO在该课程中学习的重要技能之一是遵循规范[[to the letter