K& r第1章; EXC-17 [英] K&r chapter-1; exc-17

查看:53
本文介绍了K& r第1章; EXC-17的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Q.编写一个程序来打印超过80

个字符的所有输入行。


代码来自
http://users.powernet.co .uk / eton / kandr2 / krx117.html


========================代码====================== =====

#include< stdio.h>

#define MINLENGTH 81

int readbuff(char * buffer){

size_t i = 0;

int c;

while(i< MINLENGTH){

c = getchar();

if(c == EOF)return -1;

if(c ==''\ n'')返回0;

buffer [i ++] = c;

}

返回1;

}


int copyline(char * buffer){

size_t i;

int c;

int status = 1;

for(i = 0; i< MINLENGTH; i ++)

putchar (buffer [i]);

while(status == 1){

c = getchar();

if(c == EOF)

status = -1;

else if(c ==''\ n'')

status = 0;

else

putchar(c);

}

putchar (''\ n'');

返回状态;

}


int main(void){

char buffer [MINLENGTH];

int status = 0;

while(status!= -1){

status = readbuff(buffer);

if(status == 1)

status = copyline(buffer);

}

返回0;

}

========================== ======代码============== ======

我的怀疑是:


1)为什么copyline和readbuff中有两个getchar函数。可以用一个

全局缓冲区指针完成
吗?


2)主函数中的状态变量是1, 0,-1。我看过

代码使用这种检查函数的返回类型。那是

首先用0或1初始化变量,然后检查

带有函数返回类型的变量的修改值。这个

练习是否带有任何名字?

-

组合是国际象棋的核心

A.Alekhine

邮寄地址:

sathyashrayan at gmail DOT com

(AT = @和DOT =。)


Q. Write a program to print all input lines that are longer than 80
characters.

Code taken from
http://users.powernet.co.uk/eton/kandr2/krx117.html

========================code====================== =====
#include <stdio.h>
#define MINLENGTH 81

int readbuff(char *buffer) {
size_t i=0;
int c;
while (i < MINLENGTH) {
c = getchar();
if (c == EOF) return -1;
if (c == ''\n'') return 0;
buffer[i++] = c;
}
return 1;
}

int copyline(char *buffer) {
size_t i;
int c;
int status = 1;
for(i=0; i<MINLENGTH; i++)
putchar(buffer[i]);
while(status == 1) {
c = getchar();
if (c == EOF)
status = -1;
else if (c == ''\n'')
status = 0;
else
putchar(c);
}
putchar(''\n'');
return status;
}

int main(void) {
char buffer[MINLENGTH];
int status = 0;
while (status != -1) {
status = readbuff(buffer);
if (status == 1)
status = copyline(buffer);
}
return 0;
}
================================code============== ======
My doubts are:

1)Why there are two getchar functions in copyline and readbuff. Can it
be done with one
global buffer pointer?

2) status variable in the main function is either 1 ,0,-1. I have seen
codes uses this kind of checking a function?s return type. That is
initializing the variable at first either with 0 or 1 and then checking
the modified value of that variable with function return type. Does this
practice carry any name?
--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)

推荐答案



" sathya" < SA **** @ nomail.com>在消息中写道

news:41 *************** @ nomail.com ...

"sathya" <sa****@nomail.com> wrote in message
news:41***************@nomail.com...

Q.编写一个程序来打印超过80个字符的所有输入行。

代码取自
http://users.powernet.co.uk/eton/kandr2/krx117.html
======================== code ====================== == ===
#include< stdio.h>
#define MINLENGTH 81
int readbuff(char * buffer){
size_t i = 0;
int c;
while(i< MINLENGTH){
c = getchar();
如果(c == EOF)返回-1;
if(c = =''\ n'')返回0;
缓冲区[i ++] = c;
}
返回1;
}
int copyline (char * buffer){
size_t i;
int c;
int status = 1;
for(i = 0; i< MINLENGTH; i ++)
putchar (缓冲液[I ]);
while(status == 1){
c = getchar();
if(c == EOF)
status = -1;
else if(c ==''\ n'')
status = 0;

putchar(c);
}
putchar(''\ n'');
返回状态;

int main(void){
char buffer [MINLENGTH];
int status = 0;
while(status!= -1){
status = readbuff(buffer);
if(status == 1)
status = copyline(buffer);
}
返回0;
}
================================代码============== ======
我的怀疑是:

1)为什么copyline和readbuff中有两个getchar函数。可以用一个
全局缓冲区指针完成吗?

2)主函数中的状态变量是1,0,-1。我看过
代码使用这种检查函数的返回类型。那就是首先用0或1初始化变量,然后用函数返回类型检查该变量的修改值。这个
练习是否带有任何名称?

Q. Write a program to print all input lines that are longer than 80
characters.

Code taken from
http://users.powernet.co.uk/eton/kandr2/krx117.html

========================code====================== =====
#include <stdio.h>
#define MINLENGTH 81

int readbuff(char *buffer) {
size_t i=0;
int c;
while (i < MINLENGTH) {
c = getchar();
if (c == EOF) return -1;
if (c == ''\n'') return 0;
buffer[i++] = c;
}
return 1;
}

int copyline(char *buffer) {
size_t i;
int c;
int status = 1;
for(i=0; i<MINLENGTH; i++)
putchar(buffer[i]);
while(status == 1) {
c = getchar();
if (c == EOF)
status = -1;
else if (c == ''\n'')
status = 0;
else
putchar(c);
}
putchar(''\n'');
return status;
}

int main(void) {
char buffer[MINLENGTH];
int status = 0;
while (status != -1) {
status = readbuff(buffer);
if (status == 1)
status = copyline(buffer);
}
return 0;
}
================================code============== ======
My doubts are:

1)Why there are two getchar functions in copyline and readbuff. Can it
be done with one
global buffer pointer?

2) status variable in the main function is either 1 ,0,-1. I have seen
codes uses this kind of checking a function''s return type. That is
initializing the variable at first either with 0 or 1 and then checking
the modified value of that variable with function return type. Does this
practice carry any name?




虽然K& R在假期洗牌时不知何故离开了我,但我认为

此代码忽略了文本的开发。特别是,

指令是写有问题的程序,而不是偷猎

它。除此之外,你的问题是热门话题,但超出了我能够以任何程度的真实性回答

的能力。 MPJ



Although K&R has somehow gotten away from me in the holiday shuffle, I think
this code ignores the development of the text. In particular, the
instructions are to WRITE the program in question, as opposed to poaching
it. That aside, your questions are topical but beyond my ability to answer
with any degree of veracity. MPJ


sathya写道:
sathya wrote:
问:编写一个程序来打印超过80的所有输入行
字符。

代码取自
http://users.powernet.co.uk/eton/kandr2/krx117.html

================ ========代码====================== =====
#include< stdio.h>
#define MINLENGTH 81
int readbuff(char * buffer){
size_t i = 0;
int c;
while(i< MINLENGTH){
c = getchar();
如果(c == EOF)返回-1;
if(c ==''\ n'')返回0;
缓冲区[i ++] = c;
}
返回1;
}
int copyline(char * buffer){
size_t i;
int c;
int status = 1;
for(i = 0; i< MINLENGTH; i ++)
putchar(buffer [i]);
while(status == 1){
c = getchar();
if(c == EOF)
status = -1;
else if(c =='' \ n'')
status = 0;

putchar(c);
}
putchar(''\ n'');
int main(void){
char buffer [MINLENGTH];
int status = 0;
while(status) != -1){
status = readbuff(buffer);
if(status == 1)
status = copyline(buffer);
}
返回0 ;
}
================================ code ======= ======= ======
我的怀疑是:

1)为什么copyline和readbuff中有两个getchar函数。


如果你从其他人那里拿走代码并且不理解它:

试一试!

如果我们有一条线使用> 80个字符,readbuff()中的getchar()调用

用于填充前80个字符的buf。

在copyline()中,我们写了buf的内容一次打印然后打印

输出所有多余的字符直到行尾或直到

我们遇到EOF。

由于buf足够大,我们无法读取超过80个字符进入buf。

另一方面,如果我们想要一次性输出所有字符

使用getchar ()/ putchar()组合,我们必须返回

80个字符,因为我们知道我们有超过80个字符,因为我们只有

" used向上"这些80.

可以用一个
全局缓冲区指针完成吗?


我不确定你的意思。如果你想在文件范围内声明buf

而不是将buf传递给readbuf()或copyline(),那么
这是一个坏主意。阅读你的C书或这个小组的常见问题解答

找出原因。
再一次:尝试一下!

如果你有一个想法,试试吧。然后,您可以回来向我们展示。


2)主函数中的状态变量是1,0,-1。我看过
代码使用这种检查函数的返回类型。那就是首先用0或1初始化变量,然后用函数返回类型检查该变量的修改值。这个
练习是否带有任何名称?
Q. Write a program to print all input lines that are longer than 80
characters.

Code taken from
http://users.powernet.co.uk/eton/kandr2/krx117.html

========================code====================== =====
#include <stdio.h>
#define MINLENGTH 81

int readbuff(char *buffer) {
size_t i=0;
int c;
while (i < MINLENGTH) {
c = getchar();
if (c == EOF) return -1;
if (c == ''\n'') return 0;
buffer[i++] = c;
}
return 1;
}

int copyline(char *buffer) {
size_t i;
int c;
int status = 1;
for(i=0; i<MINLENGTH; i++)
putchar(buffer[i]);
while(status == 1) {
c = getchar();
if (c == EOF)
status = -1;
else if (c == ''\n'')
status = 0;
else
putchar(c);
}
putchar(''\n'');
return status;
}

int main(void) {
char buffer[MINLENGTH];
int status = 0;
while (status != -1) {
status = readbuff(buffer);
if (status == 1)
status = copyline(buffer);
}
return 0;
}
================================code============== ======
My doubts are:

1)Why there are two getchar functions in copyline and readbuff.
If you take code from someone else and do not understand it:
Try it out!
If we have a line with >80 characters, the getchar() calls in readbuff()
were used to fill buf with the first 80 characters.
In copyline(), we write the contents of buf in one go and then print
out all the excess characters up to the end of the line or until
we encounter EOF.
We cannot read more than 80 characters into buf as buf is large enough.
On the other hand, if we wanted to output all characters in one go
with a getchar()/putchar() combination, we would have to "go back"
80 characters at realizing that we have more than 80 as we have just
"used up" these 80.
Can it
be done with one
global buffer pointer?
I am not sure what you mean by that. If you want to declare buf
on file scope and not pass buf to either readbuf() or copyline(),
that is a Bad Idea. Read your C book or this group''s FAQ to
find out why.
Once more: Try it out!
If you have an idea, try it. Then, you can come back and show us.

2) status variable in the main function is either 1 ,0,-1. I have seen
codes uses this kind of checking a function?s return type. That is
initializing the variable at first either with 0 or 1 and then checking
the modified value of that variable with function return type. Does this
practice carry any name?




状态可以用-1以外的任何东西进行初始化,以便程序

起作用。

你可以采取任何三个值来做到这一点。此外,你可以

#define ENCOUNTERED_EOF -1

#define GEQ_MINLENGTH 1

使用它们代替-1/1,重新定义它们有任何价值和

等等。而不是0,你可以返回_anything_但是ENCOUNTERED_EOF

和GEQ_MINLENGTH。

< 0,== 0,> 0通常用于表示是否比较了某些东西/>
小于,等于,大于其他东西。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



status can be initialized with anything but -1 for the program
to work.
You could take any three values to do it. Moreover, you can
#define ENCOUNTERED_EOF -1
#define GEQ_MINLENGTH 1
use them instead of -1/1, redefine them to have any values and
so on. Instead of 0, you can return _anything_ but ENCOUNTERED_EOF
and GEQ_MINLENGTH.
<0, ==0, >0 are often used to signal whether something compared
less than, equal to, greater than something else.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Merrill& Michele写道:


[snip:一个不是由OP写的程序的主题问题]
Merrill & Michele wrote:

[snip: Topical question to a program not written by the OP]


虽然K& R有在假日洗牌中不知何故离开了我,我想这个代码忽略了文本的发展。特别是,
指令是编写有问题的程序,而不是偷猎它。除此之外,你的问题是热门话题,但我无法以任何程度的准确性回答。 MPJ


Although K&R has somehow gotten away from me in the holiday shuffle, I think
this code ignores the development of the text. In particular, the
instructions are to WRITE the program in question, as opposed to poaching
it. That aside, your questions are topical but beyond my ability to answer
with any degree of veracity. MPJ




虽然你正确地提醒说OP会做得更好

自己编写程序,但最后一句话暴露了你

不会回答它(或者它的一部分)或提出与他的请求有关的专题

问题 - 并且你知道它。 />
这是不好的做法。如果你的不良做法相对于你的热门帖子,帖子超过了某个

的门槛,你最终将获得更多的
更多的killfiles。

如果你不能包含你的敦促说出来的话,随意做

所以这是值得欢迎的。有很多新闻组。来这里只需要

for C.

-Michael

-

电子邮件:我的是/ at / gmx / dot / de address。



While you are right on the reminder that the OP would do better
to write the program himself, the last sentence exposes that you
are not going to either answer it (or part of it) or pose a topical
question related to his request -- and that you are aware of it.
This is bad practice. If your "bad practice" posts exceed a certain
threshold in relation to your topical posts, you will end up in many
more killfiles.
If you cannot contain your urge to spill out words, feel free to do
so where this is welcome. There are many newsgroups. Come here only
for C.
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于K&amp; r第1章; EXC-17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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