0

整数のリストを文字に変換しようとしています (整数は空白、改行、タブで区切られています)。入力は EOF で終了します。たとえば、入力します。72 101 108 108 111 44​​ 32 119 111 114 108 100 33 出力 Hello, world!

#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   100

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void )
{
    char ch;
        ch != '\n';
    //ch = getchar();
    //while( ch != '\n' )
        //ch = getchar();
}

main()
{
    char    ch;                     /* handles user input */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter integer(s)\n: ");
        //ch = getchar();
                scanf("%d",&ch)
        char_count = 0;
        while( (ch != '\n')  &&  (char_count < MAXBUFFERSIZE)) {
            buffer[char_count++] = ch;
            ch = getchar();
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nIntegers translates to:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\007Error: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}
4

2 に答える 2

1

scanfとの違いgetchar()、特に同じ入力が提示された場合の違いを学びます。

ドキュメントをscanf非常に注意深く読んでください。多くのことが行われています。

を呼び出す代わりに、独自の特殊なルーチンを作成することによって、最も多くのことを学ぶことができますscanf()。通常、C 標準の機能を複製することはお勧めできませんが、学習を支援することが目的であれば問題ありません。

于 2012-08-22T01:01:56.533 に答える
0

固定例

#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   100

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void ){
    while('\n'!=getchar());
}

main(){
    char    ch;                     /* handles user input delimiter*/
    int     num;                    /* user input number */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter integer(s)\n: ");
        scanf(" %d%c", &num, &ch);//read integer and delimiter
        char_count = 0;
        while( char_count < MAXBUFFERSIZE - 1) {//-1 for End of string
            buffer[char_count++] = (char)num;
            if(ch == '\n')break;
            scanf("%d%c", &num, &ch);
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nIntegers translates to:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\aError: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}
于 2012-08-21T23:21:36.843 に答える