C编程:现代方法 - 第15章练习5 [英] C Programming: A Modern Approach - Chapter 15 Exercise 5

查看:47
本文介绍了C编程:现代方法 - 第15章练习5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否有人为

K.N.的第15章练习5提供解决方案或提示。 King的书C编程:现代方法?我昨天晚上花了所有的时间来盯着它并流口水。作者确实有一个

的诀窍让我觉得自己像个笨蛋,或者也许只是我自己。


下面的代码垫在单词之间有额外空格的句子所以

每行占用屏幕上相同的空间量。当分配额外的空格时,

代码将支持该行的末尾。

任务是修改它以便它在
额外的空格有利于行的结尾和行的开头。


void write_line(void)

{

int extra_spaces,spaces_to_insert,i,j;


extra_spaces = MAX_LINE_LEN - line_len;

for(i = 0; i< line_len ; i ++){

if(line [i]!='''')

putchar(line [i]);

else {

spaces_to_insert = extra_spaces /(num_words - 1);

for(j = 1; j< = spaces_to_insert + 1; j ++)

putchar('''');

extra_spaces - = spaces_to_insert;

num_words--;

}

}

putchar(''\ n'');

}


我不能为我的生活图除了首先解析

语句并添加num之外的解决方案

将句子打印到屏幕时使用的数组空格,但这感觉就像一个糟糕的kludge。

最接近我已经找到了一个不错的解决方案是将extra_spaces%(num_words

- 1)添加到spaces_to_insert,但是这会导致额外的空格不是

均匀分配。


谢谢。

解决方案

Simon Morgan写道:

下面的代码填充了句子之间有额外空格的句子
,以便每一行在屏幕上占用相同的空间。原样,在分配额外的空间时,代码将有利于行的结束。任务是修改它,以便在有利于行尾的额外空间的分布和行的开头之间交替。

void write_line(void)
{extra_spaces,spaces_to_insert,i,j;

extra_spaces = MAX_LINE_LEN - line_len;
for(i = 0; i< line_len; i ++){
if(line [i]!='''')
putchar(line [i]);
else {
spaces_to_insert = extra_spaces /(num_words - 1);


这是麻烦制造者。假设你有8个额外的空格和4个单词。

那么spaces_to_insert将是8/3 = 2.666并且_cut_ _off_为2.所以

也是少数"首先会插入空格。


如果你想要太多空格首先,将此行更改为


spaces_to_insert = int(0.5 + extra_spaces /(num_words - 1.0);


(谨防制作通过使用1.0代替1或

将浮点数转换为浮点数,否则舍入将不起作用。)

for(j = 1; j< = spaces_to_insert + 1; j ++)
putchar('''');
extra_spaces - = spaces_to_insert;
num_words--;
}
}
putchar (''\ n'');
}




问候

Steffen


2005年7月22日星期五12:54:23 +0200,Steffen Buehler写道:

spaces_to_insert = int(0.5 + extra_spaces /(num_words) - 1.0);

(注意通过使用1.0而不是1或
使extra_spaces浮动来制作除法浮点数,否则舍入将不起作用。)


感谢Steffen的快速回复。


只有一个问题。 int是一个函数还是我应该将结果表达式转换为int?
表达式?如果我把它投出来的话输出就不是我想要的那样,或者我认为这本书是怎么想的(这些空格似乎是随机的)分布在整个生产线上)所以我认为这是一个

函数,但我似乎无法在书中或

手册页中找到它安装在我的系统上。


Simon Morgan写道:

2005年7月22日星期五12:54: 23 + 0200,Steffen Buehler写道:

spaces_to_insert = int(0.5 + extra_spaces /(num_words - 1.0);
是int函数还是我应该施放
表达式的结果是一个int?




这是一个简单的演员,很抱歉这个混乱。该行应该是

spaces_to_insert =(int)(0.5 + extra_spaces /(num_words - 1.0));


(我忘了最后一个结束括号......)

如果我投了它,输出就是我不希望它是怎样的,或者我认为这本书的预期是什么(
空间似乎随机分布在整个系列中)




奇怪。它在这里工作正常。你对丢失的括号做了什么?

你对extra_spaces和num_words有什么价值?


问候

Steffen


Hi,

Does anybody have a solution or a hint for Exercise 5 of Chapter 15 of
K.N. King''s book C Programming: A Modern Approach? I spent all of
yesterday evening staring at it and drooling. The author sure does have a
knack for making me feel like a dumbass, or maybe it''s just that I am.

The following code pads out sentences with extra spaces between words so
that each line takes up the same amount of space on screen. As is, the
code will favour the end of the line when distributing the extra spaces.
The task is to modify it so that it alternates between distribution of the
extra spaces favouring the end of the line and the beginning of the line.

void write_line(void)
{
int extra_spaces, spaces_to_insert, i, j;

extra_spaces = MAX_LINE_LEN - line_len;
for (i = 0; i < line_len; i++) {
if (line[i] != '' '')
putchar(line[i]);
else {
spaces_to_insert = extra_spaces / (num_words - 1);
for (j = 1; j <= spaces_to_insert + 1; j++)
putchar('' '');
extra_spaces -= spaces_to_insert;
num_words--;
}
}
putchar(''\n'');
}

I cannot for the life of me figure out a solution beyond parsing the
sentence first and adding the number of spaces to an array to be used when
printing the sentence to screen, but this feels like an awful kludge. The
closest I''ve come to a decent solution is to add extra_spaces % (num_words
- 1) to spaces_to_insert but this results in the extra spaces not being
distributed evenly.

Thanks.

解决方案

Simon Morgan wrote:

The following code pads out sentences with extra spaces between words
so that each line takes up the same amount of space on screen. As is,
the code will favour the end of the line when distributing the extra
spaces. The task is to modify it so that it alternates between
distribution of the extra spaces favouring the end of the line and
the beginning of the line.

void write_line(void)
{
int extra_spaces, spaces_to_insert, i, j;

extra_spaces = MAX_LINE_LEN - line_len;
for (i = 0; i < line_len; i++) {
if (line[i] != '' '')
putchar(line[i]);
else {
spaces_to_insert = extra_spaces / (num_words - 1);
This is the troublemaker. Let''s say you have 8 extra spaces and 4 words.
Then spaces_to_insert will be 8/3 = 2.666 and be _cut_ _off_ to 2. So
"too few" spaces will be inserted first.

If you want to have "too much" spaces first, change this line to

spaces_to_insert = int(0.5 + extra_spaces / (num_words - 1.0);

(Beware to make the division floating-point by using 1.0 instead of 1 or
casting extra_spaces to float, otherwise the rounding will not work.)
for (j = 1; j <= spaces_to_insert + 1; j++)
putchar('' '');
extra_spaces -= spaces_to_insert;
num_words--;
}
}
putchar(''\n'');
}



Regards
Steffen


On Fri, 22 Jul 2005 12:54:23 +0200, Steffen Buehler wrote:

spaces_to_insert = int(0.5 + extra_spaces / (num_words - 1.0);

(Beware to make the division floating-point by using 1.0 instead of 1 or
casting extra_spaces to float, otherwise the rounding will not work.)



Thanks for the quick reply Steffen.

Just one question. Is int a function or am I supposed to cast the result
of the expression to an int? If I cast it the output isn''t how I would
expect it to be or how I think the book expects it to be (the spaces seem
to be randomly distributed throughout the line) so I assume it''s a
function but I can''t seem to find any mention of it in the book or in the
man pages installed on my system.


Simon Morgan wrote:

On Fri, 22 Jul 2005 12:54:23 +0200, Steffen Buehler wrote:

spaces_to_insert = int(0.5 + extra_spaces / (num_words - 1.0);
Is int a function or am I supposed to cast the
result of the expression to an int?



It''s a simple cast, sorry for the confusion. The line should read

spaces_to_insert = (int)(0.5 + extra_spaces / (num_words - 1.0));

(I forgot the last closing bracket as well...)
If I cast it the output isn''t how
I would expect it to be or how I think the book expects it to be (the
spaces seem to be randomly distributed throughout the line)



Strange. It works fine here. What did you do with the missing bracket?
What are your values for extra_spaces and num_words?

Regards
Steffen


这篇关于C编程:现代方法 - 第15章练习5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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