0

私は新しいプログラマーです。このCテストプログラムが機能しない理由を教えてください。これはトリム関数であると想定されています。

#include <stdio.h>

#define MAX 1000

void main(){
    char line[MAX];
    int lgh;
    line[0] = '\0';
    while ((lgh = getLine(line, MAX)) != 0){
        printf("%s", line);
        line[0] = '\0';
    }
}

int getLine(char s[], int length){
    char s2[length];
    int i, ii, qttWord = 0, qttWord2 = 0;
    int c; // c = getchir() d = EOF
    int flag = 2;

    s2[0] = '\0';
    /*Reads the input and puts it into s[], then, verifies if the input is just \n,
    * if so, returns 0(i), if not, puts '\n' at the end of the string ind '\0' to close
    */
    for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
         s[i] = c;
         ++qttWord;
    }
    if (i == 0){
        if (c == '\n')
            return 0;
    } else if (c == '\n'){
        s[i] = c;
        ++qttWord;
        ++i;
        s[i] = '\0';
    }
    fflush(stdin);
    /*Verifies if the string is just ' ' or '\t'
    * if so, returns 0
    */
    for (i = 0; i <= qttWord && flag != 1; ++i){
        if (s[i] == ' ' || s[i] == '\t'){
            flag = 0;
        } else{
            flag = 1;
        }
    }
    if (flag == 0)
        return 0;
    /*
    *The trim function
    */
    for (i = 0; i < qttWord; ++i){
        if (i < qttWord-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord2;
                printf("1%d\n", s2[i]);//test thing
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord2;
            printf("0%d\n", s2[i]);//test thing
        }
    }
    s[0] = '\0';
    s2[qttWord2+1] = '\0';
    printf("Q:%d\n", qttWord2);//test thing
    printf("A:%s\n", s2);//test thing
    for (i = 0; i < qttWord2+1; ++i){
        s[i] = s2[i];
    }
    return 1;
}

ご覧のとおり、printfテストを多数作成して、なぜ機能しないのかを確認しましたが、役に立たないのです。なぜ動かないのか理解できません。

4

1 に答える 1

1

fgets()特に行を読み取り、isspace()スペース文字を識別し、データをコピーするために、かなり標準的なライブラリ関数を使用するとmemmove()、次のコードが生成されます。

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX 1000

static int getLine(char s[], int length);

int main(void)
{
    char line[MAX];
    int lgh;
    /* Stop on EOF or a blank line */
    while ((lgh = getLine(line, MAX)) > 0)
        printf("%d <<%s>>\n", lgh, line);
    return(0);
}

/*
** Get a line of input with leading and trailing white space
** stripped off.  The newline is not included.  If there is no
** newline in the space available, the length 0 is returned.
** If EOF is encountered, EOF is returned.
*/
static int getLine(char s[], int length)
{
    char s2[length];

    s[0] = '\0';
    if (fgets(s2, sizeof(s2), stdin) == 0)
        return EOF;

    size_t len = strlen(s2);
    if (s2[len-1] != '\n')
        return 0;   /* No newline - line too long */

    /* Find first non-white space */
    size_t off = 0;
    while (isspace(s2[off]))
        off++;
    if (off > len)
        return 0;

    /* Chop trailing space */
    while (len-- > 0 && isspace(s2[len]))
        s2[len] = '\0';

    /* Non-blank string is in s2[off]..s2[len], plus trailing '\0' */
    memmove(s, &s2[off], len-off+2);
    return(len - off + 1);  /* Length excluding trailing null */
}

の診断出力にmain()は、報告された長さと、注意深く区切られた出力文字列とそれに続く改行が含まれることに注意してください。isspace()明らかに、必要に応じて、空白とタブを厳密にチェックするコードに置き換えることができます。の独自のアナログを書くことができますfgets()

元のコードは、入力の最初の行の後にEOFを取得したときに、それを気に入らなかった(空の入力で試したとは思わない)。退化したケースは常に適切に処理するようにしてください。

于 2012-10-28T03:16:16.673 に答える