看看这段程序问题出在哪里?

cnbj8607 2010-04-25 08:06:32
当输入Hello, world! This is a C program!时,前2个单词的长度没有显示出来?问题在哪呢?
代码如下:

#include "stdio.h"
#include "stdlib.h"
#define LENGTH (1000)
#define OUT (0)
#define IN (1)
/*编写一个程序,打印输出中单词长度的水平直方图。*/
main()
{
int ch;
int i,j;
int nw=0; /*单词计数*/
int nc=0; /*单词长度计数*/
int state=OUT; /*字符是否在单词内*/
int wl[LENGTH]; /*声明一个数组,用于存放每个单词的长度*/
printf("Please enter some sentences:\n");
for(i=0;i<LENGTH;i++)
wl[i]=0; /*初始化数组各成员的值为0*/
for(i=0;i<LENGTH&&((ch=getchar())!=EOF)&&(ch!='\n');i++)
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(state==OUT) /*字符为单词的首个字母*/
{
nw++;
nc++;
state=IN;
}
else nc++;
}
else
{
wl[nw]=nc;
nc=0;
state =OUT;
}
}
printf("\nOutput the histogram:\n");
for(i=1;i<=nw;i++)
{
printf("Word ID: %d:\t",i);
for(j=0;j<wl[i];j++) /*单词长度以星号输出*/
putchar('*');
putchar('\n');
}
putchar('\n');
system("pause");
return 0;
}
...全文
161 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonjishenxy 2010-04-26
  • 打赏
  • 举报
回复
..........
znyu2005 2010-04-26
  • 打赏
  • 举报
回复
这是我修改后的完整程序,主要有两个地方,
一个是加了flag做为一个中间空格的标志,被清0
二个是for之后又加了一句wl[nw]=nc;否则最后一个字母出不来。
应该好了,你可以试试。


#include "stdio.h"
#include "stdlib.h"
#define LENGTH (1000)
#define OUT (0)
#define IN (1)
/*编写一个程序,打印输出中单词长度的水平直方图。*/
int main(void)
{
int ch;
int i,j;
int flag=1;
int nw=0; /*单词计数*/
int nc=0; /*单词长度计数*/
int state=OUT; /*字符是否在单词内*/
int wl[LENGTH]; /*声明一个数组,用于存放每个单词的长度*/
printf("Please enter some sentences:\n");
for(i=0;i<LENGTH;i++)
wl[i]=0; /*初始化数组各成员的值为0*/
for(i=0;i<LENGTH&&((ch=getchar())!=EOF)&&(ch!='\n');i++)
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(state==OUT) /*字符为单词的首个字母*/
{
nw++;
nc++;
state=IN;
}
else nc++;
flag=1;
}
else
{
if(flag)
wl[nw]=nc;
nc=0;
state =OUT;
flag=0;
}
}

wl[nw]=nc;

printf("\nOutput the histogram:\n");
for(i=1;i<=nw;i++)
{
printf("Word ID: %d:\t",i);
for(j=0;j<wl[i];j++) /*单词长度以星号输出*/
putchar('*');
putchar('\n');
}
putchar('\n');
system("pause");
return 0;
}

znyu2005 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kingteng 的回复:]
C/C++ code

for(i=0;i<LENGTH&&((ch=getchar())!=EOF);i++)
20 {
21 if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
22 {
23 if(state==OUT) ……
[/Quote]

要是单词中间有超过一个空格,或者没有空格只有标点号,也是“炸蛋”
znyu2005 2010-04-26
  • 打赏
  • 举报
回复
问题出在这个地方,因为你的“,”和“!”不是字母,而他们后面又都有一个空格,
所以当程序执行到“,”时就已经将行信息正确赋给wl[nw],此时nc=0;但是紧接着是一个空格,
又不是字母,些时nw没有变,所以刚被赋给的值又被清空了。

if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(state==OUT) /*字符为单词的首个字母*/
{
nw++;
nc++;
state=IN;
}
else nc++;
}
else
{
wl[nw]=nc;
nc=0;
state =OUT;
}
wade_2003 2010-04-26
  • 打赏
  • 举报
回复
其实可以自己调试下,看看那里出问题了,运行于想象的结果不一致
armxing 2010-04-26
  • 打赏
  • 举报
回复
其他的基本不用改,只需要把判断换行字符的语句放到下面就行了。如下所改,可以运行一下试试。
#include "stdio.h"
#include "stdlib.h"
#define LENGTH (1000)
#define OUT (0)
#define IN (1)
/*编写一个程序,打印输出中单词长度的水平直方图。*/
main()
{
int ch;
int i,j;
int nw=0; /*单词计数*/
int nc=0; /*单词长度计数*/
int state=OUT; /*字符是否在单词内*/
int wl[LENGTH]; /*声明一个数组,用于存放每个单词的长度*/
printf("Please enter some sentences:\n");
for(i=0;i<LENGTH;i++)
wl[i]=0; /*初始化数组各成员的值为0*/
for(i=0;i<LENGTH&&((ch=getchar())!=EOF);i++)把(ch !='\n')去掉,看下面所改
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(state==OUT) /*字符为单词的首个字母*/
{
nw++;
nc++;
state=IN;
}
else nc++;
}
else if((ch =='\n'))
{
wl[nw]=nc;
nc=0;
state =OUT;//这两行可以不要
break;
}

else
{
wl[nw]=nc;
nc=0;
state =OUT;
}
}
printf("\nOutput the histogram:\n");
for(i=1;i<=nw;i++)
{
printf("Word ID: %d:\t",i);
for(j=0;j<wl[i];j++) /*单词长度以星号输出*/
putchar('*');
putchar('\n');
}
putchar('\n');

return 0;
}
白云飘飘飘 2010-04-25
  • 打赏
  • 举报
回复
#define OUT (0)
#define IN (1)
/*编写一个程序,打印输出中单词长度的水平直方图。*/
int main()
{
int ch;
int i,j;
int nw=-1; /*单词计数*/
//int nc=0; /*单词长度计数*/
int state=OUT; /*字符是否在单词内*/
int wl[LENGTH]; /*声明一个数组,用于存放每个单词的长度*/
printf("Please enter some sentences:\n");
for(i=0;i<LENGTH;i++)
wl[i]=0; /*初始化数组各成员的值为0*/
for(i=0;i<LENGTH&&((ch=getchar())!=EOF)&&(ch!='\n');i++)
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(state==OUT) /*字符为单词的首个字母*/
{
nw++;
wl[nw]++;

}
else
wl[nw]++;

state=IN;
}
else
{
state =OUT;
}
}
printf("\nOutput the histogram:\n");
for(i=0;i<=nw;i++)
{
printf("Word ID: %d:\t",i);
for(j=0;j<wl[i];j++) /*单词长度以星号输出*/
putchar('*');
putchar('\n');
}
putchar('\n');
system("pause");
return 0;
}
kingteng 2010-04-25
  • 打赏
  • 举报
回复

for(i=0;i<LENGTH&&((ch=getchar())!=EOF);i++)
20 {
21 if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
22 {
23 if(state==OUT) /*字符为单词的首个字母*/
24 {
25 nw++;
26 nc++;
27 state=IN;
28 }
29 else nc++;
30 }
31 else if(ch == ' ')
32 {
33 wl[nw]=nc;
34 nc=0;
35 state =OUT;
36 }
37 else if(ch == '\n')
38 {
39 wl[nw]=nc;
40 break;
41 }
42 }

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧