当前位置: 代码迷 >> C语言 >> 看一下这个程序
  详细解决方案

看一下这个程序

热度:119   发布时间:2007-01-09 23:16:09.0
看一下这个程序

大家帮我看一下这个程序,为什么运行不对啊?
#include<stdio.h>

int main()
{
int zm ,danc, hangshu;
zm=danc=hangshu=0;
char str[100], c;
bool ident = false;

printf("Please input a sentence:\n");
gets(str);
while((c=getchar())!=EOF)
{
++zm;
if(c=='\n')
++hangshu;
if(c==' '||c =='\n'||c=='\t')
ident=false;
else if(ident==false)
{
ident=true;
++danc;
}
}
printf("%s=%d\n%s=%d\n%s=%d\n",
"zm",zm,
"hangshu",hangshu,
"danc",danc);


return 0;
}

搜索更多相关的解决方案: include  false  

----------------解决方案--------------------------------------------------------
怎么看得有点糊涂?
----------------解决方案--------------------------------------------------------

就是统计字母数和行数,单词数的一个程序啊


----------------解决方案--------------------------------------------------------

#include<stdio.h>
#include <string.h>
int main()
{
int zm ,danc, hangshu;
zm=danc=hangshu=0;
char str[100], c;
bool ident = false;
int i=0;

printf("Please input a sentence:\n");
gets(str);
/*while((c=getchar())!=EOF)*/
while(i<strlen(str))
{
c=str[i];
++zm;
if(c=='\n')
++hangshu;
if(c==' '||c =='\n'||c=='\t')
ident=false;
else if(ident==false)
{
ident=true;
++danc;
}
i++;
}
printf("%s=%d\n%s=%d\n%s=%d\n",
"zm",zm,
"hangshu",hangshu,
"danc",danc);
return 0;
}


----------------解决方案--------------------------------------------------------
那为什么我的那不对呢?能指出来哪里错了吗?
----------------解决方案--------------------------------------------------------
楼主的gets(str);是什么意思,不是已经有c=getchar();了吗??
四楼兄弟的意思好象和楼主想的有点不一样!
----------------解决方案--------------------------------------------------------
是啊,LZ的程序我根本看不出是统计哪个字符串?
----------------解决方案--------------------------------------------------------
int getc( FILE *stream );//读入一行
int getchar( void );//一个字符
while((c=getchar())!=EOF)//不能退出来
printf("Please input a sentence:\n");
//gets(str);
while(((c=getchar())!=EOF)&&(c!='\n'))
{
str[i]=c;
i++;

----------------解决方案--------------------------------------------------------
以下是引用yjbqq在2007-1-9 23:16:09的发言:

大家帮我看一下这个程序,为什么运行不对啊?
#include<stdio.h>

int main()
{
int zm ,danc, hangshu;
zm=danc=hangshu=0;
char str[100], c;
bool ident = false;

printf("Please input a sentence:\n");
gets(str);
while((c=getchar())!=EOF)//两者相互独立.都有各自的输入.
{
++zm;
if(c=='\n')
++hangshu;
if(c==' '||c =='\n'||c=='\t')
ident=false;
else if(ident==false)
{
ident=true;
++danc;
}
}
printf("%s=%d\n%s=%d\n%s=%d\n",
"zm",zm,
"hangshu",hangshu,
"danc",danc);


return 0;
}


----------------解决方案--------------------------------------------------------
这个可能掉一个回车.
----------------解决方案--------------------------------------------------------

  相关解决方案