2011-10-11 36 views
4

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

實施例通過請求:

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); 
+1

當問這樣一個問題,一個例子將是有益的。 –

+3

哦,你應該完全把你的任務交給[80列打孔卡片](https://secure.wikimedia.org/wikipedia/en/wiki/Punched_card)。畢竟,這就是所有這些「最多80列」材料的起源。 –

+1

@GregHewgill我不得不挖掘這個問題,所以我可以告訴你,在2017年,我面臨一個ABAP程序轉儲的問題,因爲我的程序行長度超過了72個字符。 [有時候我討厭SAP](https://archive.sap.com/discussions/thread/661589)。 – gkubed

回答

6

在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); 
+0

如何編輯我的原始文章中的一些ginormostatement? – gkubed

相關問題