gpt4 book ai didi

C 未检测到 EOF

转载 作者:行者123 更新时间:2023-11-30 17:54:53 26 4
gpt4 key购买 nike

我正在编写一个程序,该程序一次读取输入一个字符,并确定它是十进制、八进制还是十六进制,然后将其转换为十进制输出。该部分有效,问题是我使用 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;
}

最佳答案

这个:

            while( ( ch == SPACE ) || ( ch== EOL ) ) 
ch=getchar();
如果输入中的最后一个字符是空格,

将导致无限循环。我认为您的意思只是 ( ch == SPACE ),但仅用 break; 替换整个语句也可以。

可能还有其他错误...

关于C 未检测到 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14692383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com