繁体   English   中英

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

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

我知道 print_winner function 的实现不正确(printf 应该在 if 语句中)。 但是,我无法弄清楚为什么 printf 在放置在 if 语句之外时会以不同的方式打印名称?

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

代码:

// 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);

Output:

~/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 语句中,这里是代码和 output:

代码:

// 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;
}

Output:

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

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

在第二种情况下,您只打印投票数等于最大值的人的姓名( if (candidates[k].votes == maximum)为真),因此只有同时获得 2 票的benanbu 才是最大值。

因为几个人可以拥有相同的票数,所以拥有可变的获胜者是没有用的,你可以这样做

// 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);
  }
}

从你的言论

您能否详细说明为什么在第一种情况下,在if (candidates[k].votes变为 true 之前,会打印出获胜者的名字 Candidate candidates[0].name

因为你用candidates[0].name;初始化了获胜者 正在做:

字符串获胜者=候选人[0].name;

并且if (candidates[k].votes == maximum)为真时才修改获胜者

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

暂无
暂无

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

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