0

入力を一度に1文字ずつ読み取り、10進数、8進数、16進数のいずれであるかを判別し、それを10進数に変換して出力するプログラムをコーディングしています。その部分は機能します。問題はwhile((ch=getchar()) != EOF)、テキストを調べるために使用していることですが、whileループが終了することはありません。EOFは-1に相当し、EOFの使用方法はcontrol + zでプログラムを終了することだと聞きましたが、自動にしたいです。私は標準の入力を使用しているだけでなく、.txtテキストをフィードしていますが、どちらの方法でもプログラムは終了しません。誰かが私のコードを見て、どこが間違っているのかを知ることができるかどうか疑問に思いました。

(長いです、申し訳ありませんが、それが私がケース内で行ったことであるかどうかはわかりません)

int main (void) 
{
    int ch, chVal, decVal ;

    ch=getchar() ;

    while ((ch != EOF)) 
    {
        switch ( ch ) 
        {
            case ' ' :
                /* Just read until it’s not a space */
                while( ( ch == SPACE ) || ( ch== EOL ) ) 
                        ch=getchar();


            case '0' :
                /* integer, octal or hex possibilities */
                ch=getchar() ;

                /**** HEX ****/

                if ( ( ch=='x' ) || ( ch=='X' ) ) 
                {
                    /* potential hex. So far we have read in 0x.  We need to see what's next. */                
                    ch=getchar() ;

                    if ( ch== SPACE ) 
                    {
                        /* all we had was 0x entered */
                        outputIllegalToken() ;
                        break ; /* we need to end every pass through the switch with a space char (space, eoln or eof) */
                    }

                    decVal=0 ;
                    while ( ( ( '0' <= ch ) && ( ch <= '9' ) ) || ( ( 'a' <= ch ) && ( ch <= 'f' ) ) || ( 'A' <= ch ) && ( ch <= 'F' ) ) 
                    {
                        chVal = DecToHex( ch ) ;
                        decVal*=16 ;
                        decVal+=chVal ;
                        ch=getchar() ;
                    }

                    /* ch is no longer a hex val. */
                    if ( ch == SPACE || ch == EOL || ch == EOF ) 
                    {
                        /* All is good. */
                        outputHex( decVal );
                    } 
                    else 
                    {
                        if ( ch=='l' || ch=='L' ) 
                        {
                            ch=getchar() ;
                            if (( ch== SPACE ) || ( ch== EOL ) || ( ch==EOF )) 
                            {
                                outputLongHex( decVal ) ;
                            } 
                            else 
                            {
                                outputIllegalToken() ;
                                while ( ch!= SPACE && ch!= EOL && ch!= EOF ) 
                                { 
                                        getchar();
                                }
                            }
                        } 
                        else 
                        {
                            outputIllegalToken() ;
                            ch = getchar() ;
                            while ( ( ch!= SPACE ) && ( ch!= EOL ) && ( ch!= EOF ) ) 
                            { 
                                ch=getchar(); 
                            }
                        }
                    }
                    printf("do we come here? \n");
                    count++;
                     break;
                    /* end of the 0x scenario  ch is  a space char */
                }

               /****** 0 *******/

                if ( ch==' ' ) 
                {
                    /* just a 0 */
                    outputDecimal(0);
                    count++;
                    break ; /* end of 0 alone scenario. ch is a space */
                }

                /***** 0L ******/

                if ( ch=='l' || ch=='L' ) {
                    /* if there is space after the l, it's ok. otherwise illegal. */
                    ch=getchar() ;
                    if ( ( ch== SPACE ) || ( ch== EOL ) || (ch==EOF ) ) 
                    {
                        outputLongDecimal(0);
                    } 
                    else 
                    {
                        outputIllegalToken() ;
                        /* read to the next space */
                        ch=getchar() ;
                        while ( ( ch!= SPACE ) && ( ch!= EOL ) & ( ch!=EOF ) ) 
                                { 
                                ch=getchar(); 
                                }
                    }
                    count++;
                    break ;  /* end of 0L scenario.  ch is a space char. */
                }

                /**** Octal ****/

                if ( ( '0'<=ch )&&( ch<='7' ) ) 
                {
                    /* potentially octal  */
                    chVal=DecToHex( ch ) ;
                    decVal=chVal ;
                    ch=getchar() ;
                    while ( ( '0'<=ch )&&( ch<='7' ) ) 
                    {
                        decVal*=8 ;
                        decVal+=DecToHex( ch ) ;
                        ch=getchar() ;
                    }
                    /* no longer octal ch */
                    if ( ch== SPACE || ch== EOL || ch==EOF ) 
                    {
                        outputOctal( decVal ) ;

                    } 
                   else 
                    {
                        if ( ch=='l' || ch=='L' ) 
                        {
                            ch=getchar() ;
                            if (( ch== SPACE ) || ( ch== EOL ) || ( ch==EOF )) 
                            {
                                outputLongOctal(decVal) ;
                            } 
                            else 
                            {
                                outputIllegalToken() ;
                                while ( ch!= SPACE && ch!= EOL && ch!=EOF ) 
                                { 
                                        getchar();
                                }
                            }
                        } 
                        else 
                        {
                            outputIllegalToken() ;
                            ch = getchar() ;
                            while ( ( ch!= SPACE ) && ( ch!= EOL ) && ( ch!=EOF ) ) 
                            { 
                                ch=getchar(); 
                            }
                        }
                    count++;
                    break;/* end octal scenario  ch is a space character. */
                }

                count++;
                break ;

            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                /* potential decimal input */

                chVal=DecToHex( ch ) ;  /* convert the character to a number */
                decVal=chVal ;          /* keep track of the overall number */

                ch=getchar() ;
                while ( ( '0'<=ch ) && ( ch<='9' ) ) 
                {
                    chVal=DecToHex( ch ) ;
                    decVal*=10 ;
                    decVal+=chVal ;
                    ch=getchar() ;
                }

                /* integers have ended.  spaces or l is acceptable. everything else is illegal */
                if ( ( ch== SPACE ) || ( ch== EOL ) || ( ch==EOF )) 
                {
                    outputDecimal( decVal ) ;
                } 
                else if ( ( ch=='l' ) || ( ch=='L' ) ) 
                {
                    /* next char needs to be space in order to be a valid long integer */
                    ch==getchar() ;
                    if ( ( ch== SPACE ) || ( ch== EOL ) || ( ch==EOF ) ) 
                    {
                        outputLongDecimal( decVal ) ;
                    } 
                    else 
                    {
                        outputIllegalToken() ;
                        while ( ( ch!= SPACE) && ( ch!= EOL ) && ( ch!=EOF ) ) 
                        {
                            ch=getchar();
                        }
                    }
                }
                count++;
                break;
        }
    }
}

return 0;
}
4

2 に答える 2

0

これはレクサーです。この複雑なホイールを再構築する代わりに、 flex/lexを使用すると、より簡単にプログラムを作成して維持できます。これは、16 進数、0 から始まる 8 進数、およびその他すべてのパススルー用のフレックス コンバーターです。

%{

%}
%option 8bit outfile="scanner.c"
%option nounput nomain noyywrap
%option warn

%%

0x[0-9]+ { fprinf(yyout, "%d", yytext); /* hex conversion */ }
0[0-9]+  { fprintf(yyout, "%d", yytext); /* octal conversion */ }
.|\n { ECHO; /* print everything else out unmodified */ }

%%
int main(int argc, char **argv);

int main (argc,argv)
int argc;
char **argv;
{
yylex();
return 0;
}

次のようにコンパイルするだけです:

flex -Cf scanner.l 
gcc -O -o wordConverter scanner.c

stdin で入力を受け入れ、EOF まで出力します。

于 2013-02-04T18:14:28.770 に答える
0

これ:

            while( ( ch == SPACE ) || ( ch== EOL ) ) 
                    ch=getchar();

入力の最後の文字がスペースの場合、無限ループが発生します。あなたは単に を意味していたと思います( ch == SPACE )が、ステートメント全体を just に置き換えることbreak;もできます。

他にもバグがあるかもしれません...

于 2013-02-04T18:01:19.537 に答える