给直方图一个镜头...... [英] Giving the histogram a shot...

查看:66
本文介绍了给直方图一个镜头......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我以为我会尝试一次拿一个程序。 (如果

你记得我的上一篇文章我试图用

的数据制作直方图每个单词的大小)

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. (If
you remember my last post I am trying to make a histogram with data on
the 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 any
other 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 a
blank 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 had
written this program well and I even sketched it out on paper
beforehand , hehe.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.

推荐答案

ext_u< ex *********** @ hotmail.com>在消息中写道

news:32 ************************** @ posting.google.c om ...
ext_u <ex***********@hotmail.com> wrote in message
news:32**************************@posting.google.c om...
好的我以为我会尝试一次拿一个程序。 (如果你还记得我的上一篇文章,我试图用每个单词大小的数据制作直方图)
首先是Anways。我显然需要确定一个单词究竟是什么。
我自己编写这个程序而不用看书或任何其他资源一次。

#include< stdio.h>
main()


int main()

{
int c;


鼓励:


非常好。许多新手犯了错误:

为getchar()定义''char''来存储数据

in。它必须是''int'',因为你有它,所以它

可以存储EOF,这不能保证在一个字母中适合




int nword,nother;

nword = nother = 0;


信息:


而不是在事后定义然后分配,

你可以给你的变量初始化定义时的值:


int nword = 0;

int nother = 0;


我建议定义每行只有一个变量。

当你进步时,原因会变得很明显

(基本上使代码更易于阅读和维护,

并防止可能出现''愚蠢'的错误,特别是当你开始使用指针时,你会发现这些错误。

while((c = getchar()) != EOF)
{
if(c ==''''||''\ t''||''\ n'')
++ nother;
其他
++ nword;
}
printf(" Words =%d \\\
Other =%d",nword,nother);
}
我基本上只是试图告诉计算机任何不是空格,标签或换行符的单词......


更多的鼓励:

嗯,我确定你意识到'这不是最终目标,

但我很高兴看到你简化了事情,所以你可以得到

*东西*工作。

但每次我运行程序时它只会添加其他变量。
我无法弄清楚为什么,无论我输入什么。我以为我已经很好地编写了这个程序,我甚至事先在纸上画出了
,呵呵。


这是一个非常好的主意,虽然在这种情况下,它并没有
帮助。 :-(你的问题是对操作员的误解

语法。

我知道你们可能会发现一个可怕的noobish错误,但是
请记住我开始学习C大概就像48小时前一样。
Ok I thought I would try to take the program one thing at a time. (If
you remember my last post I am trying to make a histogram with data on
the 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 any
other resource once.

#include <stdio.h>
main()
int main()
{
int c;
Encouragement:

Very good. Many novices make the mistake of
defining a ''char'' for getchar() to store data
in. It must be ''int'' as you have it, so it
can store EOF which is not guaranteed to fit
in a char.

int nword, nother;

nword = nother = 0;
Informative:

Rather than defining and then assigning after the fact,
you can give your variable initial values at definition time:

int nword = 0;
int nother = 0;

I recommend defining only one variable per line.
The reasons for this will become evident as you progress
(essentially makes the code easier to read and maintain,
and prevents possibly ''silly'' mistakes, especially when
you start to work with pointers).


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 a
blank space, a tab , or a newline is a word...
More encouragement:

Well, I''m sure you realize that''s not the ultimate goal,
but I''m glad to see you simplify things so you can get
*something* working.
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 had
written this program well and I even sketched it out on paper
beforehand , hehe.
That is a very good idea, although in this case, it doesn''t
help. :-( Your problem is a misunderstanding of operator
syntax.

I know you guys will probably find a horribly noobish mistake , but
please 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;


你做了一个非常好的尝试。干得好。


-Mike




Yes, 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 the
expression, ( '' '' || ''\t'' || ''\n'' ) as the ''right-hand''
operand. The ''logical or'' operator (||) returns true
if 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'', you
need 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





Mike Wahler < MK ****** @ mkwahler.net>在消息中写道

news:ds *************** @ newsread4.news.pas.earthlin k.net ...

看完Kevin的回复后,看到我的

出现了,看来你可能会使用错误的字符

来表达逻辑''要么''。在美国PC键盘上,它是

移位''反斜杠''键。如果你有其他键盘,

我不知道它是什么。最好检查一下。


[snip]

Mike Wahler <mk******@mkwahler.net> wrote in message
news:ds***************@newsread4.news.pas.earthlin k.net...

After looking at Kevin''s reply, and seeing how mine
appears, it seems you might be using the wrong characters
to express logical ''or''. On a U.S. PC keyboard, it''s the
shifted ''backslash'' key. If you have some other keyboard,
I don''t know which it is. Better check that out.

[snip]
是的,你确实犯了一个(非常常见的)新手错误。不要感到难过,
许多人都做到了这一点。问题在于你的陈述:

if(c ==''''||''\ t''||''\ n'')
++ nother ;


我从你的帖子上复制/粘贴上面的内容。

比较运算符(==)只需要两个操作数。
你提供了'' c''作为''左手''操作数,而
表达式,(''''||'''\\''''||'''\\'n'')为''右手''
操作数。 ''逻辑或''运算符(||)返回true
如果其任一操作数产生非零(true)值。
没有'''''''\ t'',或''\ n''的值为零,所以''或者''
它们中的任何一个或全部将总是产生非零(真)。

表达''如果c等于'''',''\\ t''或''\ n''中的任何一个,你需要表达三种截然不同的比较,'或-d''一起。
写:

if(c ==''''|| c ==''\t''|| c ==''\ n'')
++ nother;

++ nword;
Yes, 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;
I copy/pasted the above from your post.

The comparison operator (==) takes exactly two operands.
You supplied ''c'' as the ''left-hand'' operand, and the
expression, ( '' '' || ''\t'' || ''\n'' ) as the ''right-hand''
operand. The ''logical or'' operator (||) returns true
if 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'', you
need to express three distinct comparisons, ''or-d'' together.
Write:

if (c == '' '' || c == ''\t'' || c == ''\n'')
++nother;
else
++nword;




注意''或''运算符在这里看起来有什么不同。


-Mike



Note how the ''or'' operator appears different here.

-Mike


ext_u写道:

好的我我以为我会尝试一次拿一个程序。 (如果你还记得我的上一篇文章,我正在尝试用每个单词大小的数据制作直方图)


不要继续开始新的关于同一件事的线索。通过在原始帖子中回复

回复,并剪断与密切相关的内容,

你不必提醒别人。如果需要,它还可以让他们更容易回头看看。

首先是Anways ..我显然需要确定一个单词究竟是什么。
我自己编写了这个程序而没有看过这本书或任何其他资源。


好​​。

#include< stdio.h>
main()


get在写int main(void)的习惯中或者

" int main(int argc; char * argv)"

{
int c;
int nword,nother;

nword = nother = 0;

while((c = getchar())!= EOF)
{
if(c ==''') '||''\t''||''\ n'')


这应该是逻辑或三个逻辑语句。因为它是b $ b它不会做你想要的。试试:


if((c =='''')||(c ==''\ t'')||(c ==''\ n ''))


想一想,你会发现差异。有人可能会说

括号是多余的,但它使声明完全清楚。


BTW,更好地使用缩进3或4个空格,8个太多

多了。所以不要使用标签(如果你使用它们)。

++ nother;

++ nword;
}
printf (Words =%d \\\
Other =%d,nword,nother);


我基本上只是试图告诉计算机任何不是
空格,标签或换行符是一个单词...


你说每个字母都是一个字,除非它是......

但每次我运行程序时它只会添加其他变量。
无论我输入什么,我都无法弄清楚原因。我以为我已经很好地编写了这个程序,我甚至事先在纸上画出了
,呵呵。

我知道你们可能会发现一个可怕的错误,但<请记住,我开始学习C,就像48小时前一样。

Ok I thought I would try to take the program one thing at a time. (If
you remember my last post I am trying to make a histogram with data on
the size of each word)
Don''t keep starting new threads about the same thing. By posting
a reply in the original thread, and snipping what isn''t germane,
you don''t have to remind people about it. And it also makes it
easier for them to look back, if needed.
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 any
other resource once.
Good.

#include <stdio.h>
main()
get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"
{
int c;
int nword, nother;

nword = nother = 0;

while ((c = getchar()) != EOF)
{
if (c == '' '' || ''\t'' || ''\n'')
This should be the logical or of three logical statements. As it
is it won''t do what you want. Try:

if ((c == '' '') || (c == ''\t'') || (c == ''\n''))

think about it, and you will see the difference. Some may say the
parentheses are redundant, but it makes the statement perfectly
clear.

BTW, better to use an indentation of 3 or 4 spaces, 8 is too
much. So don''t use tabs (if you are using them).
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
You are saying that every char is a word, unless it is ...
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 had
written this program well and I even sketched it out on paper
beforehand , hehe.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.




你做得很好。


- -

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!


这篇关于给直方图一个镜头......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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