K& R2运动问题 [英] K&R2 exercise question

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

问题描述

好的,我刚刚完成了一个C级课程,现在我正在通过K& R2(以及C99标准)工作
来*真正*学习C.


所以无论如何,我正在进行第一章的练习,它给了我一些奇怪的行为。这是我写的代码:

/ ******************************* ******************* ****************************
* K& R2练习1-9

*编写程序将其输入复制到其输出,替换字符串

空白

*,一张空白。

********************************* ***************** **************************** /

#include< stdio.h>


int main(无效){


int c = 0,lastc = 0 ;(b = getchar())!= EOF){


if(c ==''''){

if(lastc!=''''){//如果c *是*空格而且lastc是

putchar(c); // *不是* a空格,打印字符

lastc = c;

} //结束内部如果

} //结束外部如果


if(c!=''''){//如果c不是空间不用担心它

putchar(c);

lastc = c;

} //结束如果

} //结束时


re转0;

}

我遇到的问题是,当我运行程序时,我没有执行

执行,只是返回提示。但是,当我在它上面运行gdb

(即使我只是运行gdb中的程序)它可以工作,甚至可以实现我预期的b $ b。任何人都可以向我解释这个吗?


crr

解决方案

2004年7月5日星期一17: 40:30 -0700,Chris Readle< c。****** @ cox.net>

在comp.lang.c中写道:

好的,我刚刚完成了一个C级课程,现在我正在通过K& R2(以及C99标准)来*真正*学习C.

所以无论如何,我正在进行第一章的练习,这给我带来了奇怪的行为。这是我写的代码:
/ *********************************** *************** ****************************
* K& R2练习1-9
*编写一个程序将其输入复制到其输出中,用一个空白替换空格
*
******* ******************************************* ******* ********************* /
#include< stdio.h>

int main(void){

int c = 0,lastc = 0;

while((c = getchar())!= EOF){

if(c == ''''){
if(lastc!=''''){//如果c *是*空格而lastc是
putchar(c); // *不是*空格,打印字符
lastc = c;
} // end inner if
} // end outer if

if(c!=''''){ //如果c不是空间我不担心它


我希望你知道上面的行可以替换为:

else {putchar(c);
lastc = c;
} //结束如果
} //结束wh ile

返回0;
}

我遇到的问题是,当我运行程序时,我没有执行,只是返回提示。但是,当我在其上运行gdb
时(即使我只是运行gdb中的程序)它可以工作,甚至可以实现我的预期。有人可以向我解释这个吗?

crr




您的输入是否包含换行符?在C中,实现定义

是否输出到文本流的最后一行是否需要最终的

''\ n''。某些实现不会将

文本的最后一行输出到显示设备(如果它没有以换行符结尾)。


尝试添加:


putchar(''\ n'');


....就在main()返回的上方。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http:/ /www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a ... FAQ-acllc.html


Jack Klein写道:

2004年7月5日星期一17:40:30 -0700,Chris Readle< c。****** @ cox.net>
在comp.lang.c中写道:

好的,我刚刚完成了一个C级课程,现在我正在通过K& R2(以及C99标准)来工作*真的*学习C.

所以无论如何,我正在第一章的练习中给我带来奇怪的行为。这是我写的代码:
/ *********************************** *************** ****************************
* K& R2练习1-9
*编写一个程序将其输入复制到其输出中,用一个空白替换空格
*
******* ***************************************** ********* ********************* /
#include< stdio.h>

int main(void){

int c = 0,lastc = 0;

while((c = getchar())!= EOF){

if(c == ''''){
if(lastc!=''''){//如果c *是*空格而lastc是
putchar(c); // *不是*空格,打印字符
lastc = c;
} // end inner if
} // end outer if

if(c!=''''){ //如果c不是空间我们不担心它

我希望你知道上面的行可以替换为:
else {


是的,我知道,但我正在努力追随这本书是的,因为它还没有包括其他的... bb> $ block $ =post_quotes> putchar(c);
lastc = c;
} //结束如果
} //结束时

返回0;
}

我遇到的问题是,当我运行程序时,我没有执行
,只是返回提示。但是,当我在其上运行gdb
时(即使我只是运行gdb中的程序)它可以工作,甚至可以实现我的预期。任何人都可以向我解释这个吗?

crr



您的输入是否包含换行符?在C中,它是实现定义的
输出到文本流的最后一行是否需要最终的
''\ n''。如果没有以换行符结尾,某些实现不会将最后一行
文本输出到显示设备。

尝试添加:

putchar('' \ n'');

...正好在main()的返回上方。



好​​吧,输入*会*包含换行符,但是当它有问题

之前描述的时候,我不能输入任何东西。基本上,我打电话给

程序,按回车键立即返回到提示符。


我已对此做了一些进一步的研究,我发现了如果

我启动了一个新的shell实例,它第一次运行时只是花花公子,

但后续运行会返回到提示而不允许任何输入。


我尝试在main(),

中的return语句之前添加换行符但是当我调用

程序。


crr


On Mon,05 Jul 2004 17:40:30 -0700, Chris Readle写道:

任何人都可以向我解释这个吗?




在猜测时,exec称为test。你在命令行中输入''test''来运行它。


尝试''测试'' - 不知何故我不知道我认为你正在运行你的程序

认为你正在运行。


Ok, I''ve just recently finished a beginning C class and now I''m working
through K&R2 (alongside the C99 standard) to *really* learn C.

So anyway, I''m working on an exercise in chapter one which give me
strange behavior. Here is the code I''ve written:
/************************************************** ****************************
* K&R2 Exercise 1-9
* Write a program to copy its input to its output, replacing strings
of blanks
* with a single blank.
************************************************** ****************************/
#include <stdio.h>

int main(void) {

int c= 0, lastc= 0;

while((c= getchar()) != EOF) {

if (c == '' '') {
if (lastc != '' '') {//if c *is* a space and lastc is
putchar(c);//*not* a space, print the char
lastc= c;
}//end inner if
}//end outer if

if (c != '' '') {//if c isn''t a space don''t worry about it
putchar(c);
lastc =c;
}//end if
}//end while

return 0;
}
The problem I''m having is that when I run the program, I get no
execution, just returned to a prompt. However, when I run gdb on it
(even if I just "run" the program within gdb) it works, and even does
what I expected it to. Can anyone explain this one to me?

crr

解决方案

On Mon, 05 Jul 2004 17:40:30 -0700, Chris Readle <c.******@cox.net>
wrote in comp.lang.c:

Ok, I''ve just recently finished a beginning C class and now I''m working
through K&R2 (alongside the C99 standard) to *really* learn C.

So anyway, I''m working on an exercise in chapter one which give me
strange behavior. Here is the code I''ve written:
/************************************************** ****************************
* K&R2 Exercise 1-9
* Write a program to copy its input to its output, replacing strings
of blanks
* with a single blank.
************************************************** ****************************/
#include <stdio.h>

int main(void) {

int c= 0, lastc= 0;

while((c= getchar()) != EOF) {

if (c == '' '') {
if (lastc != '' '') {//if c *is* a space and lastc is
putchar(c);//*not* a space, print the char
lastc= c;
}//end inner if
}//end outer if

if (c != '' '') {//if c isn''t a space don''t worry about it
I hope you know that the line above could be replaced with:
else { putchar(c);
lastc =c;
}//end if
}//end while

return 0;
}
The problem I''m having is that when I run the program, I get no
execution, just returned to a prompt. However, when I run gdb on it
(even if I just "run" the program within gdb) it works, and even does
what I expected it to. Can anyone explain this one to me?

crr



Does your input contain a newline? In C it is implementation defined
whether the final line of output to a text stream requires a final
''\n'' or not. Some implementations will not output the final line of
text to the display device if it does not end in a newline.

Try adding:

putchar(''\n'');

....just above the return from main().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Jack Klein wrote:

On Mon, 05 Jul 2004 17:40:30 -0700, Chris Readle <c.******@cox.net>
wrote in comp.lang.c:

Ok, I''ve just recently finished a beginning C class and now I''m working
through K&R2 (alongside the C99 standard) to *really* learn C.

So anyway, I''m working on an exercise in chapter one which give me
strange behavior. Here is the code I''ve written:
/************************************************** ****************************
* K&R2 Exercise 1-9
* Write a program to copy its input to its output, replacing strings
of blanks
* with a single blank.
************************************************ ******************************/
#include <stdio.h>

int main(void) {

int c= 0, lastc= 0;

while((c= getchar()) != EOF) {

if (c == '' '') {
if (lastc != '' '') {//if c *is* a space and lastc is
putchar(c);//*not* a space, print the char
lastc= c;
}//end inner if
}//end outer if

if (c != '' '') {//if c isn''t a space don''t worry about it

I hope you know that the line above could be replaced with:
else {


Yes, I knew that, but I''m trying to follow the book closely and since it
hasn''t covered else yet...

putchar(c);
lastc =c;
}//end if
}//end while

return 0;
}
The problem I''m having is that when I run the program, I get no
execution, just returned to a prompt. However, when I run gdb on it
(even if I just "run" the program within gdb) it works, and even does
what I expected it to. Can anyone explain this one to me?

crr


Does your input contain a newline? In C it is implementation defined
whether the final line of output to a text stream requires a final
''\n'' or not. Some implementations will not output the final line of
text to the display device if it does not end in a newline.

Try adding:

putchar(''\n'');

...just above the return from main().


Well, the input *would* contain a newline, but when it has the problem
previously described, I don''t get to input anything. Basically, I call
the program, press enter and am immediately returned to the prompt.

I have done some further research on this, and I have discovered that if
I start a new instance of the shell, it runs just dandy the first time,
but subsequent runs return to the prompt without allowing any input.

I tried adding the newline just before the return statement in main(),
but all that does is put an additional newline on the screen when I call
the program.

crr


On Mon, 05 Jul 2004 17:40:30 -0700, Chris Readle wrote:

Can anyone explain this one to me?



At a guess- the exec is called test. You''re running it by typing ''test'' in
the command line.

Try ''which test'' - somehow I don''t think you''re running the program you
think you''re running.


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

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