簡體   English   中英

C:找到最長的線

[英]C: finding longest line

我正在嘗試從(丹尼斯·里奇書)上運行此示例,但是找不到最長的行,有什么想法嗎?

main.c

#include <stdio.h>
#include <stdlib.h>

#include "other.h"



    int main()
    {
        /*printf("Hello world!\n");
        printf("%d\n",getmynumber());


        start of a new program%= */

        int len;
        int max;
        char line[MAXLINE];
        char longest[MAXLINE];

        max = 0;
        while ((len = getLine(line, MAXLINE)) > 0 )
            if (len > max){
                max = len;
                copyL(longest,line);
            }
        if (max > 0)
            printf("%s", longest);

        return 0;
    }

其他

#include <stdio.h>
#include "other.h"

int getmynumber(void){
    return 7;
}


int getLine(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;

}


void copyL(char to[], char from[])
{
    int i;
    i = 0;
    while ((to[i], from[i]) != '\0')
        ++i;
}

其他

#ifndef OTHER_H_INCLUDED
#define OTHER_H_INCLUDED
#define MAXLINE 1000


int getmynumber(void);
int getLine(char line[], int maxline);
void copyL(char to[], char from[]);


#endif // OTHER_H_INCLUDED

使用調試器:看起來我在copyL函數中沒有增加...為什么有任何想法?

代碼塊調試

固定:

while ((to[i] = from[i]) != '\0'){
    ++i;
}

我認為您錯誤地轉錄了代碼。 這行:

while ((to[i], from[i]) != '\0')

可能是不正確的。 表達式(to[i], from[i])是逗號運算符的實例,該運算符產生其右側作為結果值。 我認為這應該是一項任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM