本文介绍了给直方图一个镜头......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 好的,我以为我会尝试一次拿一个程序。 (如果 你记得我的上一篇文章我试图用 的数据制作直方图每个单词的大小) Anways first。我显然需要确定一个单词究竟是什么。 我自己写这个程序而不用看书或任何 其他资源一次。 #include< stdio.h> main() { int c; int nword,nother; nword = nother = 0; while((c = getchar())! = EOF) { if(c ==''''||''\ t''||''\ n'') ++ nother; else ++ nword; } printf( Words =%d \\\Other =%d,nword,nother); } 我基本上只想告诉计算机什么那不是一个空格,一个标签或一个换行符就是一个单词... 但是每次我运行程序时它只会添加到另一个变量。 无论我输入什么,我都无法弄明白为什么。我以为我已经把这个程序写得好了,我甚至在纸上画出来了 事先,呵呵。 我知道你们可能会发现一个非常可怕的错误,但是 请记住我在48小时前开始学习C语言。Ok I thought I would try to take the program one thing at a time. (Ifyou remember my last post I am trying to make a histogram with data onthe size of each word)Anways first .. I obviously need to determine what a word actually is.I wrote this program on my own without looking at the book or anyother resource once.#include <stdio.h>main(){int c;int nword, nother;nword = nother = 0;while ((c = getchar()) != EOF){if (c == '' '' || ''\t'' || ''\n'')++nother;else++nword;}printf("Words = %d\nOther = %d", nword, nother);}I am basically just trying to tell the computer anything that is not ablank space, a tab , or a newline is a word...BUT everytime I run the program it adds to only the nother variable.I can''t figure out why, no matter what I type. I thought I hadwritten this program well and I even sketched it out on paperbeforehand , hehe.I know you guys will probably find a horribly noobish mistake , butplease remember I started learning C all of like 48 hours ago.推荐答案 是的,你确实犯了一个(很常见的)新手错误。不要心疼, 很多其他人都这样做了。问题出在你的陈述上: if(c ==''''||''\t''||''\\ \\ n'') ++ nother; 比较运算符(==)只需要两个操作数。 你提供''c''作为''左手''操作数,以及 表达式,(''''||''\t''||'''\ n '')作为''右手'' 操作数。''逻辑或''运算符(||)返回true 如果其任一操作数产生非零(真实)值。 没有'''''''\ t''或''\ n'的值为零,所以''或'' 它们中的任何一个或全部总是会产生非零(真)。 要表达''如果c等于'''',''\\ t''或''\ n''中的任何一个,你需要表达三个不同的比较,b $ b需要表达三个不同的比较, ''或-d''在一起。 写道: if(c ==''''|| c ==''\t''|| c ==''\ n'') ++ nother; else ++ nword; 你做了一个非常好的尝试。干得好。 -MikeYes, you did make a (very common) novice mistake. Don''t feel bad,many others have done this. The problem is with your statement:if (c == '' '' || ''\t'' || ''\n'')++nother;The comparison operator (==) takes exactly two operands.You supplied ''c'' as the ''left-hand'' operand, and theexpression, ( '' '' || ''\t'' || ''\n'' ) as the ''right-hand''operand. The ''logical or'' operator (||) returns trueif either of its operands yields a nonzero (true) value.None of '' '', ''\t'', or ''\n'' have a value of zero, so ''or-ing''any or all of them together will always yield nonzero (true).To express ''if c is equal to any of '' '', ''\t'', or ''\n'', youneed to express three distinct comparisons, ''or-d'' together.Write:if (c == '' '' || c == ''\t'' || c == ''\n'')++nother;else++nword;You made a very good try. Great work.-Mike 注意''或''运算符在这里看起来有什么不同。 -MikeNote how the ''or'' operator appears different here.-Mike 你做得很好。 - - Chuck F(cb********@yahoo.com)(cb********@worldnet.att.net) 可用于咨询/临时嵌入式和系统。 < http://cbfalconer.home.att.net>使用worldnet地址!You are doing fine.--Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)Available for consulting/temporary embedded and systems.<http://cbfalconer.home.att.net> USE worldnet address! 这篇关于给直方图一个镜头......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 12:41