krx113的替代解决方案 [英] Alternate solution to krx113

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

问题描述

您好。我刚刚获得了K& R第二版的The C Programming Language。我刚刚完成了1-13的练习,我编写了一个更短,

评论的解决方案。我不确定你是否还在更新网站

http://users.powernet.co.uk/eton/kandr2/index.html) ,理查德,但如果

你得到机会和感觉,你可以发布解决方案。我已经对它进行了b $ b测试,看起来没有任何错误...


-

Patrick M.

/ * EOF * /

解决方案

Patrick M.写道:

你好。我刚刚获得了K& R第二版的The C Programming
Language。我刚刚完成了1-13的练习,我编写了一个更短的评论解决方案。


我不认为你的明显更短 - 似乎

与程序逻辑的数量大致相同,但Richard使用了

比你更多的空格和更长的变量名。


NB。我在下面的代码中删除了一些注释,因为

的换行问题。

/ *打印输入中单词长度的直方图。 * /
#include< stdio.h>

int main(){

long chars [12] = {0},c = 0,wl = 0,
x = 0,n = 0,p = 0,
mwl = 0,q = 0;


我想我更喜欢带有描述性变量名称的版本:)

while((c = getchar())){


如果输入包含零值

字符,这将退出循环,您是否打算这样做? (如果你打算遵循哲学理查德建议的哲学,你接受的是自由主义,你所生产的东西是严格的b $ b),你应​​该允许

零字符,并认为它是空格。

++ wl;
++ p;
if(c ==''\\ \\ n''|| c ==''\ t''|| c ==''\ b''|| c ==''''|| c == EOF){
/ *重置这些变量,因为如果你不这样做,那么应用程序就会认为单词更长* /
- wl;
--p;


AFAICS,你不要在程序的任何地方使用''p'',除了

此时递增和递减它。它的用途是什么?

if(wl> = 11){
++ chars [11]; }
else {
++ chars [wl]; }
wl = 0;


这里有缩进问题 - 你已经使用空格来支付

"但是标有++ chars的标签。线。在我的文本编辑器中(4个空格

制表符),看起来像++字符超出了1个字符(非常可靠)。当发布到Usenet时,最好避免使用标签。

}

if(c == EOF){/ * EOF收到... break * / <休息}


好​​(恕我直言)。在理查德的版本中,他引入了一个新的变量

完成,其唯一的目的是一个冗长的休息版本。

我'我想听听他的理由...

}
printf(" \ n");
/ *我们想要找到出现单词长度的最大倍数,并将mwl设置为该值* /
为(x = 1; x <= 11; ++ x){


为了保持可操作性,最好避免使用魔法

数字11。这里 - 因为它取决于chars []数组的大小,

它的推导应该反映出来。您的代码中的其他

幻数也一样。

if(chars [x]> mwl){
mwl = chars [x]; }

if(mwl> = 1)
for(x = mwl; x> = 1; --x){
/ * note自我:格式化printf(%d),在它之前带有
数字,它将通过
该数字减去数字%d
包含的数字来证明它是正确的。记住这个!!! * /
printf("%20d |",x);


这意味着x将在宽度为20的字段中右对齐。

(注意,如果x有超过20位数,那么所有x'的数字

将被打印 - 它不会在20时被切断。

while(++ n< = 11){
if(chars [n]> = x){

/ *必须用于格式化目的* /
如果(++ q == 1){
printf(" *"); }
else {
printf(" *"); }
}
其他{
printf("");
}

}
printf(" \ n");
n = 0;
q = 0;
}
/ / / * 1st printf中的空格必须比右对齐<2 br />上面的值,第二个空格必须比第一个空格数多一个* /
printf(" + - ");
for(x = 0; x< ; = 31; ++ x){
printf(" - ");
}
printf(" \ n");


更多魔法数字/魔法空间计数。

for(x = 1; x <= 11; ++ x){
if(x == 11){
printf(">&t; 10 \ n"); }
else {
printf("%2d",x); }
}


那个循环会写得更好(当然是大括号):


for(x = 1 ; x< 11; ++ x)

printf("%2d",x);

printf("> 10 \ n") ;

返回0;
}




整体还不错:)


Old Wolf写道:

Patrick M.写道:

你好。我刚刚获得了K& R第二版的The C Programming
Language。我刚刚完成了1-13的练习,我编写了一个更短的评论解决方案。



我认为你的时间要短得多 - 似乎有相同数量的程序逻辑,但理查德使用了比你更多的空格和更长的变量名。

NB。我在下面的代码中删除了一些注释,因为换行问题。




好​​吧,我的方式是,它是20行不到理查德的。没错,它不是很重要,但我觉得它很酷。 ;)

/ *打印输入中单词长度的直方图。 * /
#include< stdio.h>

int main(){

long chars [12] = {0},c = 0,wl = 0,
x = 0,n = 0,p = 0,
mwl = 0,q = 0;



我想我更喜欢这个版本描述性变量名称:)

while((c = getchar())){



如果输入包含一个,这将退出循环零价值的角色,你打算这个吗? (如果你要遵循理查德建议的哲学,你接受的是自由主义,严格遵守你所生产的),你应该允许一个零角色,认为它是空白。




你是什么意思,零字符?退出循环的唯一方法是

有EOF。

++ wl;
+ + p;
if(c ==''\ n''|| c ==''\ t''|| c ==''\ b''|| c =='' ''|| c == EOF){
/ *重置这些变量,因为如果你不这样做,那么应用程序就会认为单词更长* /
- wl;
--p;



AFAICS,你不要在程序的任何地方使用''p',除了此时递增和递减它。是什么?




好​​吧,我的坏,我忘了摆脱那些变量。当我体验和创建代码时,我必须使用它们

,并且永远不会摆脱它们。

if(wl) > = 11){
++ chars [11]; }
else {
++ chars [wl]; }
wl = 0;



这里有缩进问题 - 你已经使用了空格用于
if但是标有++ chars的标签。线。在我的文本编辑器(4个空格
选项卡)中,看起来像++字符的字符超出了1个字符(非常难以理解)。发布到Usenet时,最好避免使用标签。




另一个我的坏。我相信这是因为我正在编辑代码,并且emacs

永远不会重新缩进它。

}

if(c == EOF){/ * EOF收到...休息* /
休息; }



好(恕我直言)。在理查德的版本中,他引入了一个新的变量完成,其唯一的目的是一个冗长的休息版本。
我想听听他的理由是什么那...




谢谢。是的,另一种方法是使用我之前做过的变量,

但在这种情况下我决定使用break所以在退出之前,它不需要通过剩余的while循环来获得

}
printf(" \ n");
/ *我们要查找出现单词长度的最大次数
,并将mwl设置为该值* /
for(x = 1; x <= 11; ++ x){



为了可维护性,最好避免使用魔法数字 11 QUOT;这里 - 因为它取决于chars []数组的大小,
它的推导应该反映出来。同样适用于代码中的其他魔术数字。




这是真的......我会添加一个常数并修复它:)

if(chars [x]> mwl){
mwl = chars [x]; }

if(mwl> = 1)
for(x = mwl; x> = 1; --x){
/ * note自我:格式化printf(%d),在它之前带有
数字,它将通过
该数字减去数字%d
包含的数字来证明它是正确的。记住这个!!! * /
printf("%20d |",x);



这意味着x将在宽度字段中右对齐20.
(请注意,如果x的数字超过20位,那么x'的所有数字将被打印 - 它不会在20处被切断。)
< blockquote class =post_quotes> while(++ n< = 11){
if(chars [n]> = x){

/ *必须用于格式化目的* /
if(++ q == 1){
printf(" *"); }
else {
printf(" *"); }
}
其他{
printf("");
}

}
printf(" \ n");
n = 0;
q = 0;
}
/ / / * 1st printf中的空格必须比右对齐<2 br />上面的值,第二个空格必须比第一个空格数多一个* /
printf(" + - ");
for(x = 0; x< ; = 31; ++ x){
printf(" - ");
}
printf(" \ n");



更多魔法数字/魔法空间计数。

for(x = 1; x< = 11; ++ x){
if(x = = 11){
printf("> 10 \ n"); }
else {
printf("%2d",x); }
}



那个循环最好写成(括号可选,当然):

for(x = 1; x< 11; ++ x)
printf("%2d",x);
printf("> 10 \ n");



是的,这是一个更好的版本...

return 0;
}


整体还不错:)




感谢有用的评论...我会记住这些建议

并相应地编辑代码。到目前为止,我真的很喜欢C,我不能等待

,直到我对它有了很好的了解...这有经验,

课程。当我有经验的时候,我有一些非常甜蜜的项目

足够用C. :)


再次感谢!


-

Patrick M.

/ * EOF * /


Patrick M.写道:

Old Wolf写道:

while((c = getchar())){



如果输入包含零值
字符,这将退出循环,您是否打算这样做? (如果你要遵循理查德建议的哲学,你接受的是自由主义,严格遵守你所生产的),你应该允许一个零角色,认为它是空白。



你是什么意思,零字符?退出循环的唯一方法就是
有EOF。




没关系,我明白你的意思了。 C将其读作while(c = getchar()!=

0)因为C中大多数事物的真值都是1,因此当它是1时它会循环,当它读取零值时它会停止循环。

我看到解决这个问题的唯一方法就是做理查德所做的事情(顺便说一句,我用b $ b看看他的代码,直到我按照自己的方式创建代码)并替换

表示while(var == 1)。还有其他建议吗?

-

Patrick M.

/ * EOF * /


Hello. I just got the K&R 2nd Edition of "The C Programming Language". I
also just finished exercise 1-13, and I''ve coded a much shorter,
commented solution. I''m not sure if you''re still updating the site
(http://users.powernet.co.uk/eton/kandr2/index.html), Richard, but if
you get the chance and feel like it, you can post the solution. I''ve
tested it and there seem to be no bugs from what I see...

--
Patrick M.
/* EOF */

解决方案

Patrick M. wrote:

Hello. I just got the K&R 2nd Edition of "The C Programming
Language". I also just finished exercise 1-13, and I''ve coded
a much shorter, commented solution.
I don''t think yours is significantly shorter -- there seems to
be about the same amount of program logic, but Richard used
more whitespace and longer variable names than you did.

NB. I have removed some comments in the code below, because
of line-wrapping issues.
/* Print a histogram of the lengths of words in input. */
#include <stdio.h>

int main() {

long chars[12] = {0}, c = 0, wl = 0,
x = 0, n = 0, p = 0,
mwl = 0, q = 0;
I think I prefer the version with descriptive variable names :)

while ((c = getchar())) {
This will exit the loop if the input contains a zero-valued
character, did you intend this? (If you are going to follow
the philosophy Richard suggests, "be liberal in what you accept,
strict in what you produce"), you should probably allow a
zero character, and consider it to be whitespace.
++wl;
++p;
if (c == ''\n'' || c == ''\t'' || c == ''\b'' || c == '' '' || c == EOF) {
/* reset these variables because if you don''t,
the app will think the words are much longer */
--wl;
--p;
AFAICS, you do not use ''p'' anywhere in the program, except for
incrementing and decrementing it at this point. What is it for?

if (wl >= 11) {
++chars[11]; }
else {
++chars[wl]; }
wl = 0;
There are indenting problems here -- you''ve used spaces for the
"if" but tabs for the "++chars" line. In my text editor (4 space
tabs) it looks like the ++chars is out-dented 1 character (very
unreadable). When posting to Usenet it''s a good idea to avoid tabs.
}

if (c == EOF) { /* EOF received... break */
break; }
Good (IMHO). In Richard''s version, he introduces a new variable
"done", whose sole purpose is a long-winded version of "break".
I''d like to hear his rationale on that...
}
printf("\n");
/* We''re going to want to find the max times a word length
appeared, and set mwl to that value */
for (x = 1; x <= 11; ++x) {
For maintainability, it would be a good idea to avoid the magic
number "11" here -- since it depends on the size of the chars[] array,
its derivation should reflect that. Same goes for the other
magic numbers in your code.
if (chars[x] > mwl) {
mwl = chars[x]; }
}

if (mwl >= 1)
for (x = mwl; x >= 1; --x) {
/* note to self: formatting printf("%d") with a
number before it, it will right justify it by
that number MINUS the amount of digits %d
contains. Remember this!!!*/
printf("%20d |", x);
This means that x will be right-justified in a field of width 20.
(Note that if x has more than 20 digits, then all of x''s digits
will be printed -- it won''t cut off at 20).
while (++n <= 11) {
if (chars[n] >= x) {

/* must be done for formatting purposes */
if (++q == 1) {
printf(" *"); }
else {
printf(" *"); }

}
else {
printf(" ");
}

}
printf("\n");
n = 0;
q = 0;
}

/* spaces in 1st printf must be 2 more than right-justified
value above, spaces in second must be one more than 1st
printf space count */
printf(" +-");
for (x = 0; x <= 31; ++x) {
printf("-");
}
printf("\n ");
Some more magic numbers / magic space counts.
for (x = 1; x <= 11; ++x) {
if (x == 11) {
printf(">10\n"); }
else {
printf("%2d ", x); }
}
That loop would be better written as (braces optional, of course):

for (x = 1; x < 11; ++x)
printf("%2d ", x);
printf(">10\n");
return 0;
}



Pretty good overall :)


Old Wolf wrote:

Patrick M. wrote:

Hello. I just got the K&R 2nd Edition of "The C Programming
Language". I also just finished exercise 1-13, and I''ve coded
a much shorter, commented solution.



I don''t think yours is significantly shorter -- there seems to
be about the same amount of program logic, but Richard used
more whitespace and longer variable names than you did.

NB. I have removed some comments in the code below, because
of line-wrapping issues.



Well, the way mine is, it''s 20 lines less than Richard''s. True, it''s not
significant, but I think it''s sort of cool. ;)

/* Print a histogram of the lengths of words in input. */
#include <stdio.h>

int main() {

long chars[12] = {0}, c = 0, wl = 0,
x = 0, n = 0, p = 0,
mwl = 0, q = 0;



I think I prefer the version with descriptive variable names :)

while ((c = getchar())) {



This will exit the loop if the input contains a zero-valued
character, did you intend this? (If you are going to follow
the philosophy Richard suggests, "be liberal in what you accept,
strict in what you produce"), you should probably allow a
zero character, and consider it to be whitespace.



What do you mean, zero character? The only way it exits the loop is if
there is EOF.

++wl;
++p;
if (c == ''\n'' || c == ''\t'' || c == ''\b'' || c == '' '' || c == EOF) {
/* reset these variables because if you don''t,
the app will think the words are much longer */
--wl;
--p;



AFAICS, you do not use ''p'' anywhere in the program, except for
incrementing and decrementing it at this point. What is it for?



Ok, my bad, I forgot to get rid of those variables. I must''ve used them
when I was experiementing and creating the code, and never got rid of them.

if (wl >= 11) {
++chars[11]; }
else {
++chars[wl]; }
wl = 0;



There are indenting problems here -- you''ve used spaces for the
"if" but tabs for the "++chars" line. In my text editor (4 space
tabs) it looks like the ++chars is out-dented 1 character (very
unreadable). When posting to Usenet it''s a good idea to avoid tabs.



Another "my bad". I believe it''s because I was editing code, and emacs
never re-indented it.

}

if (c == EOF) { /* EOF received... break */
break; }



Good (IMHO). In Richard''s version, he introduces a new variable
"done", whose sole purpose is a long-winded version of "break".
I''d like to hear his rationale on that...



Thanks. Yes, another way is to use a variable, which I''ve done before,
but in this case I decided to use "break" so it didn''t have to go
through the rest of the while loop before exiting.

}
printf("\n");
/* We''re going to want to find the max times a word length
appeared, and set mwl to that value */
for (x = 1; x <= 11; ++x) {



For maintainability, it would be a good idea to avoid the magic
number "11" here -- since it depends on the size of the chars[] array,
its derivation should reflect that. Same goes for the other
magic numbers in your code.



That''s true... I''ll add a constant and fix that up :)

if (chars[x] > mwl) {
mwl = chars[x]; }
}

if (mwl >= 1)
for (x = mwl; x >= 1; --x) {
/* note to self: formatting printf("%d") with a
number before it, it will right justify it by
that number MINUS the amount of digits %d
contains. Remember this!!!*/
printf("%20d |", x);



This means that x will be right-justified in a field of width 20.
(Note that if x has more than 20 digits, then all of x''s digits
will be printed -- it won''t cut off at 20).

while (++n <= 11) {
if (chars[n] >= x) {

/* must be done for formatting purposes */
if (++q == 1) {
printf(" *"); }
else {
printf(" *"); }

}
else {
printf(" ");
}

}
printf("\n");
n = 0;
q = 0;
}

/* spaces in 1st printf must be 2 more than right-justified
value above, spaces in second must be one more than 1st
printf space count */
printf(" +-");
for (x = 0; x <= 31; ++x) {
printf("-");
}
printf("\n ");



Some more magic numbers / magic space counts.

for (x = 1; x <= 11; ++x) {
if (x == 11) {
printf(">10\n"); }
else {
printf("%2d ", x); }
}



That loop would be better written as (braces optional, of course):

for (x = 1; x < 11; ++x)
printf("%2d ", x);
printf(">10\n");



Yes, that is a better version...

return 0;
}



Pretty good overall :)



Thanks for the useful critique... I''ll keep those suggestions in mind
and edit the code accordingly. So far I really like C and I can''t wait
till I gain a good knowledge of it... which comes with experience, of
course. I''ve got some really sweet projects in mind when I''m experienced
enough with C. :)

Thanks again!

--
Patrick M.
/* EOF */


Patrick M. wrote:

Old Wolf wrote:

while ((c = getchar())) {



This will exit the loop if the input contains a zero-valued
character, did you intend this? (If you are going to follow
the philosophy Richard suggests, "be liberal in what you accept,
strict in what you produce"), you should probably allow a
zero character, and consider it to be whitespace.



What do you mean, zero character? The only way it exits the loop is if
there is EOF.



Nevermind, I see what you mean. C reads this as "while (c = getchar() !=
0)" since the truth value in most things in C is 1, and therefore while
it''s 1 it will loop, and when it reads a zero value it stops looping.
The only way I see to fix this is to do what Richard did (By the way, I
didn''t look at his code until after I created it my own way) and replace
that with "while (var == 1)". Any other suggestions?
--
Patrick M.
/* EOF */


这篇关于krx113的替代解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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