简体   繁体   English

关于 for 循环内函数调用的问题

[英]Question regarding function calls within for loops

I am writing a simple code that runs a plurality vote.我正在编写一个运行多数票的简单代码。 My code seems to work only if I don't call the bool vote function after char *name = get_string("Vote: ") Why is this the case?我的代码似乎只有在char *name = get_string("Vote: ")之后不调用 bool 投票函数时才有效,为什么会这样? The logic that I don't understand is that char *name changes every time a new value is assigned with get_string n number times in the for loop.我不明白的逻辑是每次在 for 循环中使用 get_string n 次分配新值时 char *name 都会更改。 If this is the case, shouldn't it logically make sense to call the vote function for each name before a new value is assigned to name?如果是这种情况,在将新值分配给 name 之前为每个 name 调用投票函数在逻辑上不应该有意义吗? Why doesn't the code work after I add a line that calls the vote function before each iteration of the for loop?为什么在 for 循环的每次迭代之前添加一行调用投票函数后代码不起作用?

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

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct candidate
{
    char *name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;
int voter_count;

// Function prototypes
bool vote(char *name);
void print_winner(void);

int main(int argc, char *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;
        printf("%s\n", candidates[i].name);
    }

    voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        char *name = get_string("Vote: ");
        //The main difference between the answer and my code was that I tried to call vote(name) at this point. I should figue out why this is incorrect, and the following logical explanation.

        // 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(char *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
    int maxVotes = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > maxVotes)
        {
            maxVotes = candidates[i].votes;
        }
    }
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == maxVotes)
        {
            printf("%s\n", candidates[i].name);
        }
    }
    return;
}

Besides the fact that the spec says " You should not modify anything else in plurality.c ", there already is a call to vote(name) for each voter here if (!vote(name)) .另外一个事实,即规范说“你不应该修改其他任何东西plurality.c”,已经一个呼叫vote(name)每个选民在这里if (!vote(name))

It does not " logically make sense to call the vote function for each name before a new value is assigned to name " because the order of operations is: 在将新值分配给 name 之前为每个 name 调用投票函数在逻辑上没有意义”,因为操作顺序是:

  • get a candidate name for voter i获取选民 i 的候选人姓名
  • add that vote to the selected candidate将该票添加到选定的候选人中

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

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