70,021
社区成员




#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;
}
#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;
}
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;
}
#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;
}
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 }