K& R练习1-18 [英] K&R exercise 1-18

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

问题描述

你好,


我一直在研究K& R书(目前只有第一章)的例子

和练习。经过大量的汗水和头发拉动,我想我有一个

解决方案,适用于第1页的前1-18页。


似乎工作但可能会丢失一些错误检查。你可以请

看看我的逻辑是否正确以及其他任何改进。


这不是作业,但它也可以。我想在C编程上获得一个

的处理能力,并且最终希望在接近退休时帮助GNU项目获得一些支持。


/ *函数从输入行中删除尾随空白和制表符* /

/ *而不打印任何空白行* /


#include< stdio.h>


#define最大1000 / *最大行长,包括''\'''标记* /


void remblank(char line []);


int main()

{

int c ,i;

i = 0;

char line [MAXIMUM];

c = getchar();

while (c!= EOF)

{

line [i] = c;

if(i ==(MAXIMUM - 1))/ *确保行是< = 1000 * /

line [i] =''\ n'';

if(line [i] =='' \ n'')

{

line [i + 1] =''\ 0'';

remblank(line) ;

i = 0;

}

其他

++ i;

c = getchar();

}


/ *打印最后一位,如果在最后一个换行符后有更多* /

/ * * /

if(c == EOF)

{

line [i] =''\ 0'';

while(line [i - 1] == (''\t'')|| line [i - 1] ==(''''))

{

line [i - 1] =''\ 0'';

--i;

}

printf("%s",line);

}

返回0;

}


void remblank(char s [])

{

int i;

for(i = 0; s [i]!=''\ n''; i ++)/ *计算行中的字符* /

;


/ *如果换行前的字符是空格或制表符* /

/ *用换行符替换它并制作下一个* /

/ *字符数组标记的结尾* /

while(s [i - 1] ==(''\t'')|| s [i - 1] ==(''''))

{

s [i -1] =''\ n'';

s [i] =''\ 0'';

- i;

}


/ *如果是不是空白,打印行没有* /

/ *尾随空格或标签* /

if(s [0]!=''\ n'')

printf("%s&q uot;,s);

}


谢谢,

Eric

解决方案



Eric ???é?????

你好,

我一直在研究K& R书(目前只有第一章)的例子和练习。经过大量的汗水和头发拉动之后,我认为我在第31页上有一个针对前1-18的解决方案。

它似乎有用,但可能会遗漏一些错误检查。你能否请你看看我的逻辑是否正确以及其他任何改进。

这不是一个功课,但它也可以。我正在努力获得关于C编程的处理能力,并且最终希望在我接近退休时帮助解决一些GNU项目。

/ *函数到从输入行* /
/ *剥去尾随空白和制表符而不打印任何空白行* /

#include< stdio.h>

#define MAXIMUM 1000 / *最大行长度,包括''\'''标记* /

void remblank(char line []);

int main()
{
int c,i;
i = 0;
char line [MAXIMUM];
c = getchar();
while(c!= EOF)
{line = [i] = c;
if(i ==(MAXIMUM - 1))/ *确保行是< = 1000 * /
行[i] =''\ n'';
if(line [i] ==''\ n'')
{
line [i + 1] ='' \\ 0'';
remblank(line);
i = 0;
}
其他
++ i;
c = getchar();
}
/ *如果在最后一个换行符之后有更多* /
/ *,则打印最后一位* /
如果(c == EOF)
{
line [i] =''\ 0'';
while(line [i - 1] ==(''\t'')|| line [i - 1] ==(''''))
{
line [i - 1] =''\ 0'';
- i;
}
printf("%s",line);
}
返回0;
}

void remblank(char s [])
{i / 0; s [i]!=''\ n''; i ++)/ *计算行中的字符* /
;

/ *如果换行前的字符是空格或制表符* /
/ *用换行符替换它并使下一个* /
/ *字符成为数组的结尾标记* /
while(s [i - 1] ==(''\t'')|| s [i - 1] ==(''''))
{
s [i -1] =''\ n'';
s [i] =''\ 0'';
- i;
}

/ *如果行不为空,则打印不带* /
的行/ *尾随空白或标签* /
if(s [0]!=''\ n'')
printf("%s",s);
}

谢谢你,

Eric



#include< stdio.h>

#include< ; stdlib.h>


#define MAXQUEUE 1001

int advance(int指针)

{

if(指针< MAXQUEUE - 1)

返回指针+ 1;

其他

返回0;

}


int main(无效)

{

char blank [MAXQUEUE];

int head,tail;

int nonspace;

int retval;

int c;

int spaceJustPrinted; / * boolean:是打印的最后一个字符

空格?* /


retval = spaceJustPrinted = nonspace = head = tail = 0;


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

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

head = tail = 0;

if(spaceJustPrinted == 1)/ *如果有一些尾随的空格是打印的...... * /

retval = EXIT_FAILURE;


if(nonspace){

putchar(''\ n'');

spaceJustPrinted = 0; / *这条指令不是真的

必要,因为

spaceJustPrinted仅用于

确定

返回值,但我们将保留此布尔值

如实* /

nonspace = 0; / *在条件内移动只是为了节省

不用

赋值* /

}

}

else if(c ==''''|| c ==''\t''){

if(advance(head)== tail){

putchar(空白[尾]); / *这些空白字符被打印

提前

只是一个问题,如果它们落后,

我们会在我们点击时检查a \ n或EOF

* /

spaceJustPrinted = 1;

tail = advance(尾);

nonspace = 1;

}


空白[head] = c;

head = advance(head);

}

else {

while(head!= tail){

putchar(blank [tail]);

tail = advance(尾);

}

putchar(c);

spaceJustPrinted = 0;

nonspace = 1;

}

}

如果最后一行不是
/ *在EOF之前以换行符结束,

我们需要弄清楚这里是否打印了尾随空格* /

if(spaceJustPrinted == 1)/ * if一些尾随的空白是

打印... * /

retval = EXIT_FAILURE;


返回retval;

}


qi ********* @ yahoo.com.cn 写道:
< blockquote class =post_quotes>
Eric ???é?????

你好,

我一直在研究K& ; R书(目前仅有第一章)
实例和练习。经过大量的汗水和头发拉动之后,我认为我在第31页上有一个针对前1-18的解决方案。

它似乎有用,但可能会遗漏一些错误检查。你能否请你看看我的逻辑是否正确以及其他任何改进。

这不是一个功课,但它也可以。我正在尝试处理C编程,并最终希望在我接近退休时帮助解决一些GNU项目。

....程序切断以节省空间....
谢谢,

Eric



#include< stdio.h>
#include< stdlib.h>

#define MAXQUEUE 1001
int advance(int指针)
{
if(指针< MAXQUEUE - 1)
返回指针+ 1;

返回0;
}
int main(void)
{
char blank [MAXQUEUE];
int head,tail;
int nonspace;
int retval;
int c;
int spaceJustPrinted; / * boolean:是打印的最后一个字符
空格?* /

retval = spaceJustPrinted = nonspace = head = tail = 0;

while((c = getchar())!= EOF){
if(c ==''\ n''){
head = tail = 0;
if(spaceJustPrinted == 1)/ *如果打印了一些尾随的空白...... * /
retval = EXIT_FAILURE;

if(nonspace){
putchar(''\ n'') ;
spaceJustPrinted = 0; / *这条指令实际上并不是必需的,因为
spaceJustPrinted仅用于确定
返回值,但我们会保持这个布尔值
真实* /
非空间= 0; / *在条件内移动只是为了保存一个不必要的
任务* /
}
}
如果(c ==''''|| c == ''\t''){
if(advance(head)== tail){
putchar(blank [tail]); / *这些空白字符被打印
早期
只是一个问题,如果它们落后,
我们将检查当我们点击\\\
或EOF
* /
spaceJustPrinted = 1;
tail = advance(tail);
nonspace = 1;
}

空白[head] = c;
head = advance(head);
}
else {
while(head!= tail){
putchar(空白[tail]);
tail = advance (尾);
}
putchar(c);
spaceJustPrinted = 0;
nonspace = 1;
}
}

/ *如果最后一行在EOF之前没有用换行符结束,我们需要弄清楚是否在这里打印了尾随空格* /
if(spaceJustPrinted == 1 )/ *如果打印了一些尾随的空格...... * /
retval = EXIT_FAILURE;

返回retval;
}




感谢您的回复。我需要一段时间才能完全消化这个并且

了解你的程序背后的逻辑。你的变量更多是描述性的,我需要记住这样做。我用

编译并测试了一个测试文件,我使用不同的行长度和尾随的标签和

空白。它给了我与我的版本相同的结果。我是b / b
在布尔逻辑语句(==,!=,||)中苦苦挣扎,因为我使用真值表并且布尔值非常好,因此
让我感到惊讶当天回来
代数。


谢谢,


Eric

删除" ;所有的垃圾邮件回复。


> Eric ???é?????


我一直在研究K& R书(目前只有第一章)的例子。和>练习。经过大量的汗水和头发拉动,我认为我有一个解决方案,请参阅第31页的前1-18。


< snip>

qi ********* @ yahoo.com.cn 写道:#include< stdio.h> ;
#include< stdlib.h>

#define MAXQUEUE 1001



< snip>


您可以告知OP可以从哪里获取代码。


看起来非常类似于......

http://users.powernet.co.uk/eton/kandr2/krx118 .html


-

彼得


Hello,

I have been working through the K&R book (only chapter one so far) examples
and exercises. After much sweat and hair pulling, I think I have a
solution for ex 1-18 on page 31.

It seems to work but may be missing some error checking. Can you please
take a look and see if my logic is correct and any other improvements.

This is not a homework but it could just as well be. I am trying to get a
handle on C programming and would eventually like to help out with some of
the GNU projects as I get closer to retirement.

/* function to strip trailing blanks and tabs from the input line */
/* and not to print any blank lines */

#include <stdio.h>

#define MAXIMUM 1000 /* maximum line length including ''\0'' marker */

void remblank(char line[]);

int main()
{
int c, i;
i = 0;
char line[MAXIMUM];
c = getchar();
while (c != EOF)
{
line[i] = c;
if (i == (MAXIMUM - 1)) /* make sure line is <= 1000 */
line[i] = ''\n'';
if (line[i] == ''\n'')
{
line[i + 1] = ''\0'';
remblank(line);
i = 0;
}
else
++i;
c = getchar();
}

/* print the last bit if there is more */
/* after the last newline */
if (c == EOF)
{
line[i] = ''\0'';
while (line[i - 1] == (''\t'') || line[i - 1] == ('' ''))
{
line[i - 1] = ''\0'';
--i;
}
printf("%s", line);
}
return 0;
}

void remblank(char s[])
{
int i;
for(i = 0; s[i] != ''\n''; i++) /* count characters in line */
;

/* if character before newline is a blank or tab */
/* replace it with a newline and make the next */
/* character an end of array marker */
while (s[i - 1] == (''\t'') || s[i - 1] == ('' ''))
{
s[i -1] = ''\n'';
s[i] = ''\0'';
--i;
}

/* if line is not blank, print the line without */
/* the trailing blanks or tabs */
if(s[0] != ''\n'')
printf("%s", s);
}

Thank you,

Eric

解决方案


Eric ???é?????

Hello,

I have been working through the K&R book (only chapter one so far) examples
and exercises. After much sweat and hair pulling, I think I have a
solution for ex 1-18 on page 31.

It seems to work but may be missing some error checking. Can you please
take a look and see if my logic is correct and any other improvements.

This is not a homework but it could just as well be. I am trying to get a
handle on C programming and would eventually like to help out with some of
the GNU projects as I get closer to retirement.

/* function to strip trailing blanks and tabs from the input line */
/* and not to print any blank lines */

#include <stdio.h>

#define MAXIMUM 1000 /* maximum line length including ''\0'' marker */

void remblank(char line[]);

int main()
{
int c, i;
i = 0;
char line[MAXIMUM];
c = getchar();
while (c != EOF)
{
line[i] = c;
if (i == (MAXIMUM - 1)) /* make sure line is <= 1000 */
line[i] = ''\n'';
if (line[i] == ''\n'')
{
line[i + 1] = ''\0'';
remblank(line);
i = 0;
}
else
++i;
c = getchar();
}

/* print the last bit if there is more */
/* after the last newline */
if (c == EOF)
{
line[i] = ''\0'';
while (line[i - 1] == (''\t'') || line[i - 1] == ('' ''))
{
line[i - 1] = ''\0'';
--i;
}
printf("%s", line);
}
return 0;
}

void remblank(char s[])
{
int i;
for(i = 0; s[i] != ''\n''; i++) /* count characters in line */
;

/* if character before newline is a blank or tab */
/* replace it with a newline and make the next */
/* character an end of array marker */
while (s[i - 1] == (''\t'') || s[i - 1] == ('' ''))
{
s[i -1] = ''\n'';
s[i] = ''\0'';
--i;
}

/* if line is not blank, print the line without */
/* the trailing blanks or tabs */
if(s[0] != ''\n'')
printf("%s", s);
}

Thank you,

Eric


#include <stdio.h>
#include <stdlib.h>

#define MAXQUEUE 1001

int advance(int pointer)
{
if (pointer < MAXQUEUE - 1)
return pointer + 1;
else
return 0;
}

int main(void)
{
char blank[MAXQUEUE];
int head, tail;
int nonspace;
int retval;
int c;
int spaceJustPrinted; /*boolean: was the last character printed
whitespace?*/

retval = spaceJustPrinted = nonspace = head = tail = 0;

while ((c = getchar()) != EOF) {
if (c == ''\n'') {
head = tail = 0;
if (spaceJustPrinted == 1) /*if some trailing whitespace was
printed...*/
retval = EXIT_FAILURE;

if (nonspace) {
putchar(''\n'');
spaceJustPrinted = 0; /* this instruction isn''t really
necessary since
spaceJustPrinted is only used to
determine the
return value, but we''ll keep this boolean
truthful */
nonspace = 0; /* moved inside conditional just to save a
needless
assignment */
}
}
else if (c == '' '' || c == ''\t'') {
if (advance(head) == tail) {
putchar(blank[tail]); /* these whitespace chars being printed
early
are only a problem if they are trailing,
which we''ll check when we hit a \n or EOF
*/
spaceJustPrinted = 1;
tail = advance(tail);
nonspace = 1;
}

blank[head] = c;
head = advance(head);
}
else {
while (head != tail) {
putchar(blank[tail]);
tail = advance(tail);
}
putchar(c);
spaceJustPrinted = 0;
nonspace = 1;
}
}

/* if the last line wasn''t ended with a newline before the EOF,
we''ll need to figure out if trailing space was printed here */
if (spaceJustPrinted == 1) /*if some trailing whitespace was
printed...*/
retval = EXIT_FAILURE;

return retval;
}


qi*********@yahoo.com.cn wrote:


Eric ???é?????

Hello,

I have been working through the K&R book (only chapter one so far)
examples
and exercises. After much sweat and hair pulling, I think I have a
solution for ex 1-18 on page 31.

It seems to work but may be missing some error checking. Can you please
take a look and see if my logic is correct and any other improvements.

This is not a homework but it could just as well be. I am trying to get
a handle on C programming and would eventually like to help out with some
of the GNU projects as I get closer to retirement.
....program cut off to save space ....
Thank you,

Eric


#include <stdio.h>
#include <stdlib.h>

#define MAXQUEUE 1001

int advance(int pointer)
{
if (pointer < MAXQUEUE - 1)
return pointer + 1;
else
return 0;
}

int main(void)
{
char blank[MAXQUEUE];
int head, tail;
int nonspace;
int retval;
int c;
int spaceJustPrinted; /*boolean: was the last character printed
whitespace?*/

retval = spaceJustPrinted = nonspace = head = tail = 0;

while ((c = getchar()) != EOF) {
if (c == ''\n'') {
head = tail = 0;
if (spaceJustPrinted == 1) /*if some trailing whitespace was
printed...*/
retval = EXIT_FAILURE;

if (nonspace) {
putchar(''\n'');
spaceJustPrinted = 0; /* this instruction isn''t really
necessary since
spaceJustPrinted is only used to
determine the
return value, but we''ll keep this boolean
truthful */
nonspace = 0; /* moved inside conditional just to save a
needless
assignment */
}
}
else if (c == '' '' || c == ''\t'') {
if (advance(head) == tail) {
putchar(blank[tail]); /* these whitespace chars being printed
early
are only a problem if they are trailing,
which we''ll check when we hit a \n or EOF
*/
spaceJustPrinted = 1;
tail = advance(tail);
nonspace = 1;
}

blank[head] = c;
head = advance(head);
}
else {
while (head != tail) {
putchar(blank[tail]);
tail = advance(tail);
}
putchar(c);
spaceJustPrinted = 0;
nonspace = 1;
}
}

/* if the last line wasn''t ended with a newline before the EOF,
we''ll need to figure out if trailing space was printed here */
if (spaceJustPrinted == 1) /*if some trailing whitespace was
printed...*/
retval = EXIT_FAILURE;

return retval;
}



Thank you for your reply. It will take me a while to fully digest this and
understand the logic behind your program. Your variables are much more
descriptive which I need to remember to do. I compiled and tested it with
a test file I made with different line lengths with trailing tabs and
blanks. It gave me the same results that I got with my version. I am
struggling through the boolean logic statements (==, !=, ||) which
surprised me as I use to be pretty good with truth tables and boolean
algebra back in the day.

Thanks,

Eric
Remove "all the spam" to reply.


> Eric ???é?????


I have been working through the K&R book (only chapter one so far) examples
and exercises. After much sweat and hair pulling, I think I have a
solution for ex 1-18 on page 31.

<snip>

qi*********@yahoo.com.cn wrote: #include <stdio.h>
#include <stdlib.h>

#define MAXQUEUE 1001


<snip>

You could have informed the OP where the code could be sourced from.

It looks quite similar to that at...

http://users.powernet.co.uk/eton/kandr2/krx118.html

--
Peter


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

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