简体   繁体   English

这些最后一行代码在K&R的C编程语言的练习1-13中做了什么?

[英]What are these last lines of code doing in Exercise 1-13 of K & R's The C Programming Language?

I am very new to programming in general, so please bear with my lack of knowledge. 我对编程一般都很陌生,所以请忍受我缺乏的知识。

I have spent a couple of hours now on exercise 1-13. 我现在花了几个小时进行练习1-13。 I finally decided to look up the answer, which I found at this link https://github.com/ccpalettes/the-c-programming-language-second-edition-solutions/blob/master/Chapter1/Exercise%201-13/word_length.c . 我终于决定查找答案,我在这个链接上找到了这个链接https://github.com/ccpalettes/the-c-programming-language-second-edition-solutions/blob/master/Chapter1/Exercise%201-13 /word_length.c

Because I didn't want to copy it completely for the sake of learning, I tried to understand the code and then remake it. 因为我不想为了学习而完全复制它,所以我试着理解代码然后重新制作它。 (This resulted in almost a complete copy, but I understand it better than I would have otherwise.) (这导致了几乎完整的副本,但我理解它比其他方式更好。)

This is what I have so far: 这是我到目前为止:

#include <stdio.h>

#define IN 1
#define OUT 0
#define LARGEST 10

main() 
{
    int c, state, l, i, j;
    int length[LARGEST + 1];

    for (i = 0; i <= LARGEST; ++i)
    length[i] = 0;

    state = OUT;

    while ((c = getchar()) != EOF) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            if (state == OUT) {
                l = 0;
                state = IN;
            }
        ++l;
        }
        else 
            if (state == IN) {
                if (l <= LARGEST)
                    ++length[l - 1]; 
                    //minus 1 because the nth term of an array is actually array[n-1]
                else //if (l > LARGEST)
                    ++length[LARGEST];
                state = OUT;
            }
            if (c == EOF)
                break;
    }

    for (i = 0; i <= LARGEST; ++i) {
        if (i != LARGEST) //because array[10] refers to the 11th spot
            printf("\t %2d |", i + 1); //plus one because 1st is array [0] 
            //this actually results in 1-10 because the 0-9 plus one makes the highest 10
        else
            printf("\t>%2d |", LARGEST);
        for (j = 0; j < length[i]; ++j)
            putchar('x');
        putchar('\n');
    }


    return 0;
}

Please ignore my comments. 请忽略我的评论。 They were meant for me, so that I could explain the program to myself. 它们是给我的,所以我可以自己解释这个程序。

I am having two issues that I just can't figure out, and they're driving me crazy: 我有两个问题,我无法弄清楚,他们让我发疯:

  1. The output always accounts for one word less than in the input, meaning "my name is not bob" results in: 输出总是比输入中的一个字少,这意味着“我的名字不是bob”导致:

     ... 2 |xx 3 |x 4 |x ... 
  2. Also, I don't understand what is going on at the end of the program. 另外,我不明白程序结束时发生了什么。 Specifically, I don't understand here why the variable j is being used: 具体来说,我不明白为什么使用变量j

      for (j = 0; j < length[i]; ++j) putchar('x'); 

Thanks so much for your help, and I'm sorry if this is too beginner for the community. 非常感谢您的帮助,如果这对社区来说太初学,我很抱歉。

Well, trying to sum up all the answers since the question is not closed. 好吧,试图总结所有答案,因为问题没有结束。 First, we need to correct the main() line: 首先,我们需要更正main()行:

    int main(void) {
    ...
    return 0;
    }

The int is necessary because you return a value at the end of the function, and the void means that the function doesn't receive any arguments. int是必需的,因为你在函数的末尾返回一个值,而void意味着函数没有接收到任何参数。

  1. I've copied your code and executed on my machine (Ubuntu 12.04) and it worked perfectly. 我复制了你的代码并在我的机器上执行(Ubuntu 12.04),它运行得很好。 Could you present some examples to generate the error? 你能举一些例子来产生错误吗?

  2. As everybody said, j is just a variable to traverse the vector. 正如大家所说, j只是遍历向量的变量。 length[i] is a vector that holds, in each position i the number of words with length i . length [i]是在每个位置i中保持长度为i的单词数的向量。 For instance, if position 3 has a value of 4, eg length[3] = 4, it means that there are 4 words with length 3. 例如,如果位置3的值为4,例如length [3] = 4,则意味着有4个长度为3的单词。

Finally, if I may, I'd like to give you a tip. 最后,如果可以的话,我想给你一个提示。 It is good practice choosing meaningful names for your variables. 为变量选择有意义的名称是一种很好的做法。 The code you linked here helped me to understand what the program should do. 在这里链接的代码帮助我理解了程序应该做什么。 Variable names such, length, or defines IN, OUT or LARGEST are too vague. 变量名称,长度或定义IN,OUT或LARGEST太模糊。

I hope this gather all answers until now and helped you even more. 我希望这收集所有答案,直到现在,并帮助你更多。

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

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