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

发布于 2024-12-09 11:37:42 字数 656 浏览 0 评论 0原文

我的教授要求我的代码每行不超过 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);

My professor requests that my code does not exceed 80 characters per line, but I have some printf statements that exceed this limit. Is there a way to break this statement into two or more lines without changing the output?

Example by request:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-12-16 11:37:42

在 C++ 中,您可以像这样分解文字字符串:

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

into

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

In C++, you can break literal strings like this:

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

into

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

Any double-quoted strings that are separated by only whitespace, are coalesced into one string by the compiler before parsing. The resulting string does not contain any extra characters except what is between each pair of double quotes (so, no embedded newline).

For the example included in your post, I might do the following:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文