整体还不错:) 感谢有用的评论...我会记住这些建议 并相应地编辑代码。到目前为止,我真的很喜欢C,我不能等待 ,直到我对它有了很好的了解...这有经验, 课程。当我有经验的时候,我有一些非常甜蜜的项目 足够用C. :) 再次感谢! - Patrick M. / * EOF * / 如果输入包含零值字符,这将退出循环,您是否打算这样做? (如果你要遵循理查德建议的哲学,你接受的是自由主义,严格遵守你所生产的),你应该允许一个零角色,认为它是空白。 你是什么意思,零字符?退出循环的唯一方法就是有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". Ialso 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 ifyou get the chance and feel like it, you can post the solution. I''vetested it and there seem to be no bugs from what I see...--Patrick M./* EOF */ 解决方案Pretty good overall :) 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 notsignificant, but I think it''s sort of cool. ;) I think I prefer the version with descriptive variable names :) 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 ifthere is EOF. 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 themwhen I was experiementing and creating the code, and never got rid of them. 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 emacsnever re-indented it. 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 gothrough the rest of the while loop before exiting. 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 :) 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). Some more magic numbers / magic space counts. 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... Pretty good overall :)Thanks for the useful critique... I''ll keep those suggestions in mindand edit the code accordingly. So far I really like C and I can''t waittill I gain a good knowledge of it... which comes with experience, ofcourse. I''ve got some really sweet projects in mind when I''m experiencedenough with C. :)Thanks again!--Patrick M./* EOF */ 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 whileit''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, Ididn''t look at his code until after I created it my own way) and replacethat with "while (var == 1)". Any other suggestions?--Patrick M./* EOF */ 这篇关于krx113的替代解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!