CS50 多个,在平局的情况下不打印多个获胜者

CS50 plurality, does not print multiple winners in case of tie

我使用 check50 检查了我的代码,除一个条件外,所有条件都满足,print_winner() 函数在平局的情况下不打印多个获胜者,我不知道为什么不正在工作。

这些是我的结果:

:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:) print_winner identifies Alice as winner of election
:) print_winner identifies Bob as winner of election
:) print_winner identifies Charlie as winner of election
:( print_winner prints multiple winners in case of tie
    print_winner function did not print both winners of election
:) print_winner prints all names when all candidates are tied

我应该在以下代码中修复什么?

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

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct {
    string name;
    int votes;
} candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

int voter_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

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

    // Populate array of candidates
    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;
    }

    voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++) {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name)) {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name) {
    for (int i = 0; i < candidate_count; i++) {
        if (strcmp(candidates[i].name, name) == 0) {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
string x;

void print_winner(void) {
    for (int i = 0; i < candidate_count - 1; i++) {
        for (int j = i + 1; j < candidate_count; j++) {
            if (candidates[j].votes < candidates[i].votes) {
                // swapping elements
                x = candidates[i].name;
                candidates[i].name = candidates[j].name;
                candidates[j].name = x;
            }
        }
    }
    
    for (int k = 0; k < candidate_count; k++) {
        if (candidates[k].votes == candidates[candidate_count - 1].votes) {
            printf("%s\n", candidates[k].name);
        }
    }
    return;
}

您的排序代码只对候选人姓名进行排序,而不对候选人投票进行排序。请注意,不需要按票数对候选人进行排序,您可以确定最大票数并打印所有具有该票数的候选人。为了保持一致,该函数应重命名为 print_winners()。同时删除全局变量 x,无论如何它应该是 print_winner() 的局部变量。

这是修改后的版本:

void print_winners(void) {
    int max_votes = candidates[0].votes;
    for (int i = 1; i < candidate_count; i++) {
        if (max_votes < candidates[i].votes) {
            max_votes = candidates[i].votes;
        }
    }
    for (int i = 0; i < candidate_count; i++) {
        if (candidates[i].votes == max_votes) {
            printf("%s\n", candidates[i].name);
        }
    }
}