簡體   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