简体   繁体   English

Kernighan 和 Ritchie C 练习 1-16

[英]Kernighan and Ritchie C exercise 1-16

I tried to implement a solution for the exercise on the C language of K&R's book.我试图为 K&R 书中的 C 语言练习实现一个解决方案。 I wanted to ask here if this could be considered a legal "solution", just modifying the main without changing things inside external functions.我想问这里是否可以将这视为合法的“解决方案”,只需修改main而不更改外部函数内部的内容。

Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.修改最长行程序的主程序,使其正确打印任意长输入行的长度,以及尽可能多的文本。

#include <stdio.h>

#define MAXLINE 2 ////

int get_line1(char s[], int lim)
{
    int c, i;

    for (i = 0; i < lim - 1 && ((c = getchar()) != EOF) && c != '\n'; i++) {
        s[i] = c;
    }
    if (c == '\n') {
        s[i] = c;
        i++;
    }
    s[i] = '\0';
    return i;
}

int main()
{
    int len;
    int max = MAXLINE;
    char line[MAXLINE];
    int tot = 0;
    int text_l = 0;

    while ((len = get_line1(line, max)) > 0) {
        if (line[len - 1] != '\n') {
            tot = tot + len;
        }
        if (line[1] == '\n' || line[0] == '\n') {
            printf("%d\n", tot + 1);
            text_l = text_l + (tot + 1);
            tot = 0;
        }
    }
    printf("%d\n", text_l);
}

The idea is to set the max lenght of the string considered for the array line ad 2. For a string as abcdef\\n , the array line will be ab .这个想法是为数组line ad 2 设置考虑的字符串的最大长度。对于abcdef\\n字符串,数组lineab Since the last element of the array is not \\n (thus the line we are considering is not over), we save the length up until now and repeat the cycle.由于数组的最后一个元素不是\\n (因此我们正在考虑的行还没有结束),我们保存直到现在的长度并重复循环。 We will get then the array made of cd , then ef and at the end we will get the array of just \\n .然后我们将得到由cd的数组,然后是ef ,最后我们将得到只有\\n的数组。 Then the else if condition is executed, since the first element of this array is \\n , and we print the tot length obtained from the previous additions.然后, else if条件被执行,因为此数组的第一个元素是\\n ,我们打印tot从以前的加法得到的长度。 We add +1 in order to also consider the new character \\n .我们添加 +1 也是为了考虑新字符\\n This works also for odd strings: with abcdefg\\n the process will go on up until we reach g\\n and the sum is done correctly.这也适用于奇数字符串:使用abcdefg\\n过程将继续,直到我们到达g\\n并且求和正确完成。

Outside the loop then we print the total amount of text.在循环之外,然后我们打印文本的总量。

Is this a correct way to do the exercise?这是做练习的正确方法吗?

The exercise says to “Revise the main routine,” but you altered the definition of MAXLINE , which is outside of main , so that is not a valid solution.练习说要“修改主程序”,但是您更改了MAXLINE的定义,该定义在main之外,因此这不是有效的解决方案。

Also, your code does not have the copy or getline routines of the original.此外,您的代码没有原始代码的copygetline例程。 Your get_line1 appears to be identical except for the name.除了名称外,您的get_line1似乎完全相同。 However, a correction solution would use identical source code except for the code inside main .但是,更正解决方案将使用相同的源代码,除了main的代码。

Additionally, the exercise says to print “as much as possible of the text.”此外,练习说要打印“尽可能多的文本”。 That is unclearly stated, but I expect it means to keep a buffer of MAXLINE characters (with MAXLINE at its original value of 1000) and use it to print the first MAXLINE −1 characters of the longest line.这没有明确说明,但我希望这意味着保留MAXLINE字符的缓冲区( MAXLINE为其原始值 1000)并使用它来打印最长行的第一个MAXLINE -1 个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM