【问题标题】:CS50 PSET 3 Runoff vote function confusionCS50 PSET 3 决选功能混乱
【发布时间】:2022-06-17 22:00:22
【问题描述】:

voter 函数应该在决选选举中检查已投选票中的姓名,如果选民选票名称与候选人姓名匹配,它会将该候选人姓名记录在首选项数组中作为该选民的选择。下面我把源码和投票功能贴出来。

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

// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9

// preferences[i][j] is jth preference for voter i
int preferences[MAX_VOTERS][MAX_CANDIDATES];

// Candidates have name, vote count, eliminated status
typedef struct
{
    string name;
    int votes;
    bool eliminated;
} candidate;

// Array of candidates
candidate candidates[MAX_CANDIDATES];

// Numbers of voters and candidates
int voter_count;
int candidate_count;

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

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

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX_CANDIDATES)
    {
        printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
        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: ");
    if (voter_count > MAX_VOTERS)
    {
        printf("Maximum number of voters is %i\n", MAX_VOTERS);
        return 3;
    }

    // Keep querying for votes
    for (int i = 0; i < voter_count; i++)
    {

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            // Record vote, unless it's invalid
            if (!vote(i, j, name))
            {
                printf("Invalid vote.\n");
                return 4;
            }
        }

        printf("\n");
    }

    // Keep holding runoffs until winner exists
    while (true)
    {
        // Calculate votes given remaining candidates
        tabulate();

        // Check if election has been won
        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;
        }

        // Eliminate anyone with minimum number of votes
        eliminate(min);

        // Reset vote counts back to zero
        for (int i = 0; i < candidate_count; i++)
        {
            candidates[i].votes = 0;
        }
    }
    return 0;
}

// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(name, candidates[i].name) == 0)
        {
            preferenes[voter][rank] = i;
        }
    }
    return false;
}

投票功能的解决方案不是我的,我只是无法理解 [voter] [rank] 来自哪里?这些变量没有在其他任何地方初始化,如果没有给它们任何值,我只是不明白它们是如何在数组中使用的。我也很困惑为什么它等于 i。我知道这很可能是一个菜鸟问题,并且有一个非常简单的答案。提前感谢您的帮助。

【问题讨论】:

  • 显然在这次选举中不允许​​写入候选人:-(。无论如何,vote 显然是坏的。它总是返回 false。voter 和 rank 在该函数中的值是由调用者提供的参数提供给函数的参数。在这种情况下,它们来自main 中的i 和j 循环(整个源代码中唯一调用vote 的地方:if (!vote(i, j, name)))。
  • @WhozCraig — 在许多地方,“写入候选人”的概念并不存在。
  • 有一个带有嵌套 for 循环的片段,它使用选民 ID (i, 数字 0..(voter_count - 1)) 和排名 (j) 调用 vote() 函数, 一个数字 0..(candidate_count - 1))。
  • @WhozCraig --so 在调用函数的地方声明 vote(i , j, name) 会将 for 循环的使用传递给函数中提供的任何参数,我可以在函数内部调用它们当我需要使用循环?只是想在继续下一个 TODO 之前确保我理解正确
  • @tjgribbin 这不是声明;那是在调用中。 i 和 j 的值作为参数传递给该函数的实现,该函数分别将这些值用于 voter 和 rank。相信我;买一本关于 C 的好书。这类问题在此类书籍中都非常详细。

标签: c cs50


【解决方案1】:

注意函数调用,里面有什么,怎么传递=)

if (!vote(i, j, name))
        {
            printf("Invalid vote.\n");
            return 4;
     }

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多