簡體   English   中英

CS50 pset3 復數程序沒有 print_winner function (沒有打印兩個選舉的獲勝者)

[英]CS50 pset3 plurality program doesn't print_winner function (did not print both winners of election)

我剛剛完成了我的多個 cs50 程序的編程。 它運行順利,但是當我運行檢查時,它說它不打印獲勝者的名字,但確實如此。 這是代碼,有人可以幫助我嗎? 正如我所說的 function 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");
    }
}

很高興看到您的更多代碼,例如定義/初始化candidatescandidate_count的位置,例如,如果您正在執行#include <string> ,這表明您實際上using的是std::string而不是其他的字符串的版本。

如果是這種情況,您對std::string的使用很奇怪。 通常strcmp()需要一個指向 C 樣式的字符數組或類型const char*的指針 ... printf() ... 所以你應該 append .c_str()來使用你的name和 Candidate candidates[n - i].name

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

不過,再次假設您實際上正在使用std::string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM