CS50 Runoff 程序正在运行,但 check50 表示它没有运行

CS50 Runoff program is working but check50 says it doesn't

所以基本上我的程序应该进行某种决选(在这里你可以看到它是什么:https://cs50.harvard.edu/x/2020/psets/3/runoff/)。我需要实现 6 个功能才能做到这一点,我做到了。我的程序工作得很好,但 check50 说函数 print_winner 不起作用(尽管它只花费 4/24 个可能的点)。如果他拥有所有选票的多数(> 50%),则此功能必须打印选举候选人。该错误仅显示 "print_winner must print name when someone has a majority, print_winner did not print winner of election" 和另外 3 个此类错误。

完整代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cs50.h>
#include <math.h>

#define MAX 9

typedef struct
{
    string name;
    int votes;
    bool eliminated;
}
candidate;

candidate candidates[MAX];

int voter_count;
int candidate_count;
int preferences[MAX][MAX];
float winner_vote;

//void check_preference(void);
bool is_tie(int min);
bool vote(int voter, int rank, string name);
bool print_winner(void);
void tabulate(void);
int find_min(void);
void eliminate(int min);


int main(int argc, string argv[])
{
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    candidate_count = argc - 1;

    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        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: ");

    winner_vote = voter_count / 2;
    int integer = winner_vote;

    if (winner_vote == integer)
    {
        winner_vote++;
    }
    else
    {
        winner_vote = ceil(winner_vote);
    }

    for (int i = 0; i < voter_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {


            string name = get_string("Rank %i ", j + 1);

            if (!vote(i, j, name))

            {
                printf("Invalid vote\n");
                j--;
            }

        }
        printf("\n");
    }


    //check_preference();

    while (true)
    {
        tabulate();

        if (print_winner())
        {
            return 1;
        }
        else if (is_tie(find_min()))
        {
            return 1;
        }
        else
        {
            eliminate(find_min());
        }

    }
}


void eliminate(int min)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].eliminated)
        {
            continue;
        }
        else if (min == candidates[i].votes)
        {
            candidates[i].eliminated = true;
        }
    }

}




int find_min(void)
{
    int min = MAX;
    for (int i = 1; i < candidate_count; i++)
    {
        if (candidates[i].eliminated)
        {
            continue;
        }
        else if (min > candidates[i].votes)
        {
            min = candidates[i].votes;
        }
    }

    return min;



}


bool is_tie(int min)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].eliminated)
        {
            continue;
        }
        else if (min == candidates[i].votes)
        {
            continue;
        }
        else
        {
            return 0;
        }
    }
    return 1;
}




bool vote(int voter, int rank, string name)
{
    bool exist = false;



    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(name, candidates[preferences[voter][i]].name) == 0 && rank > 0)
        {
            return 0;
        }

        if (strcmp(name, candidates[i].name) == 0)
        {

            preferences[voter][rank] = i;
            exist = true;
            break;
        }
    }

    return exist;
}







bool print_winner(void)
{
    // candidate candidateHolder;
    for (int f = 0; f < candidate_count; f++)
    {
        //candidateHolder = candidates[f];
        if (candidates[f].votes >= winner_vote)
        {
            printf("%s\n", candidates[f].name);
            return 1;
        }

    }
    return 0;
}

void tabulate(void)
{
    int check = 0;
    for (int i = 0; i < voter_count; i++)
    {
        if (!candidates[preferences[i][check]].eliminated)
        {
            candidates[preferences[i][check]].votes++;
            check = 0;
        }
        else
        {
            check++;
            i--;
        }
    }
}

这里是产生错误的函数:

bool print_winner(void)
{
    // candidate candidateHolder;
    for (int f = 0; f < candidate_count; f++)
    {
        //candidateHolder = candidates[f];
        if (candidates[f].votes >= winner_vote)
        {
            printf("%s\n", candidates[f].name);
            return 1;
        }

    }
    return 0;
}

我在函数中声明了 winner_vote,它起作用了

问题是它将我的函数放在了他们的代码中。所以 winner_vote 变量在那里未定义。与 1 或 0 无关。顺便说一句,bool 中的 1 表示 true,0 表示 false。为什么我用它们代替文字,因为解释视频说 return 1 if ... 和 return 0 if ... 我想也许这会改变一些东西。

我的另一个问题是我忘记将我的答案标记为正确答案。所以人们仍在努力提供帮助

你好像忘记了 "If any candidate has a majority (more than 50%) of the first preference votes, that candidate is declared the winner of the election."

用于确定 winner_vote(又名)的变量大多数必须是浮点数,即 voter_count / 2。整数将不起作用,如果你必须除以 3/2 或 5/2

此外,为了赢得多数票,候选人[f].votes 必须是 > 多数票,而不是 >=。在后一种情况下,两个候选人之间的平局将在 print_winner 函数中打印出来。关系在代码后面的 is_tie 函数中确定。

布尔函数的 return 值必须为 true 或 false。

这里的问题是您的 printer_winner 会 return 0 或 1。 check50 需要 TRUE 或 FALSE。

一条简单的语句将为您解决这个问题:

printf("%s", winner_vote ? "true" : "false");

这会将 1 和 0 转换为 TRUE 和 FALSE。 希望这可以帮助。第一次在这里做程序员 ;).

您可以初始化两个全局变量,一个是整数,另一个是浮点数(int max; float pmax;),并在开始时删除不必要的代码和变量。

下面你的功能内容:

bool print_winner(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (max < candidates[i].votes)
        {
            max = candidates[i].votes;
            pmax = ((float) max /(float) voter_count);
            if (pmax > 0.5)
            {
                printf("%s\n", candidates[i].name);
                return true;
            }
        }
    }
    return false;
}
bool print_winner(void)
{
    int maj = voter_count / 2;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > maj)
        {
            printf("%s\n",candidates[i].name);
            return true;
        }
    }
    return false;
}

你为什么不试试这个呢?? 当指定的类型是 bool 时,为什么你 return 一个 int?

您也可以使用它作为替代方案

 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;
    }
    // Print the winner of the election, if there is one
bool print_winner(void)
{
    int won;
    win_score = voter_count / 2;
    won = win_score;
    if (win_score == won)
    {
        win_score++;
    }
    else
    {
        win_score = ceil(win_score);
    }
    //  for each candidate sum the 1st choices
    // if candidates[i].votes > voters / 2; win then quit
    // else call find_min then is_tie if so everyone wins else eliminate min
    // then tabulate next rank to candidate/s
    for (int j = 0; j < candidate_count; j++)
    {
        if (candidates[j].votes >= win_score)
        {
            printf("%s\n", candidates[j].name);
            return 1;
        }
    }
    return 0;
}