复数,cs 50,为什么 printf 函数在 if 语句外打印不同?

Plurality, cs 50, Why printf function prints differently outside if statement?

我知道 print_winner 函数的实现不正确(printf 应该在 if 语句中)。但是,我不明白为什么 printf 在 if 语句之外打印名称不同?

如果我将 printf 放在 if 语句之外,这里是代码和输出

代码:

// Print the winner (or winners) of the election
void print_winner(void)
{

int maximum = candidates[0].votes;
string winner = candidates[0].name;

    for (int h = 0; h < candidate_count; h++)
        {
            if (candidates[h].votes > maximum)
                {
                    maximum  = candidates[h].votes;
                }
        }

    for (int k = 0; k < candidate_count; k++)
        {
            if (candidates[k].votes == maximum)
                {
                    winner = candidates[k].name;

                }
                printf("%s\n", winner);

输出:

~/pset3/plurality/ $ ./test sonya criss ben anbu
Number of voters: 4
Vote: ben  
Vote: ben
Vote: anbu
Vote: anbu
sonya
sonya
ben
anbu

如果我将 printf 放在 if 语句中,这里是代码和输出:

代码:

// Print the winner (or winners) of the election
void print_winner(void)
{

int maximum = candidates[0].votes;
string winner = candidates[0].name;
    //Find maximum votes
    for (int h = 0; h < candidate_count; h++)
        {
            if (candidates[h].votes > maximum)
                {
                    maximum  = candidates[h].votes;
                    winner = candidates[h].name;

                }

        }
    //Find candidate with maximum votes
     for (int k = 0; k < candidate_count; k++)
        {
            if (candidates[k].votes == maximum)
                {
                    winner = candidates[k].name;

                    printf("%s\n", winner);
                }

        }

    return;
}

输出:

~/pset3/plurality/ $ ./plurality sonya criss ben anbu
Number of voters: 4
Vote: ben
Vote: ben
Vote: anbu
Vote: anbu
ben
anbu

在您的第一种情况下,您打印的获胜者姓名是 candidates[0].name,即 sonia,直到 if (candidates[k].votes == maximum) 变为 true 并且您重新分配 winner ,然后你打印 candidate_count 次,因为无条件地在循环中。

在第二种情况下,你只打印票数等于最大值的人的名字(if (candidates[k].votes == maximum)是真的),所以只有 benanbu同时获得2票为最大值

因为几个人可以有相同的票数所以有变量 winner 是没有用的,你可以做

// Print the winner (or winners) of the election
void print_winner(void)
{
  int maximum = candidates[0].votes; /* suppose candidate_count > 0, else initialize with -1 */
  
  //Find maximum votes
  for (int h = 1; h < candidate_count; h++) /* useless to redo at index 0 */
  {
     if (candidates[h].votes > maximum)
       maximum  = candidates[h].votes;
  }

  //Find candidate(s) with maximum votes
  for (int k = 0; k < candidate_count; k++)
  {
     if (candidates[k].votes == maximum)
       puts(candidates[k].name);
  }
}

根据您的评论

Could you elaborate why in the first case winner name is printed candidates[0].name until if (candidates[k].votes becomes true

因为你用 candidates[0].name; 初始化 winner 做 :

string winner = candidates[0].name;

并且当 if (candidates[k].votes == maximum) 为真时,您修改 winner only :

       if (candidates[k].votes == maximum)
           {
               winner = candidates[k].name;