getint函数解决办法
getint函数
函数getint将输入的字符流分解成整数,且每次调用得到一个整数。getint需要返回转换后得到的整数,并且,在到达输入结尾时要返回文件结束标记。
该版本的getint函数在到达文件结尾时返回EOF,当下一个输入不是数字时返回0,当输入中包含一个有意义的数字时返回一个正值。
下面代码用到 getch函数 和 ungetch函数,有何妙处,不解?
------最佳解决方案--------------------
大版主 我在6楼 贴了一段 怎么使用getint 的代码 还望指点 ?
------其他解决方案--------------------
int getch(void);
應該是getc的wrapper
Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.
void ungetch(int);
應該是ungetc的wrapper
The ungetc function pushes back the character c onto the input stream stream.
------其他解决方案--------------------
c != EOF判断是否是文件 结束符
------其他解决方案--------------------
求大神 回复
------其他解决方案--------------------
------其他解决方案--------------------
if(c != EOF) /* 这里是干嘛的?c != EOF, 说明 19行里取出的c是一个非数字的字符,就把这个字符返回输入流(stdin)
ungetch(c);
楼主程序用的getch不对啊,应该是getchar,
标准库里没有getch函数啊,好像也木有ungetch函数啊,倒是有一个ungetc
------其他解决方案--------------------
函数getint将输入的字符流分解成整数,且每次调用得到一个整数。getint需要返回转换后得到的整数,并且,在到达输入结尾时要返回文件结束标记。
该版本的getint函数在到达文件结尾时返回EOF,当下一个输入不是数字时返回0,当输入中包含一个有意义的数字时返回一个正值。
下面代码用到 getch函数 和 ungetch函数,有何妙处,不解?
int getch(void);
void ungetch(int);
/*getint 函数:将输入中的下一个整型数赋值给*pn */
int getint(int *pn)
{
int c, sign;
while(isspace(c = getch())) /*跳过空白符*/
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-')
{
ungetch(c); /*输入不是一个数字 */
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c = getch();
for(*pn = 0; isdigit(); c = getch())
* pn = 10 * *pn + (c - '0');
*pn *= sign;
if(c != EOF) ?这里是干嘛的?
ungetch(c);
return c;
}
------最佳解决方案--------------------
大版主 我在6楼 贴了一段 怎么使用getint 的代码 还望指点 ?
------其他解决方案--------------------
int getch(void);
應該是getc的wrapper
Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.
void ungetch(int);
應該是ungetc的wrapper
The ungetc function pushes back the character c onto the input stream stream.
------其他解决方案--------------------
c != EOF判断是否是文件 结束符
------其他解决方案--------------------
求大神 回复
------其他解决方案--------------------
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
------其他解决方案--------------------
if(c != EOF) /* 这里是干嘛的?c != EOF, 说明 19行里取出的c是一个非数字的字符,就把这个字符返回输入流(stdin)
ungetch(c);
楼主程序用的getch不对啊,应该是getchar,
标准库里没有getch函数啊,好像也木有ungetch函数啊,倒是有一个ungetc
------其他解决方案--------------------