C 通过for循环读取字符串时获得随机重复

C 通过for循环读取字符串时获得随机重复,c,arrays,string,comparison,iteration,C,Arrays,String,Comparison,Iteration,不管我怎样 Down, down, down. Would the fall NEVER come to an end! 'I wonder how S1: line 1, bytes = 68 S2: line 1, score = 5.000 --- many miles I've fallen by this time?' she said aloud. 'I must be getting S1: line 2, bytes = 72 S2: l

不管我怎样

Down, down, down. Would the fall NEVER come to an end! 'I wonder how  
S1: line     1, bytes = 68  
S2: line     1, score =  5.000
---
many miles I've fallen by this time?' she said aloud. 'I must be getting  
S1: line     2, bytes = 72  
S2: line     2, score =  4.000
如果我通过添加printf(“%c”,第[y]行)按循环打印该行,则得到此输出

SO: query = fall down
---
Down, down, down. Would the fall NEVER come to an end! 'I wonder how
S1: line   1, bytes = 68
line   1, score = 0.000000
---
many miles I've fallen by this time?' she said aloud. 'I must be getting
S1: line   2, bytes = 72
: line   2, score = 0.000000
---
SO:query=fall-down
---
下来,下来,下来。秋天永远不会结束我想知道怎么做
S1:第1行,字节=68
下来,下来,下来。难道秋天永远不会来临吗?崩溃,倒下,倒下。秋天永远不会结束我不知道该怎么办⌠“”S2:第1行,分数=0.000000
---
“到现在为止,我已经跌了好几英里了?”她大声说我一定是疯了
S1:第2行,字节=72
“这次我已经走了多少英里了?”她大声说,“这次我走了多少英里了。”我一定是疯了⌠“”S2:第2行,分数=0.000000
---
int
打印行(字符查询[最大查询]){
int line_num=0;
int query_len=strlen(查询);
内线长度=0,i,y,z=0,位置,c=0;
int limit=query\u len>line\u len?line\u len:query\u len;
双倍分数=0,最大值;
char score_list[15]=“”;
字符行[最大行];
而((行长度=读取行(行))!=EOF){
c=0;
行_num++;
printf(“S1:第%d行,字节=%d\n”,第n行,第n行);
/*循环查询和行并比较字符
*如果相同,则会增加分数
*一旦到达不同的字符,分数将重置
*/
对于(i=0;i最大值){
最大值=得分列表[位置];
}
}
/*将分数列表重置回零
*/
而(c<15){
评分表[c]=0;
C++;
}
printf(“S2:第%d行,分数=%f\n”,第n行,最大值);
printf(“--\n”);
}
返回0;
}

最大的问题是设计
读取行
while
循环,在
打印行
中调用。您不能在
while
循环中返回(1)
line\u len
,或(2)
EOF
并测试
EOF
,因为这两者并不相互排斥。例如,通常情况下,您的文件在最后一行的文本后面没有一个
换行符
。(POSIX规定所有文件都应该有一个结尾
换行符
,但许多编辑很乐意编写没有它的文件)。如果出现这种情况,并且您正在循环中测试
EOF
,则您错过了最后一行文字

此外,如果您在任何地方读取的字符数超过
MAX\u LINE
,则不进行测试。要解决此问题,您可以重新设计
read\u line
,如下所示:

SO: query = fall down
---
Down, down, down. Would the fall NEVER come to an end! 'I wonder how
S1: line   1, bytes = 68
Down, down, down. Would the fall NEVER come tDown, down, down. Would the fall     NEVER come to an end! 'I wonder how                   °·) ⌠"" S2: line   1, score = 0.000000
---
many miles I've fallen by this time?' she said aloud. 'I must be getting
S1: line   2, bytes = 72
many miles I've fallen by this time?' shmany miles I've fallen by this time?' she said aloud. 'I must be getting           °·) ⌠""  S2: line   2, score =     0.000000
---

int
print_lines(char query [MAX_QUERY]){
    int line_num = 0;
    int query_len = strlen(query);
    int line_len = 0,i,y,z = 0, position,c = 0;
    int limit = query_len > line_len ? line_len : query_len;
    double score = 0, maximum;
    char score_list[15] = "";
    char line[MAX_LINE];
    while((line_len = read_lines(line)) != EOF){
        c = 0;
        line_num ++;
        printf("S1: line   %d, bytes = %d \n",line_num,line_len);
        /* Loops through the query and line and compares the characters
         * If it is the same, it adds to the score
         * Once it gets to different character the score is reset
         */
        for (i = 0; i < limit; i++){
            for (y = 0; y < limit; y++){
                if (query[i] == line[y]){
                    score++;
                }
                else if(score != 0 && query[i] != line[y]){
                    score_list[z] = score;
                    z++;
                }
            }
        }
        /* Finds the largest score in the array
         */
        maximum = score_list[0];
        for (position = 1; position < 15; position++){
            if (score_list[position] > maximum){
            maximum = score_list[position];
            }
        }
        /* resets the score list back to zero
         */
        while (c < 15){
            score_list[c] = 0;
            c++;
        }
        printf("S2: line   %d, score = %f\n",line_num,maximum); 
        printf("---\n");
    }
    return 0;
}
int query_len = strlen (query);
...
int limit = query_len > line_len ? line_len : query_len;
接下来,除了在注释中给出的关于使用警告编译的所有好理由之外,如果没有空格或注释,代码将无法阅读。您也没有以简洁的方式处理循环限制。如果
查询
的长度超过
,则在读取
中的最后一个字符后,您可以在外部循环中继续循环,而不执行任何操作。一个简单的例子:

int read_lines (char line[MAX_LINE])
{
    int line_len = 0;
    int c;

    while ((c = getchar ()) != '\n' && c != EOF) {
        line[line_len] = c;
        line_len++;
        if (line_len == MAX_LINE - 1) break;
    }
    line[line_len] = 0;

    if (line_len)
        printf ("%s\n", line);

    return line_len ? line_len : c == EOF ? EOF : line_len;
}
将为可用于计算
分数的单个循环设置限制。(注意:所有长度
int
变量实际上应该是type
size\t
unsigned
,因为不能有负长度。对于只包含正值的计数器,如循环计数器,也是如此…)

然后,对于实际处理,为了处理最后一行文本的所有情况,您可以将循环重新构造为:

SO: query = fall down
---
Down, down, down. Would the fall NEVER come to an end! 'I wonder how
S1: line   1, bytes = 68
Down, down, down. Would the fall NEVER come tDown, down, down. Would the fall     NEVER come to an end! 'I wonder how                   °·) ⌠"" S2: line   1, score = 0.000000
---
many miles I've fallen by this time?' she said aloud. 'I must be getting
S1: line   2, bytes = 72
many miles I've fallen by this time?' shmany miles I've fallen by this time?' she said aloud. 'I must be getting           °·) ⌠""  S2: line   2, score =     0.000000
---

int
print_lines(char query [MAX_QUERY]){
    int line_num = 0;
    int query_len = strlen(query);
    int line_len = 0,i,y,z = 0, position,c = 0;
    int limit = query_len > line_len ? line_len : query_len;
    double score = 0, maximum;
    char score_list[15] = "";
    char line[MAX_LINE];
    while((line_len = read_lines(line)) != EOF){
        c = 0;
        line_num ++;
        printf("S1: line   %d, bytes = %d \n",line_num,line_len);
        /* Loops through the query and line and compares the characters
         * If it is the same, it adds to the score
         * Once it gets to different character the score is reset
         */
        for (i = 0; i < limit; i++){
            for (y = 0; y < limit; y++){
                if (query[i] == line[y]){
                    score++;
                }
                else if(score != 0 && query[i] != line[y]){
                    score_list[z] = score;
                    z++;
                }
            }
        }
        /* Finds the largest score in the array
         */
        maximum = score_list[0];
        for (position = 1; position < 15; position++){
            if (score_list[position] > maximum){
            maximum = score_list[position];
            }
        }
        /* resets the score list back to zero
         */
        while (c < 15){
            score_list[c] = 0;
            c++;
        }
        printf("S2: line   %d, score = %f\n",line_num,maximum); 
        printf("---\n");
    }
    return 0;
}
int query_len = strlen (query);
...
int limit = query_len > line_len ? line_len : query_len;
这意味着在第一次通过时,
score\u列表
被重置,但是对于随后的每一次通过,
c
已经是
15
,并且该代码从未执行


如果你解决了这些问题,你可能会让你的代码做你想做的。值得一提的是,在编译时,在启用警告的情况下编译。至少
-Wall-Wextra
。然后修复所有警告。如果你有任何问题,请尝试一下,让我知道。

最大的问题是
阅读的设计_行
while
循环,在
打印行
中调用它。不能返回(1)
,也不能返回(2)
EOF
并在
while
循环中测试
EOF
,因为这两者不是相互排斥的。例如,通常情况下,您的文件在最后一行的文本后面没有
换行符。(POSIX规定所有文件都应该有一个结尾
换行符
,但许多编辑会很乐意编写没有结尾的文件)。如果出现这种情况,并且您正在循环中测试
EOF
,那么您就错过了最后一行文本

此外,如果您在任何地方读取的字符数超过了
MAX\u LINE
,则不进行测试。要解决此问题,您可以重新设计
read\u LINE
,如下所示:

SO: query = fall down
---
Down, down, down. Would the fall NEVER come to an end! 'I wonder how
S1: line   1, bytes = 68
Down, down, down. Would the fall NEVER come tDown, down, down. Would the fall     NEVER come to an end! 'I wonder how                   °·) ⌠"" S2: line   1, score = 0.000000
---
many miles I've fallen by this time?' she said aloud. 'I must be getting
S1: line   2, bytes = 72
many miles I've fallen by this time?' shmany miles I've fallen by this time?' she said aloud. 'I must be getting           °·) ⌠""  S2: line   2, score =     0.000000
---

int
print_lines(char query [MAX_QUERY]){
    int line_num = 0;
    int query_len = strlen(query);
    int line_len = 0,i,y,z = 0, position,c = 0;
    int limit = query_len > line_len ? line_len : query_len;
    double score = 0, maximum;
    char score_list[15] = "";
    char line[MAX_LINE];
    while((line_len = read_lines(line)) != EOF){
        c = 0;
        line_num ++;
        printf("S1: line   %d, bytes = %d \n",line_num,line_len);
        /* Loops through the query and line and compares the characters
         * If it is the same, it adds to the score
         * Once it gets to different character the score is reset
         */
        for (i = 0; i < limit; i++){
            for (y = 0; y < limit; y++){
                if (query[i] == line[y]){
                    score++;
                }
                else if(score != 0 && query[i] != line[y]){
                    score_list[z] = score;
                    z++;
                }
            }
        }
        /* Finds the largest score in the array
         */
        maximum = score_list[0];
        for (position = 1; position < 15; position++){
            if (score_list[position] > maximum){
            maximum = score_list[position];
            }
        }
        /* resets the score list back to zero
         */
        while (c < 15){
            score_list[c] = 0;
            c++;
        }
        printf("S2: line   %d, score = %f\n",line_num,maximum); 
        printf("---\n");
    }
    return 0;
}
int query_len = strlen (query);
...
int limit = query_len > line_len ? line_len : query_len;
接下来,除了注释中给出的关于使用警告编译的所有好理由外,您还通过完全不带空格或注释的方式使代码无法阅读。您也没有以简洁的方式处理循环限制。如果
查询
的长度超过
,则会不断循环在读取
中的最后一个字符后,不做任何操作。一个简单的:

int read_lines (char line[MAX_LINE])
{
    int line_len = 0;
    int c;

    while ((c = getchar ()) != '\n' && c != EOF) {
        line[line_len] = c;
        line_len++;
        if (line_len == MAX_LINE - 1) break;
    }
    line[line_len] = 0;

    if (line_len)
        printf ("%s\n", line);

    return line_len ? line_len : c == EOF ? EOF : line_len;
}
将为您可以执行的单个循环设置限制