如何将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 %-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);
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 将stl字符串缩小到小于15个字符的容量
- 带有多个字符分隔符的正则表达式
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 如何删除字符串中的前 2 个字符(如果它有"-")
- 注册表正在设置 1 个字符
- 使用类 ?C++ 和 SFML 以及标题中的 15 个字符
- 如何忽略第一个字符输入后的所有内容
- std::stoi 只转换前几个字符
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- C++字符数组不接受超过 4 个字符的输入
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- 增量生成 - 错误:字符串的第一个字符应'0'或'1'
- 对于多个字符(如 ETX/STX 对),是否有类似于 std::quote 的东西
- 在函数中连接两个字符,并在 C++ 中返回输出
- 如何通过组合多个字符数组来创建字符数组?
- 对c++中的输入使用getline函数时忽略第一个字符
- 在 Linux 上的 C 语言中,popen / system to "ps all > file" 将所有行截断为 80 个字符
- 如何将c++代码中的printf语句限制为每行80个字符?