K&安培; R练习1.16 - 线长度限制 [英] K&R Exercise 1.16 - Limitation on line length

查看:100
本文介绍了K&安培; R练习1.16 - 线长度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习从K&放C; R的 C编程语言的书。我做在书指定的练习。我在练习数1.16,但我不明白。

1.16练习:


  

修改最长行程序的主程序所以它将
  正确打印任意长度的输入线的长度,并作为
  尽可能的文字。


我的问题:


  1. ......尽可能的文字... - 有对字符串的长度一定的局限性?也许在标准头有一个与字符串长度的最大允许值的变量?


  2. ......任意长的输入线的长度。 - 但在code MAXLINE被定义为1000。这是有限的体积太大。我看到一些解决方案这里,但在我看来,这不是解决方案的决定,因为对前有一行(1000个字符)的长度的限制。


也许我不理解的任务。我的理解是,我必须取出1000个字符的限制。


解决方案

这是一个pretty早锻炼在K&放大器; R,你只是应该做的code,而不是总有些小的改动重新设计code的。



  1.   

    ......尽可能文本......


    是由你来间preT。我想通过打印什么存储在最长缓冲做到这一点。即打印出到该行的1000个字符。再次,这是一个早锻炼,很少介绍了动态分配的内存呢。而当时的K&安培; R写入,储存远任意长的文本行没有像今天这样可行



  2.   

    ......任意长的输入线的长度。


    是一个硬要求。你应该找到正确的长度,无论它有多长

  3. (至少的边界的 INT 内)

要解决这个问题的一个方法是:


  • 调用函数getline()后,检查最后一个字符读入缓冲区是一个换行符('\\ n')

  • 如果是这样,你读的完整产品线。在 LEN 变量是行(函数getline的返回值()的正确长度,比起原来的code不需要特别的考虑。

  • 如果它的的,你没看过整条生产线,并需要去寻找这一行的末尾。你加一个while循环,调用的getchar(),直到它返回一个换行符(或EOF),并计算您在循环中读取的字符数。只要做到 LEN ++ 计数。

  • 当while循环完成后,新的 LEN 现在是该行的实际长度,但我们的缓冲区只是有它的第999个字符。

  • 像以前一样,储存起来(复印件()函数调用)的电流缓冲(最多1000个字符),如果这条线是最长的为止。

  • 当你完成,你打印出存储线之前(最长缓冲液)最大的长度是可变的。

    • 由于上面提到的while循环最大长度现在是正确的。

    • 如果在最长行确实是超过1000个字符长。你至少打印出的第一个字符999 - 这是尽可能多地


我不会破坏它,并张贴了code,你需要做到这一点,但它仅仅是6 code线,你需要添加到运动1-16最长行程序。

I'm learning C from K&R's "The C Programming Language" book. I'm doing the exercises specified in the book. I'm on exercise number 1.16, but I don't understand it.

Exercise 1.16:

Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.

My questions:

  1. "...as much as possible of the text..." - is there some limitation on string length? Maybe in standard headers there's a variable with the max allowed value of string length?

  2. "...the length of arbitrarily long input lines..." - but in the code MAXLINE is defined as 1000. It is limited size too. I see some solutions here, but in my opinion it is not solution decision, since on the former there is a restriction on length of a line (1000 characters).

Maybe I don't understood the task. My understanding is I must remove the 1000-character limitation.

解决方案

It's a pretty early exercise in K&R, you're just supposed to do some minor changes to the code, not a total redesign of the code.

  1. "...as much as possible of the text..."

    is up to you to interpret. I'd do it by printing what's stored in the longest buffer. i.e. print out up to 1000 characters of the line. Again, it's an early exercise, with little introduction to dynamically allocated memory yet. And at the time K&R was written, storing away arbitrarily long text lines wasn't as feasible as it is today.

  2. "...the length of arbitrarily long input lines..."

    Is a hard requirement. You're supposed to find the correct length no matter how long it is (at least within the bounds of an int. )

One way to solve this problem is:

  • After the call to getline(), check if the last character read into the line buffer is a newline ('\n')
  • If it is, you read a complete line. The len variable is the correct length of the line(the return value of getline(), and no special consideration is needed compared to to original code.
  • If it is not , you did not read the entire line, and need to hunt for the end of this line. You add a while loop, calling getchar() until it returns a newline (or EOF), and count the number of characters you read in that loop. Just do len++ to count.
  • When the while loop is done, the new len is now the actual length of the line, but our buffer just has the first 999 characters of it.
  • As before, you store away (the copy() function call) the current line buffer (max 1000 chars) if this line is the longest so far.
  • When you're done, you print out the stored line as before (the longest buffer) and the max variable for the length.
    • Due to the above mentioned while loop that max length is now correct.
    • If the longest line indeed was longer than 1000 chars. you at least print out those first 999 chars - which is "as much as possible".

I'll not spoil it and post the code you need to accomplish this, but it is just 6 lines of code that you need to add to the longest-line program of exercise 1-16.

这篇关于K&安培; R练习1.16 - 线长度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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