繁体   English   中英

CS50 算法 - 如何解决 print_winner() 函数的循环

[英]CS50 algorithms - how to solve loop for print_winner() function

为了解决 print_winner() 函数,我首先尝试对候选数组进行排序,完成后打印出数组中最后一个候选的名称。

我认为我非常接近(或远:p)答案,但我的编译器不断返回:(空)。

请问有什么帮助吗? 用最简单的术语来说,因为我对编程还很陌生,谢谢!

到目前为止,这是我的代码:

#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");
        }
        //hier wordt vote gecalled, waarom is dit niet zichtbaar adhv een else statement? Hoe kunnen we dit zichtbaar maken?
    }

    // 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
    
    //sort candidate array by swap,then printf last candidate name of array.
    
    int swap; 
    
    for (int i = 0, n = candidate_count; i < n; i++)
    {
        if (candidates[i].votes > candidates[i + 1].votes)
        {
            swap = candidates[i].votes;
            candidates[i].votes = candidates[i + 1].votes; 
            candidates[i + 1].votes = swap;
        }
    }
    printf("%s\n", candidates[candidate_count].name);
        
    return;
}

您只交换投票计数,因此candidates[candidate_count].name保持NULL

试试这个而不是你的print_winner()函数:

// Print the winner (or winners) of the election
void print_winner(void)
{
    // TODO
    
    //sort candidate array by swap,then printf last candidate name of array.
    
    candidate swap; // *** change type of swap ***
    
    for (int i = 0, n = candidate_count; i < n; i++)
    {
        if (candidates[i].votes > candidates[i + 1].votes)
        {
            // *** swap the whole structure instead of only votes ***
            swap = candidates[i];
            candidates[i] = candidates[i + 1]; 
            candidates[i + 1] = swap;
        }
    }
    printf("%s\n", candidates[candidate_count].name);
        
    return;
}

暂无
暂无

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

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