2011-10-11 63 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

相关问题