我想了解K& R的练习1-21 [英] I'm trying to understand the K&R's exercise 1-21

查看:117
本文介绍了我想了解K& R的练习1-21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要理解的问题:

写一个程序"entab",该程序用最少的空格替换字符串 制表符和空格的数量以实现相同的间距.当一个 制表符或单个空格就足以达到制表位,这应该 被赋予优先权?

Write a program 'entab' that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?

解码问题:

a .这是一个在输入中插入标签"的程序.

a. It's a program that injects 'tabs' in the input.

b .如果字符串由连续的空格组成,则这些空格 空格必须替换为最少的制表符和空格.

b. If a string consists of continuous blank spaces, then these blank spaces has to be replaced with minimum number of tabs and spaces.

该程序在以下输入中应如何表现:

How should the program behave in the following below inputs:

  1. 嘿*****
  2. ******嗨
  3. 嘿**************嗨!
  4. 嗨####嘿!
  5. 你好,你好吗?
  6. 嗨#**嘿!
  1. hey*****
  2. ******hi
  3. hey**************hi!
  4. hi####hey!
  5. hi***how****are*you?
  6. hi#**hey!

确定最小制表符数量以及最小制表符和空格组合的标准是什么?

What should be the criteria to decide on the minimum number of tabs, and combination of min tabs and spaces?

#"代表制表符,"*"代表空格,TABSTOP = 8.

'#' for tab, '*' for blank space, TABSTOP = 8.

该语句是什么意思:当一个制表符或单个空白足以达到制表位时."

What does the statement mean: "when either a tab or a single blank would suffice to reach a tab stop."

注意:我已经回答了这个重复的问题,但是我 认为它们没有帮助.

Note: I have gone through answers of this duplicate question, but I don't find them helpful.

推荐答案

链接的可能重复的问题集中在正确进行算术上.我猜您的问题比这更原始:您真的不知道制表位是什么.

The linked possible-duplicate question focuses on doing the arithmetic correctly. I'm guessing your problem is more primitive than that: you don't really know what a tab stop is.

当K& R编写练习时,他们可以期望读者具有打字机的经验,这是制表符停止的地方. Tab键使滑架滑到下一个止动位置,这是一种真实的物理现象,实际上阻止了滑架继续移动,直到按下另一个键为止. (后来的打字机变得越来越像计算机,已经对停止位置进行了编程,而不是对物理停止进行了编程.)

When K&R wrote their exercises, they could expect the reader to have experience with typewriters, which is where tab stops come from. The tab key caused the carriage to slide over to the position of the next stop, which was a real physical thing that literally stopped the carriage from moving further until another key was pressed. (Later typewriters, becoming more computer-like, had programmed stop locations instead of physical stops.)

制表符的目的是制表(制作表格).将制表位设置在分隔表各列的水平位置,然后在每个值之后按制表键输入表.例如,如果我要键入此表:

The purpose of tabs was tabulation (making tables). The tab stops were set at the horizontal locations separating the columns of the table, and then the table was entered by pressing the tab key after each value. For example, if I want to type this table:

Character         ASCII
Tab               9
Linefeed          10
Carriage Return   13
Space             32

在不使用制表符的情况下,我必须在单词"Tab"后多次键入空格,而在换行符"之后则不必键入多次,而在回车"之后只需键入几次.但是,如果我在第二列开始的位置设置了制表位,则变得更加容易.我在单词"Tab"之后按一次Tab键,然后笔架前进到9的正确位置,在单词"Linefeed"之后按一次Tab键,然后前进到10的正确位置,依此类推.如果需要第三列,则可以在第二列的右边设置另一个制表位.

Without using tabs, I have to type space many times after the word "Tab", not as many times after "Linefeed", and only a few times after "Carriage Return". But if I set a tab stop at the position where the second column starts, it becomes easier. I press the tab key once after the word "Tab", and the carriage advances to the correct place for the 9, press the tab key once after the word "Linefeed", and it advances to the correct place for the 10, etc. If I needed a third column, I would set another tab stop a little bit to the right of the second column.

您可以在文本编辑器中体验到这一点-不是将所有不相关的功能分配给Tab键的高级IDE,而是像vi这样的普通功能.甚至是终端仿真器,运行不执行任何操作的程序(cat > /dev/null).键入几行不同长度的单词,每行后面都有一个制表符,观察它们如何对齐,以及当一个单词足够长以占据2个表列时会发生什么.继续使用它,直到您对制表符的功能有了直观的了解.

You can experience this in a text editor - not a fancy IDE that assigns all kinds of unrelated functions to the tab key, but a plain one like vi. Or even a terminal emulator, running a program that does nothing (cat > /dev/null). Type several lines of words of various lengths, with a tab after each one, and observe how they line up, and what happens when one of your words is long enough to occupy 2 table columns. Keep playing with it until you have an intuitive understanding of what the tab character does.

现代文本编辑器和终端仿真器通常每8个字符设置一个制表位.这就是"tabstop = 8"的意思.止动点在第8,16,24,32列...,或者如果您认为最左边的列是1而不是0,则在第9,17,25,33列....制表位实际上可以在vt100-上编程终端,但这是很少使用的功能.

Modern text editors and terminal emulators usually have tab stops set every 8 characters. That's what "tabstop = 8" means. The stops are at columns 8,16,24,32,..., or if you believe the leftmost column is 1 instead of 0, columns 9,17,25,33,... Tab stops are actually programmable on vt100-ish terminals, but that's a rarely used feature.

在文本编辑器中,返回新行的开头,键入 1 2 Tab 3 4 .您将1234分隔成由制表符组成的某些空格.然后开始另一行,并在选项卡之前的空格处键入相同的内容: 1 2 space Tab 3 4 .第二行看起来与第一行完全相同,但是这次1234之间的空白由空格和制表符组成.通过键入 space 直到它对齐,使第三行看起来相同而不使用任何选项卡.

Back in your text editor, a the start of a new line, type 1 2 Tab 3 4. You get 12 and 34 separated by some whitespace made of a tab character. Then start another new line and type the same thing with a space before the tab: 1 2 space Tab 3 4. The second line looks exactly like the first, but this time the whitespace between 12 and 34 is made of a space and a tab. Make a third line that looks the same without using any tabs, by typing space until it lines up.

这3行是对练习1-21程序可能输入的示例.第一个只有一个选项卡且没有空格的字符使用最少的字符数,因此这是您希望为所有3个输入输出的字符.

Those 3 lines are examples of possible input to the exercise 1-21 program. The first one, with a single tab and no spaces, uses the minimum number of characters so that's what you want to output for all 3 inputs.

如果需要帮助来确定要输出多少制表符和空格的一般公式,请参见另一个问题.这是一个粗略的描述,为您留下了一些细节供您考虑:阅读输入时,请跟踪您所在的列.进入空格或制表符时,请阅读空格和制表符序列的末尾,记住序列开始时您所在的列.在序列的最后,您知道光标位于哪一列以及要将其移动到哪一列,并且必须计算空格和制表符的最佳顺序才能进行该移动.

If you need help figuring out a general formula for how many tabs and spaces to output, see the other question. Here's a rough description that leaves some details for you to think about: As you read the input, keep track of what column you're in. When you get to a space or tab, read to the end of the sequence of spaces and tabs, remembering what column you were in when the sequence started. At the end of the sequence, you know what column the cursor is in, and what column you want to move it to, and you must calculate the optimal sequence of spaces and tabs to make that movement.

这篇关于我想了解K& R的练习1-21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆