读书人

getint函数解决办法

发布时间: 2012-12-28 10:29:05 作者: rapoo

getint函数
函数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
------其他解决方案--------------------


下面是完整 getint 函数使用的代码
输入一段字符串 1a23bc4
输出 是不是 1 23 4
这个函数怎么用啊 很困惑? 还有书上说 借助getch和ungetch两个函数 ,函数getint必须读入的一个多余字符就可以重新写回到输入中? 什么意思啊?

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

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); c = getch())
* pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}

#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;
}

int main()
{
int n, array[10];
for (n = 0; n < 10 && getint(&array[n]) != EOF; n++)
;
for (n = 0; n < 10; n++)
printf("%d\n", array[n]);
return 0;
}

[其他解释]
引用:
if(c != EOF) /* 这里是干嘛的?c != EOF, 说明 19行里取出的c是一个非数字的字符,就把这个字符返回输入流(stdin)
ungetch(c);


楼主程序用的getch不对啊,应该是getchar,
标准库里没有getch函数啊,好像也木有ungetch函数啊,倒是有一个ungetc
嗯 版主说的对的 这两个函数 是书上写的呢 ungetch 是把压回的字符送到缓冲区内 这个也叫 把字符返回 输入流(stdin)吧。 对输入流的概念有点模糊。
[其他解释]
输入流可以看做一个字符的队列。
[其他解释]
函数没有问题, main函数里的用法有问题

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



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语言

热点推荐