getchar的问题 [英] problem with getchar

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

问题描述

程序正确编译(大部分来自k& r),但第二个

函数(此处为character_count)给出了错误的答案。

有人可以解释一下为什么?

#include< stdio.h>

#define IN 1

#define OUT 0


int word_count();

int character_count();


int

main(无效)

{

printf("%d \ nn",word_count());

printf("%d \ n",character_count() );


返回0;

}


int

word_count()

{


int c,nw,state;

state = OUT;

nw = 0;


而((c = getchar())!= EOF){

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

state = OUT;

else if(state == OUT){

州= IN;

++ nw;

}

}


返回nw;

}

int

character_count()

{

int c;

int nc = 0;

而((c = getchar())!= EOF)

++ nc;

返回nc;

}

解决方案


sh ************ @ gmail.com schreef:

该程序编译正确(大部分来自k& r),但第二个
函数(此处为character_count)给出了错误的答案。


需要什么答案?

有人可以解释一下原因吗?


getchar从stdin读取一个字符。


< snip>

int wc = 0; / *字数* /

int cc = 0; / *字符数* /

int
main(无效)
{
count(); printf("%d \ nn",wc);
printf("%d \ n",cc);


word_count()和character_count()独立调用get_char。

因此它们在两个完全不同的序列上运行。如果这需要
,那就好了。如果两者都应该在_same_序列上运行,

这是一个严重的问题。

返回0;
}

void

count(void){

int c,nw,state;
state = OUT;
nw = 0;


很高兴看到你练习FSM'。可能有更简单的方法,

虽然。

而((c = getchar())!= EOF){
if(c == ''''|| c ==''\ n''|| c ==''\ t'')
{state = OUT;
}否则if(state == OUT)
{state = IN;
++ wc;

++ cc; }
}
}




sh ************ @ gmail.com schrieb:

该程序编译正确(大部分是来自k& r)但是第二个
函数(这里是character_count)给出了错误的答案。


您输入了什么?你有什么期望?你得到了什么?


有人可以解释一下原因吗?


从理论上讲,完全有可能EOF对stdin很粘,

所以你必须在使用character_count()之前清除它。

您是否在没有前面的word_count()的情况下测试了character_count()?

#include< stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf(" %d \ n",word_count());
printf("%d \ n",character_count());

返回0;
}


使用

int

main(无效)

{

printf ("%d \ n",character_count());


返回0;

}

输入a bb ccc\\\
dddd eeeee ffffff \ n给出了

预期答案。

也许你有一些概念上的问题:如果你想要计算_one_的字符和单词一组输入,

你要么必须首先读取所有输入并将其作为计算函数的

参数提供,否则你必须计算所有内容

在一个函数中读取它(即读取一个带有

getchar()的字符并根据需要调整_all_计数器。)

int
word_count()
{

int c,nw,state;
state = OUT;


注意:当你在这里使用状态IN和OUT时,可能更好地限制他们的可见性:

enum {OUT,IN};

state = OUT;

nw = 0;

while((c = getchar() )!= EOF){
if(c ==''''|| c ==''\ n''|| c ==''\ t'')
state = OUT;
if if(state == OUT){
state = IN;
++ nw;
}
}
返回nw;
}
int
character_count()
{
int c;
int nc = 0;
while((c = getchar) ())!= EOF)
++ nc;
返回nc;
}




这对我来说很好。


干杯

Michael

-

电子邮箱:我的是/ at / gmx / dot / de address。


kl*****@xs4all.nl schrieb:

sh ************ @ gmail.com schreef:
int wc = 0; / *字数* /
int cc = 0; / *字符数* /


如何诱导OP使用文件范围标识符

extern linkage(vulgo:" globals")有帮助吗?

int
main(void)
{count();

printf("%d \ n" ;,wc);
printf("%d \ n",cc);





和word_count()都独立调用get_char。
因此,它们在两个完全不同的序列上运作。如果这是必需的,那就没关系了。如果两者都应该在_same_序列上运行,那么这是一个严重的问题。




True。

return 0;
}



void
count(void)

{
int c,nw,state;
state = OUT;
nw = 0;



很高兴看到你练习FSM'。可能有更简单的方法,但是,




FSM不是公认的TLA。在这附近,所以有些人可能会对宗教活动感到好奇...... ;-)

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

{

state = OUT;


}

else if(state == OUT)


{

state = IN;


++ wc;
++ cc;

}


这应该怎么样?你的修正使得wc和cc

实际上是一样的。你可能意味着:

++ cc;

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

state = OUT;

}

else if(state == OUT){

state = IN;

++ wc;

}

然而,文件范围的使用仍然非常难看

wc和cc。

}
}



干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
Can someone please explain why ?
#include<stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());

return 0;
}

int
word_count ()
{

int c, nw, state;
state = OUT;
nw = 0;

while ((c = getchar ()) != EOF) {
if (c == '' '' || c == ''\n'' || c == ''\t'')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}

return nw ;
}
int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}

解决方案


sh************@gmail.com schreef:

The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
What answer is required?
Can someone please explain why ?
getchar reads one character from stdin.

<snip>
int wc = 0; /* word count */
int cc = 0; /* character count */
int
main(void)
{ count(); printf("%d\n",wc);
printf("%d\n",cc);
both word_count() and character_count() call get_char independently.
Hence they operate on two totally different sequences. If this is
required, than it''s ok. If both should operate on the _same_ sequence,
this is a serious problem.
return 0;
}
void
count (void) {

int c, nw, state;
state = OUT;
nw = 0;

It''s good to see you practice FSM''s. There may be easier ways to do it,
though.
while ((c = getchar ()) != EOF) {
if (c == '' '' || c == ''\n'' || c == ''\t'') { state = OUT; } else if (state == OUT) { state = IN; ++wc;
++cc; }
}
}




sh************@gmail.com schrieb:

The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
What did you give as input? What did you expect? What did you get?

Can someone please explain why ?
In theory, it is perfectly possible that EOF is sticky for stdin,
so you would have to clear it before using character_count().
Did you test character_count() without preceding word_count()?
#include<stdio.h>
#define IN 1
#define OUT 0

int word_count();
int character_count();

int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());

return 0;
}
Using
int
main (void)
{
printf("%d\n",character_count());

return 0;
}
on an input of "a bb ccc\ndddd eeeee ffffff\n" gives the
expected answer.
Maybe there is some conceptual problem on your part: If you
want to count the characters and words of _one_ set of inputs,
you either have to first read all the input and provide it as
argument to counting functions or you have to count everything
in one function as you read it (i.e. read a character with
getchar() and adjust _all_ counters as necessary).

int
word_count ()
{

int c, nw, state;
state = OUT;
Note: As you use the states IN and OUT only here, it may be
better to restrict their "visibility":
enum {OUT, IN};
state = OUT;
nw = 0;

while ((c = getchar ()) != EOF) {
if (c == '' '' || c == ''\n'' || c == ''\t'')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}

return nw ;
} int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}



This looks fine to me.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


kl*****@xs4all.nl schrieb:

sh************@gmail.com schreef:
int wc = 0; /* word count */
int cc = 0; /* character count */
How is inducing the OP to use file scope identifiers with
extern linkage (vulgo: "globals") helpful?

int
main(void)
{ count();

printf("%d\n",wc);
printf("%d\n",cc);



both word_count() and character_count() call get_char independently.
Hence they operate on two totally different sequences. If this is
required, than it''s ok. If both should operate on the _same_ sequence,
this is a serious problem.



True.

return 0;
}



void
count (void)

{
int c, nw, state;
state = OUT;
nw = 0;



It''s good to see you practice FSM''s. There may be easier ways to do it,
though.



FSM is no "recognized TLA" around here, so some people may
wonder about religious exercises... ;-)

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


{

state = OUT;


}

else if (state == OUT)


{

state = IN;


++wc;
++cc;

}
How is that supposed to work? Your correction makes wc and cc
effectively the same. You probably meant:
++cc;
if (c == '' '' || c == ''\n'' || c == ''\t'') {
state = OUT;
}
else if (state == OUT) {
state = IN;
++wc;
}
However, there is still the exceedingly ugly use of file scope
wc and cc.
}
}


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于getchar的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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