简体   繁体   English

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

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

I know it is not correct implementation for print_winner function (printf should be inside the if statment).我知道 print_winner function 的实现不正确(printf 应该在 if 语句中)。 However, I cannot figure out why printf prints names differently when placed outside if-statement?但是,我无法弄清楚为什么 printf 在放置在 if 语句之外时会以不同的方式打印名称?

If I place printf outside if statement, here is the code and output如果我将 printf 放在 if 语句之外,这里是代码和 output

Code:代码:

// 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: Output:

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

If I put printf inside if statement, here is the code and output:如果我将 printf 放在 if 语句中,这里是代码和 output:

Code:代码:

// 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: Output:

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

in your first case the winner name you print is candidates[0].name which is sonia until if (candidates[k].votes == maximum) become true and you reassign winner , and you print candidate_count times because unconditionally in the loop.在您的第一种情况下,您打印的获胜者名称是 Candidate candidates[0].name ,它是sonia ,直到if (candidates[k].votes == maximum)变为 true 并且您重新分配了获胜者并且您打印了 Candidate_count 次,因为无条件地在循环中。

In the second case you only print the name of the person whose votes number equals the maximum ( if (candidates[k].votes == maximum) is true), so only ben and anbu having both 2 votes being the maximum.在第二种情况下,您只打印投票数等于最大值的人的姓名( if (candidates[k].votes == maximum)为真),因此只有同时获得 2 票的benanbu 才是最大值。

Because several persons can have the same number of votes it is useless to have the variable winner , you can do因为几个人可以拥有相同的票数,所以拥有可变的获胜者是没有用的,你可以这样做

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

From your remark从你的言论

Could you elaborate why in the first case winner name is printed candidates[0].name until if (candidates[k].votes becomes true您能否详细说明为什么在第一种情况下,在if (candidates[k].votes变为 true 之前,会打印出获胜者的名字 Candidate candidates[0].name

because you initialize winner with candidates[0].name;因为你用candidates[0].name;初始化了获胜者 doing:正在做:

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

and you modify winner only when if (candidates[k].votes == maximum) is true:并且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