C语言程序设计getint,getint函数解决办法

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

------其他解决方案--------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值