CS50 复数性——难以理解为什么使用 candidate_count

CS50 Plurality – having trouble understanding why candidate_count is used

我试图理解为什么在 CS50 的复数(第 3 周)中使用 candidate_count 而不是选民计数。下面是我的代码。

如果我们想象我们有三个候选人(爱丽丝、鲍勃、查理)并且每次我们遍历 bool 函数或 print_winner 函数时,如果我们有类似的东西,我们会不会错过计票10个选民?根据我的理解,'i' 只会迭代 3 次。我在理解为什么我们不在底部的 print winner 函数中使用 voter_count 时遇到概念性问题。

我仍在努力完善我的代码,因此某些部分可能仍然存在问题。我只是在寻求一些帮助来澄清这个问题的逻辑。

#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;

// 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 (number of arguments - 1 because the first arg is going to be plurality)
    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;
    }

    int 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
void print_winner(void)
{
    int maxvotes = 0;
    
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > maxvotes)
        {
            maxvotes = candidates[i].votes;
        }
    }
    
    for (int i = 0; i < candidate_count; i++)
    {
        printf("the winner is %s\n!", candidates[i].name);
    }

    return;
}

我认为你误解了我们正在循环的内容。 candidate_count 用于循环遍历 candidates,这是一个具有 candidate_count 个元素的数组 .

考虑这个简单的数组-

int arr[] = {1, 2, 3, 4, 5};

我们将循环使用-

for (int i = 0; i < 5; i++)
{
    printf("value at index %d is %d\n", i, arr[i]);
}

注意 i < 5 部分。为什么是5?好吧,因为那是数组的长度 arr,多了我们就会越界读取,少了我们就不会读取整个 arr.

现在把arr换成candidates,也是一个数组,把5换成candidate_count,显然就是说的长度数组.

voteprint_winner 函数中的循环不“计票”,它们 遍历候选人数组 。这就是那些循环的目的。因此,要遍历数组,我们必须使用 index < length_of_array。这正是它的作用。

就是为了解决“到哪里算票?”的问题。快来看看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;
}

此函数在用户输入后触发,假设用户 被要求输入姓名 - 他们选择一些姓名,假设此姓名与候选人匹配。所以 vote 被调用,它 遍历候选数组 。为什么会迭代?它需要找到与用户刚刚提供的姓名同名的候选人。

所以这个循环的目的是遍历数组candidates,而这个循环中的代码的目的是比较每个数组的名称candidate 到用户提供的名称。如果匹配,则增加该候选人的选票

这一行- candidates[i].votes++;

就是votes的计算点。就像增加一个计数器一样简单。

想象一个真实的场景,你就是程序。候选人都站成一排(一个数组)。每个候选人从 0 张牌(选票)开始。假设其中 none 个具有相同的名称。

  • 一位用户告诉您他们想投票给候选人 foo(只是一个名字)。

  • 你再去那一行应聘。你从第一位候选人开始,问他们“你叫什么名字?”。

  • 候选人告诉你他们的名字。

    • 如果他们的名字和用户给你的名字相符,就是这样,你给他们一张代表投票的卡片。现在他们比以前多了一张牌

    • 如果他们的名字不匹配,你转到下一个候选人并重复。

    但是你在哪里停下来?很简单,你在最后一个候选人之前停下来。这最多需要多少次迭代? candidate_count,即站在该行的候选人人数。所以,最坏的情况是,你要找的人排在队伍的最后,所以你必须问 candidate_count 个人才能最终找到你要找的人。 遍历数组.

  • 最后你只要数一数每位考生有多少张牌就知道谁是赢家了。 (.votes)

print_winner 函数应该做同样的事情。虽然你的代码看起来有点歪。

for (int i = 0; i < candidate_count; i++)
{
    if (candidates[i].votes > maxvotes)
    {
        maxvotes = candidates[i].votes;
    }
}

这个循环相当于遍历了整行候选人,询问每个人有多少张牌(票),找出谁的牌(票)最多。

但是你还需要问一下带maxvotes的人的名字!这就是您要找的。

但是你把刚才计算出来的maxvotes完全舍弃了,你再也不用了。

for (int i = 0; i < candidate_count; i++)
{
    printf("the winner is %s\n!", candidates[i].name);
}

该循环将打印每个 candidate 的名称。这相当于遍历整行候选人(数组),问每个人“你叫什么名字?”并大喊“获胜者是[他们的名字]!”。但事实并非如此!

你应该存储候选人的姓名,以及maxvotes然后在第一个循环后打印它-

string winner_name;

...

for (int i = 0; i < candidate_count; i++)
{
    if (candidates[i].votes > maxvotes)
    {
        maxvotes = candidates[i].votes;
        winner_name = candidates[i].name;
    }
}
printf("the winner is %s\n!", candidates[i].name);

当然,这不考虑多个获胜者,如果2个候选人的票数相同怎么办?那是你必须自己尝试的东西。

但我希望这能解答您关于困惑的问题。