簡體   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