K&R 练习 1-20 - 需要一些说明
我不完全理解以下练习的要求:
“编写一个程序 detab,用适当数量的空格替换输入中的制表符,以间隔到下一个制表位。假设有一组固定的制表位,例如每 n 列。 n 应该是变量还是符号参数?”
有人可以澄清粗体部分吗?
I don't fully understand what the following exercise is asking:
"Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?"
Could someone clarify the bolded part?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本练习要求您通过添加正确数量的空格来模拟制表符的行为,以便输出仍然在制表位上对齐。
例如:
如果每 4 列有一个制表位(即 n = 4),则 : 应变为 :(
制表符已替换为三个空格)。
或者通过指示制表位的位置来澄清:
如果制表位每 3 列,那么您应该得到:(
制表符仅被 1 个空格替换)
This exercise is asking you to emulate the behavior of tabs by adding the correct amount of spaces, such that the output is still aligned on tab stops.
For example :
should become :
(the tab has been replaced with three spaces), if tab stops are every 4 columns (ie. n = 4).
Or to clarify by indicating where the tab stops are :
If tab stops are every 3 columns, then you should get :
(the tab was replaced by only 1 space)
以制表位设置为 n=8 为例,如果输入有 1 个字符,则制表符将添加 7 个空格(将您带到第 9 列)。基本上,不要总是添加 n 个空格,而是添加空格数,以便将您带到适合您的特定 n 值的列。
例如:
“假设一组固定的制表位”基本上是针对非程序员的。我们习惯了制表符总是在 4,8 等的倍数上对齐。但在文字处理程序中,制表位是可配置的...因此第一个制表符会将您对齐到第 6 列,第二个制表符将转到第 30 列(例如,草率地居中文本),第三个制表符将为您提供第 70 列(用于页码)或其他什么)。他只是在这里指出我们正在谈论“程序员”制表符,而不是文字处理器的制表符。
If you take an example of tabstops being set at n=8, for example, the if the input has 1 character, the tab will add 7 spaces (to bring you to column 9). Basically, don't always add n spaces, add the number of spaces that brings you to the appropriate column for your particular value of n.
For example:
"Assume a fixed set of tab stops" is for non-programmers, basically. We're used to a tab always aligning on a multiple of 4,8,etc. But in word processors, the tab stops are configurable... so the first tab would align you on column 6, the second would go to 30 (for example to sloppily center text) and the third would give you column 70 (for page numbers or something). He's just specifying here that we're talking about "programmer" tabstops, not a word processor's tabstops.