【问题标题】:CS50 pset3 plurality program doesn't print_winner function (did not print both winners of election)CS50 pset3 多个程序没有 print_winner 功能(没有打印两个选举的获胜者)
【发布时间】:2021-03-14 17:04:48
【问题描述】:

我刚刚完成了我的多个 cs50 程序的编程。它运行顺利,但是当我运行检查时,它说它不打印获胜者的名字,但确实如此。这是代码,有人可以帮助我吗? 正如我所说的函数print_winner() 有效,如果我运行debug50 我可以看到。 使用命令行参数,我假设宣布不超过 9 个候选人,选择我想要多少票,投票给候选人,然后程序假设宣布获胜者。问题是它可以做到,但根据 bot50 它没有。

#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)
{       
    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 n = candidate_count;
    typedef struct
    {
        string name;
        int votes;
    }
    bubble;
    bubble bubbles[n];
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {    //doing a bubble sort to move the max vote at the end and doing 
               the same with the candidate name
                 
            if (candidates[i].votes > candidates[j].votes)
            {
                bubbles[0].votes = candidates[i].votes;
                candidates[i].votes = candidates[j].votes;
                candidates[j].votes = bubbles[0].votes;
                bubbles[0].name = candidates[i].name;
                candidates[i].name = candidates[j].name;
                candidates[j].name = bubbles[0].name;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (candidates[n - i].votes == candidates[n - 1].votes)
        {
            printf("%s ", candidates[n - i].name);
        }
        printf("\n");
    }
}

【问题讨论】:

  • 它可能会打印名称,但您确定它打印的名称正确吗?
  • 是的,defenetley
  • 您是否尝试过检查其格式是否与应有的格式相同?
  • 什么意思?

标签: c printing cs50


【解决方案1】:

很高兴看到您的更多代码,例如定义/初始化candidates 和candidate_count 的位置,例如,如果您正在执行#include &lt;string&gt;,这表明您实际上是using @ 987654325@ 而不是其他版本的字符串。

如果是这样,您对std::string 的使用就很奇怪。通常strcmp() 需要一个指向C 风格字符数组的指针 或const char*... 类型,printf()... 也是如此,因此您应该将.c_str() 添加到您的用法中name 和 candidates[n - i].name。

即strcmp(candidates[i].name.c_str(), name.c_str()) 和 printf("%s ", candidates[n - i].name.c_str());

不过,这还是假设您实际使用的是std::string。

【讨论】:

  • 还有我的课程我还没有做指针,所以我想我必须以不同的方式继续。即使因为我不知道它们是什么以及如何使用它们啊哈哈
  • 感谢您提供其余部分。文件“cs50.h”必须包含类似typedef char* string; 之类的东西,我猜……这隐藏了它实际上是一个“指向字符数组的指针”,就像我提到的那样。指针对于大多数刚接触 C 的人来说都很可怕,所以你并不孤单,但是一旦你学习并理解了它们,它们对你来说就会很容易。我无法尝试编译您的代码,因为我没有该文件(并且它还必须包含常规 C 之外的函数,例如“get_string”),但除此之外,我在您的代码中看不到任何明显错误或不会编译。
  • 你只使用你的bubbles[n]数组的第一个元素......所以它可以被定义为bubble bubbles;并将所有bubbles[0]更改为bubbles——尽管在那点你可能想重命名你的变量!
  • 我不熟悉 CS50 或特定于 CS50 的文件,但假设它是 Google 建议的在线计算机科学课程……我猜他们有一种自动检查您的程序的方法正确性。如果它似乎与您看到您的程序正常工作相矛盾,那么可能是其他程序出错了。你的问题请咨询班级的教授或助教,因为我认为你已经做了你能做的一切。祝你好运! ......不要气馁!这些东西可能很难……但学习新事物一开始通常看起来是这样。
  • 我刚刚想到的最后一件事......如果你的教授/助教正在使用一些“机器人”程序来检查你的程序是否运行正确——它可能会直接进行字符串比较而没有任何灵活性。因此,请确保您的printfs 准确输出您想要的字符串,包括大写字母与小写字母、空格、换行符等。例如,print_winner() 的最后一个 for 循环将输出一个候选名称 后跟一个空格以及一系列换行符。如果机器人不期待这些换行符,它将失败。同样,机器人:"candidate_name" != "candidate_name "
猜你喜欢
  • 2023-03-23
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多