簡體   English   中英

如何在c ++的代碼中將printf語句限制為每行80個字符?

[英]How can I limit my printf statement to 80 characters per line in the code in c++?

我的教授要求我的代碼每行不超過80個字符,但我有一些超出此限制的printf語句。 有沒有辦法在不改變輸出的情況下將此語句分成兩行或多行?

請求示例:

printf("\n%-20s %-4d %-20s %-4d %-20s %-4d\n%-20s %-4d %-20s %-4d%-20s %-4d\n%-20s %-4d %-20s %-4d %-20s %-4d\n%-20s %-4d %-20s %-4d %-20s %-4d\n%-20s %-4d %-20s %-4d\n", "1 - Ones", ones, "2 - Twos", twos, "3 - Threes", threes, "4 - Fours", fours, "5 - Fives", fives, "6 - Sixes", sixes, "7 - Three of a Kind", threeOfAKind, "8 - Four of a Kind", fourOfAKind, "9 - Full House", fullHouse, "10 - Small Straight", smallStraight, "11 - Large Straight", largeStraight, "12 - Yahtzee", yahtzee, "13 - Chance", chance, "Total Score: ", score);

在C ++中,您可以像這樣破壞文字字符串:

printf("This is a very long line. It has two sentences.\n");

printf("This is a very long line. "
       "It has two sentences.\n");

在解析之前,編譯器將由空格分隔的任何雙引號字符串合並為一個字符串。 結果字符串不包含任何額外的字符,除了每對雙引號之間的內容(因此,沒有嵌入的換行符)。

對於帖子中包含的示例,我可能會執行以下操作:

printf("\n%-20s %-4d %-20s %-4d %-20s %-4d\n"
       "%-20s %-4d %-20s %-4d%-20s %-4d\n"
       "%-20s %-4d %-20s %-4d %-20s %-4d\n"
       "%-20s %-4d %-20s %-4d %-20s %-4d\n"
       "%-20s %-4d %-20s %-4d\n",
       "1 - Ones", ones, "2 - Twos", twos, "3 - Threes", threes,
       "4 - Fours", fours, "5 - Fives", fives, "6 - Sixes", sixes,
       "7 - Three of a Kind", threeOfAKind,
           "8 - Four of a Kind", fourOfAKind,
           "9 - Full House", fullHouse,
       "10 - Small Straight", smallStraight,
           "11 - Large Straight", largeStraight,
           "12 - Yahtzee", yahtzee,
       "13 - Chance", chance, "Total Score: ", score);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM