繁体   English   中英

pset3 复数 - 候选人投票未更新 - CS50

[英]pset3 plurality - candidates votes are not being updated - CS50

我试图完成 CS50 的 pset3,复数并且我的代码不会更新每个候选人的投票。 我已经实现了投票 function,但它似乎不起作用。 你知道我需要在哪里改进我的代码吗?

这是我目前的代码:

#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
    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)
{
    // TODO
    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)
{
    // TODO
    int highest_vote = 0;
    string winner;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes >= candidates[0].votes)
        {
            highest_vote = candidates[i].votes;
            winner = candidates[i].name;
        }
    }
    printf("%s\n", winner);
    return;
}

INPUT:

./plurality a b c d
Number of voters: 5
Vote: a
Vote: a
Vote: b
Vote: b
Vote: c

OUTPUT:

b


我已经使用 debug50 查看问题出在哪里,但我无法找到解决方案。 有没有人也做过 pset3 并且有任何有用的想法?

你的问题在这里:

bool vote(string name)
{
    // TODO
    // LOOK OUT HERE FOR THE MISTAKE
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++ ;
            return true;
        }
        return false;  // <<<<<<<<<<<<<<<<<< ERROR!
    }
}

如果您在第一次迭代期间没有找到匹配项,您将从 function 返回,并且bob永远不会得到改变来计算一些选票。 您需要在循环之后移动第二个return

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

还有一个问题:

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

首先:索引应该从我评论中提到的 0 开始。 第二:您必须更新highest_vote ,而不是相反。

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

您的投票 function 似乎很好。 Print_winner,没那么多。 一些冗余,一些奇怪的东西。 尽量保持简单。 我看到你已经解决了它,但可能有助于比较你在哪里使用了你不需要的东西/行。

void print_winner(void)
{
    int winner = 0;
    // The var 'winner' is an int, a number of votes, not a name.

    // Finding the highest number of votes
    for (int i = 0; i < candidate_count; i++)
    {
    if (candidates[i].votes > winner)
        {
            winner = candidates[i].votes;
        }
    }

    // Printing the names of all candidates with 'winner' number of votes
    for (int j = 0; j < candidate_count; j++)
    {
        if (candidates[j].votes == winner)
        {
            printf("%s\n", candidates[j].name);
        }
    }
    return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM