K& R 1.5.1运动 [英] K&R 1.5.1 exercise

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

问题描述

使用基本工具进行练习非常困难。我花了大约十五分钟来练习1-7:

#include< stdio.h>


int main(int orange,char ** apple)

{

int c;

c = -5;

while(c!= EOF)

{

c ++;

}

printf("%d \\ \\ t",c);

printf("%d \ t",EOF);

return(0);

}

由于我的printf'同意窗口的'EOF是汉密尔顿'(ijk)^ 2,我认为

它是正确的。


练习1-6给我消化不良。这个编译和链接但不是
按照我的意愿行事:

#include< stdio.h>


int main(int orange,char **芒果)

{

int c;

c =(getchar()!= EOF);

printf("%d \ t",c);

返回(0);

}


我正在寻找一个0或1当我在键盘上随机啄,并且我在我的结局时,我似乎也无法调试。记住,如果你在他失败的时候踢了一个

的男人,你最好确保他留下来或者他不是2美元b $ b米身高,114公斤。 ++感谢。 MPJ

解决方案

2004年10月11日星期一21:20:10 -0500,Merrill& Michele写道:

使用基本工具进行练习非常困难。我花了大约十五分钟来练习1-7:
#include< stdio.h>

int main(int orange,char ** apple)


为什么这些变量的奇怪名称?分别是argc和argv,

已经为几乎所有的C程序员提供了很好的服务。

{
int c;
c = -5;
while(c!= EOF)
{
c ++;
}


这是微不足道的。你应该允许EOF的可能性等于INT_MIN这样的值,在这种情况下,这个程序可能会非常非常长时间地运行一个

。 (假设它可以从INT_MAX回收到INT_MIN

,并且不会崩溃。)

printf("%d \ t",c);
printf("%d \ t",EOF);


您可能需要在这些

对帐单中的至少一个中使用换行符,而不是制表符。

return(0) ;
}
由于我的printf'同意窗口的'EOF是汉密尔顿'(ijk)^ 2,我认为它是对的。


这是正确的,假设您的机器上的EOF> = -5。

练习1-6给我消化不良。这会编译和链接,但不会按照我的意愿行事:
#include< stdio.h>

int main(int orange,char ** mango)
{
int c;
c =(getchar()!= EOF);


我认为这是问题所在。你期待从

这个非常奇怪的陈述得到一致的结果。

printf("%d \ t",c);


ITYM printf("%d \ nn,c);

return(0);
}
<当我在键盘上随机啄时,我正在寻找一个0或1,并且在我的结束时,我似乎也无法调试。


我不知道标准保证价值,或者在任何机器上合理预期



记住,如果你在他摔倒的时候踢了一个男人,你最好确保他留下来或者他身高不超过11​​4公斤。 ++感谢。 MPJ




你不会从我这里得到任何结果。


" Merrill& ;米歇尔" <是******** @ comcast.net>写道:

使用基本工具进行练习非常困难。


如果你的意思是来自K& R的练习,我发现它没有任何问题。

大多数人都使用像纸和笔一样的小工具。然后再次......

int main(int orange,char ** apple)




....我不是故意的乖乖,也许,也许,只是也许,如果你开始认真对待

,你会得到更多的东西吗?


Richard


In< cO ******************** @ comcast.com> Merrill&米歇尔" <是******** @ comcast.net>写道:

使用基本工具进行练习非常困难。我花了大约十五分钟来练习1-7:
#include< stdio.h>

int main(int orange,char ** apple)
{
int c;
c = -5;
while(c!= EOF)
{
c ++;
}
printf("%d \t",c);
printf("%d \ t",EOF);
返回(0);
}
由于我的printf'同意窗口的'EOF是汉密尔顿'(ijk)^ 2,我认为它是对的。


不,不是。你的while循环最终会导致整数溢出,

这是C中的未定义行为。此外,它与你的问题完全无关,其解决方案简单如下: br />

#include< stdio.h>


int main()

{

printf("%d \ n",EOF);

返回0;

}

练习1-6给出我消化不良。这会编译和链接,但不会按照我的意愿行事:
#include< stdio.h>

int main(int orange,char ** mango)
{
int c;
c =(getchar()!= EOF);
printf("%d \t",c);
返回(0 );
}

当我在键盘上随机啄时,我正在寻找一个0或1,并且我在我的结束时,我也是似乎无法调试。




你的程序几乎是正确的。用\ n替换\t,它是完全正确的。

如果使用循环,使用getchar()!= EOF

评估会更好以0作为退出条件,但它甚至可以在您的版本中工作,

如果您了解stdin,当连接到终端时,行是

缓冲。因此,在按下Return / Enter键之前不会发生任何事情。

即使在eof键之后,您的平台也可能需要它。之后,你的

getchar()调用将返回你键入的第一个字符的代码

或EOF如果你输入了你的平台的相应字符

(DOS / Windows为CTRL-Z,Unix为CTRL-D)。因此,大多数时候你可以期望1输出为
输出,除非输入的第一个字符是产生文件结束条件的字符。

。 >

如果您使用循环并从文件重定向

stdin,则此程序更容易使用:


fangorn :〜/ tmp 146> cat输入

12345

fangorn:〜/ tmp 147> cat test.c

#include< stdio.h>


int main()

{

int cond;


while((cond = getchar()!= EOF)== 1)printf("%d",cond);

printf("%d \ n",cond);

返回0;

}

fangorn:〜/ tmp 148> gcc test.c

fangorn:〜/ tmp 149> ./a.out<输入

1 1 1 1 1 1 0

输入文件包含6个字符:五个可见的字符(12345)

和换行符。对于它们中的每一个,表达式

getchar()!= EOF的计算结果为1,并且程序中显示的是1 / b $ b输出。第七个getchar()调用在流上生成和结束文件状态

并返回EOF。此时,表达式

getchar()!= EOF计算结果为0,while循环终止。

显示表达式的相应值,后跟

强制性换行符。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da ***** @ ifh.de

目前在欧盟寻找工作


It''s very difficult to do an exercise with elementary tools. It took me
about fifteen minutes to get exercise 1-7:
#include <stdio.h>

int main(int orange, char **apple)
{
int c;
c=-5;
while(c != EOF )
{
c++;
}
printf("%d\t",c);
printf("%d\t",EOF);
return (0);
}
Since my printf''s agree that window''s EOF is Hamilton''s (ijk)^2, I think
it''s right.

Exercise 1-6 is giving me dyspepsia. This compiles and links but does not
behave according to my wishes:
#include <stdio.h>

int main(int orange, char **mango)
{
int c;
c=(getchar() != EOF);
printf("%d\t",c);
return (0);
}

I''m looking for a 0 or 1 when I peck randomly at the keyboard, and am at my
wit''s end, as I also seem to be unable to debug. Remember, if you kick a
man when he''s down, you better make sure he either stays down or he isn''t 2
meters tall, 114 kg. ++Thanks. MPJ

解决方案

On Mon, 11 Oct 2004 21:20:10 -0500, Merrill & Michele wrote:

It''s very difficult to do an exercise with elementary tools. It took me
about fifteen minutes to get exercise 1-7:
#include <stdio.h>

int main(int orange, char **apple)
Why the bizarre names for these variables? argc and argv, respectively,
have served nearly all C programmers very well.
{
int c;
c=-5;
while(c != EOF )
{
c++;
}
This is trivial enough. You should allow for the possibility for EOF to be
equal to a value like INT_MIN, in which case this program could run a
very, very long time. (Assuming it can wrap around from INT_MAX to INT_MIN
at all, and do so without crashing.)
printf("%d\t",c);
printf("%d\t",EOF);
You probably want a newline, not a tab, in at least one of these
statements.
return (0);
}
Since my printf''s agree that window''s EOF is Hamilton''s (ijk)^2, I think
it''s right.
It will be right, assuming EOF >= -5 on your machine.

Exercise 1-6 is giving me dyspepsia. This compiles and links but does not
behave according to my wishes:
#include <stdio.h>

int main(int orange, char **mango)
{
int c;
c=(getchar() != EOF);
This is the problem, I think. You''re expecting a coherent result from a
very strange statement.
printf("%d\t",c);
ITYM printf("%d\n",c);
return (0);
}

I''m looking for a 0 or 1 when I peck randomly at the keyboard, and am at my
wit''s end, as I also seem to be unable to debug.
I don''t know that either value is guaranteed by the Standard, or
reasonably expectable on any machines.
Remember, if you kick a
man when he''s down, you better make sure he either stays down or he isn''t 2
meters tall, 114 kg. ++Thanks. MPJ



You''ll get no kicks from me.


"Merrill & Michele" <be********@comcast.net> wrote:

It''s very difficult to do an exercise with elementary tools.
If you mean the exercises from K&R, I found it no problem at all doing
most of them using tools as elementary as paper and pen. Then again...
int main(int orange, char **apple)



....I wasn''t wilfully perverse, so maybe, just maybe, if you start taking
it seriously, you would get a bit farther?

Richard


In <cO********************@comcast.com> "Merrill & Michele" <be********@comcast.net> writes:

It''s very difficult to do an exercise with elementary tools. It took me
about fifteen minutes to get exercise 1-7:
#include <stdio.h>

int main(int orange, char **apple)
{
int c;
c=-5;
while(c != EOF )
{
c++;
}
printf("%d\t",c);
printf("%d\t",EOF);
return (0);
}
Since my printf''s agree that window''s EOF is Hamilton''s (ijk)^2, I think
it''s right.
Nope, it isn''t. Your while loop will eventually cause integer overflow,
which is undefined behaviour in C. Furthermore, it is entirely irrelevant
to your problem, whose solution is as simple as:

#include <stdio.h>

int main()
{
printf("%d\n", EOF);
return 0;
}
Exercise 1-6 is giving me dyspepsia. This compiles and links but does not
behave according to my wishes:
#include <stdio.h>

int main(int orange, char **mango)
{
int c;
c=(getchar() != EOF);
printf("%d\t",c);
return (0);
}

I''m looking for a 0 or 1 when I peck randomly at the keyboard, and am at my
wit''s end, as I also seem to be unable to debug.



Your program is almost correct. Replace \t by \n and it is fully correct.
It would have been even better if you used a loop, with getchar() != EOF
evaluating to 0 as the exit condition, but it works even in your version,
if you understand that stdin, when connected to the terminal, is line
buffered. So, nothing will happen until you press the Return/Enter key.
Your platform may require it even after the eof key. After that, your
getchar() call will return the code of the first character you have typed
or EOF if you have typed the corresponding character for your platform
(CTRL-Z for DOS/Windows, CTRL-D for Unix). So, you can expect a 1 as
output most of the time, unless the first character you have typed was
the one generating an end of file condition.

This program is much easier to work with if you use a loop and redirect
stdin from a file:

fangorn:~/tmp 146> cat input
12345
fangorn:~/tmp 147> cat test.c
#include <stdio.h>

int main()
{
int cond;

while ((cond = getchar() != EOF) == 1) printf("%d ", cond);
printf("%d\n", cond);
return 0;
}
fangorn:~/tmp 148> gcc test.c
fangorn:~/tmp 149> ./a.out <input
1 1 1 1 1 1 0

The input file contains 6 characters: the five visible ones (12345)
and the newline character. For each of them, the expression
getchar() != EOF evaluates to 1 and a 1 is displayed in the program
output. The seventh getchar() call generates and end of file condition
on the stream and it returns EOF. At that point, the expression
getchar() != EOF evaluates to 0 and the while loop is terminated.
The corresponding value of the expression is displayed, followed by
the mandatory newline character.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union


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

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