关于K& R运动的问题(第96页) [英] question about an exercise in K&R (p96)

查看:86
本文介绍了关于K& R运动的问题(第96页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想弄清楚这是否是预期的行为,或者是否是b $ b我错过了什么......(可能是后者)。


给出的例子(p96,K& R)用于说明指针行为。


K& R说示例将一个字符流打破为整数




相关代码是:(我认为)


(主要内容)


for(i = 0; i< SIZE&& getint(& arr [i])!= EOF; i ++);


和我相信getint中的相关代码是:


while(isspace(c = getch()));


if(!isdigit( c)&& c!= EOF&& c!=''+''&& c!='' - ''){

ungetch(c);

返回0;


其中getch()和ungetch()是推送/获得

额外字符的K& R版本来自缓冲区。

我的问题是:


如果我的输入是这样的:&qu OT; 89 76 45 -90输出是相同的

(如果我打印数组)


但是一旦我引入了一个数字以外的字符,它就会进入

无限循环,当


i< SIZE是假的。


这是预期的行为吗?


提前致谢。

I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.

K&R say the example "break(s) a stream of characters into integer
values"

The relevant code is: ( I think)

(in Main)

for (i=0; i < SIZE && getint(&arr[i]) != EOF; i++);

and I believe the relevent code in getint is:

while ( isspace(c=getch()));

if ( !isdigit (c) && c != EOF && c != ''+'' && c != ''-''){
ungetch(c);
return 0;

where getch() and ungetch() are K&R versions of pushing/getting the
extra character to/from a buffer.
My question is this:

If my input is something like: " 89 76 45 -90" the out put is the same
(if I print the array)

but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?

Thanks in advance.

推荐答案

mdh说:
mdh said:

我只想弄清楚这是不是预期的行为,还是

我遗漏了一些东西......(可能是后者)。


给出的例子(p96,K& R)用于说明指针行为。
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.



< snip>

<snip>


但是当我引入一个数字以外的字符时,它会进入

无限循环,当


i< SIZE是假的。


这是预期的行为吗?
but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?



这不是预期的输入,这是肯定的!为了清晰起见,K& R正在牺牲

稳健性(在我看来,这是一个错误,但这是一个意见问题) - 或者他们搞砸了。您的电话。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:正常服务将尽快恢复。请不要

调整您的电子邮件客户端。

It''s not the intended input, that''s for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that''s a
matter of opinion) - or they screwed up. Your call.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.


Richard Heathfield写道:
Richard Heathfield wrote:

mdh说:
mdh said:

我只想弄清楚这是不是预期的行为,还是

我错过了什么...... (可能是后者)。


给出的例子(p96,K& R)用于说明指针行为。
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.



< snip>

<snip>


但是一旦我引入了一个数字以外的字符,它就会进入

无限循环,并退出时


i< SIZE是假的。


这是预期的行为吗?
but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?



这不是预期的输入,这是肯定的!为了清晰起见,K& R正在牺牲

稳健性(在我看来,这是一个错误,但这是一个意见问题) - 或者他们搞砸了。你的来电。


It''s not the intended input, that''s for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that''s a
matter of opinion) - or they screwed up. Your call.



人们会想到K& R'的练习中的错误之前会被发现

。如果mdh发布完整的消息来源会有所帮助,因为我的副本现在还没有拿到现金。



One would''ve thought a bug in K&R''s exercises would''ve been spotted
before now. It would help if mdh posted the full source, as my copy of
K&R isn''t at hand right now.


santosh说:
santosh said:

Richard Heathfield写道:
Richard Heathfield wrote:

> mdh说:
>mdh said:

我只想弄清楚这是否是预期的行为,或者是否是b $ b我错过了什么......(可能是后者)。 br />

给出的例子(p96,K& R)用于说明指针行为。
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.


< snip>

<snip>


但是一旦我引入了一个数字以外的字符,它就会进入

无限循环,并在

$时退出b $ bi< SIZE是假的。


这是预期的行为吗?
but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?


这不是预期的输入,这是肯定的!为了清晰起见,K& R都牺牲了坚固性(在我看来,这是一个错误,但这是一个意见问题) - 或者他们搞砸了。你的来电。


It''s not the intended input, that''s for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that''s a
matter of opinion) - or they screwed up. Your call.



在此之前,人们会想到K& R'的练习中的错误会被发现



One would''ve thought a bug in K&R''s exercises would''ve been spotted
before now.



这不是一个错误。当提供

适当的输入时,该程序工作正常,毕竟这是一个指针练习

函数参数! K& R很可能已经考虑过一个更强大的驱动因素。事实上,他们甚至懒得提出一个完整的

司机。引用来自main()的OP的代码实际上只是一个

四行代码片段。

It isn''t a bug as such. The program works fine when presented with
appropriate input, and this is, after all, an exercise in pointers as
function arguments! K&R may well have considered a more robust driver to be
a distraction. In fact, they don''t even bother to present a complete
driver. The code the OP as quoted as being from main() is in fact just a
four-line code fragment.


它会有所帮助如果mdh发布了完整的源代码,
It would help if mdh posted the full source,



他发布的内容几乎就是他们为此编写的所有代码,尽管

他出现了从记忆中完成它,因为有一两个

的差异。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:正常服务将尽快恢复。请不要

调整您的电子邮件客户端。

What he posted, pretty much, is all the code they wrote for this, although
he appears to have done it from memory, as there are one or two
discrepancies.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.


这篇关于关于K&amp; R运动的问题(第96页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆