如何将c++代码中的printf语句限制为每行80个字符?

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

本文关键字:80个 字符 语句 c++ 代码 printf      更新时间:2023-10-16

我的教授要求我的代码每行不超过80个字符,但是我有一些printf语句超过了这个限制。是否有一种方法可以在不改变输出的情况下将该语句分成两行或多行?

请求示例:

printf("n%-20s %-4d %-20s %-4d %-20s %-4dn%-20s %-4d %-20s %-4d%-20s %-4dn%-20s %-4d %-20s %-4d %-20s %-4dn%-20s %-4d %-20s %-4d %-20s %-4dn%-20s %-4d %-20s %-4dn", "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 %-4dn"
       "%-20s %-4d %-20s %-4d%-20s %-4dn"
       "%-20s %-4d %-20s %-4d %-20s %-4dn"
       "%-20s %-4d %-20s %-4d %-20s %-4dn"
       "%-20s %-4d %-20s %-4dn",
       "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);