繁体   English   中英

CS50 复数 PSET3 - 代码似乎有效,但 check50 另有说明

[英]CS50 Plurality PSET3 - Code seems to work but check50 says otherwise

因此,该程序应该通过命令行参数(候选人名称)output 已知选举类型的获胜候选人,称为多票/“赢家通吃”。 从我所看到的和个人编译的等等来看,它就是这样做的。 如果有平局,它会输出所有平局的候选人,如果只有一个获胜者,它会输出他们的名字。 当我通过 check50 运行它进行测试时,它给了我在这张图片中发现的错误。 分配从创建函数“vote”和“print_winner”开始。 我将不胜感激有人可以向我转发正确的方向:) 谢谢

    #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 | DONE
    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 largest_vote = candidates[0].votes;
    int largest_vote_ids[candidate_count];
    bool tie_toggle = false;
    
    // Goes through candidate votes and checks whether or not there is a tie
    for (int i = 1; i < candidate_count; i++)
    {
        // Default winner if there is only 1 candidate
        if (candidate_count == 1)
        {
            printf("%s\n",candidates[0].name);
            return;
        }
        if (candidates[i].votes == largest_vote)
        {
            tie_toggle = true;
            // Stores candidate index as a tied competitor
            largest_vote_ids[i] = i;
        }
        // DISCARD (LOW VOTE)
        if (candidates[i].votes < largest_vote)
        {
            largest_vote_ids[i] = 100;
        }
        // NEW MAX VOTE
        if (candidates[i].votes > largest_vote)
        {
            tie_toggle = false;
            for (int a = 0; a < candidate_count; a++)
            {
                // Clears list of tied candidates
                largest_vote_ids[a] = 100; // 100 is considered "empty" - Reset list
            }
            // Stores candidates index as the current highest voted 
            largest_vote_ids[0] = i;
            largest_vote = candidates[i].votes;
        }
        
    }
    if (tie_toggle)
    {
        for (int i = 0; i<candidate_count; i++)
        {
            // For every tied candidate in the list, print their name (the int 100 refers to an empty element of the list)
            if (largest_vote_ids[i] != 100) 
            {
                printf("%s\n", candidates[largest_vote_ids[i]].name);
            }
        }
    }
    else
    {   // Print the winning candidate (solo win)
        printf("%s\n", candidates[largest_vote_ids[0]].name);
    }
    return;
}

所以我采取了一种更简单的方法,结果花了几秒钟,仍然很困惑为什么以前的代码不起作用但是哦。 学会了简单的重要性。

(我没有列出可能并列的候选人名单,而是检查了每个候选人并检查他们的投票是否等于最高票并相应打印)

暂无
暂无

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

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