程序要求:编写程序,打印输入单词长度的垂直方向的直方图(大概形状请看最下面).
注:此题出自<<C程序设计语言(第2版.新版)>>之练习1-13.
下面的程序有一个BUG.当我多次输入' '或'\n'或'\t'时,这些控制符前面的单词长度line[i](详见程序中)就成了0.查看函数 countAndLengthOfWords(int line[],int lim) 多次未果.联系其他函数,包括主函数仍未果 .
特次请教各位大虾!!!
谢谢!!!
#include<stdio.h>
#define MAXMUBER 1000
int countAndLengthOfWords(int line[],int lim);
int maxLen(int line[],int lim,int & nw);
int main()
{
int line[MAXMUBER];
int max;
int nw;
int i;
int keep;
max=maxLen(line,MAXMUBER,nw);
keep=max;
printf("---打印输入中单词长度的垂直方向的直方图---\n\n\n\n");
while(max>0){
for(i=0;i<nw;i++)
if(line[i]/max)
printf(" *");
else
printf(" ");
if(i==nw)
putchar('\n');
max--;
}
for(i=0;i<nw;i++)
printf("%3d",i+1);
if(i=nw)
putchar('\n');
for(i=0;i<nw;i++)
printf("%3d",line[i]);
if(i=nw)
putchar('\n');
printf(" %d %d ",nw,keep);
return 0;
}
int maxLen(int line[],int lim,int &nw)
{
int max=0;
int i;
if((nw=countAndLengthOfWords(line,lim ))>0){
for(i=0;i<nw;i++)
if(line[i]>max)
max=line[i];
}
return max;
}
#define IN 1
#define OUT 0
int countAndLengthOfWords(int line[],int lim)
{
int c,nw,len,state;
state=OUT;
nw=len=0;
while((c=getchar())!=EOF&&nw<=lim) {
if(c==' '||c=='\n'||c=='\t'){
if(state=IN){
line[nw-1]=len;
len=0;
}
state=OUT;
}
else {
len++;
if(state==OUT) {
state=IN;
++nw;
}
}
}
return nw;
}
执行:
#include<stdio.h>
#define MAXMUBER 1000
int countAndLengthOfWords(int line[],int lim);
int maxLen(int line[],int lim,int & nw);
int main()
{
int line[MAXMUBER];
int max;
int nw;
int i;
int keep;
max=maxLen(line,MAXMUBER,nw);
keep=max;
printf("---打印输入中单词长度的垂直方向的直方图---\n\n\n\n");
while(max>0){
for(i=0;i<nw;i++)
if(line[i]/max)
printf(" *");
else
printf(" ");
if(i==nw)
putchar('\n');
max--;
}
for(i=0;i<nw;i++)
printf("%3d",i+1);
if(i=nw)
putchar('\n');
for(i=0;i<nw;i++)
printf("%3d",line[i]);
if(i=nw)
putchar('\n');
printf(" %d %d ",nw,keep);
return 0;
}
int maxLen(int line[],int lim,int &nw)
{
int max=0;
int i;
if((nw=countAndLengthOfWords(line,lim ))>0){
for(i=0;i<nw;i++)
if(line[i]>max)
max=line[i];
}
return max;
}
#define IN 1
#define OUT 0
int countAndLengthOfWords(int line[],int lim)
{
int c,nw,len,state;
state=OUT;
nw=len=0;
while((c=getchar())!=EOF&&nw<=lim) {
if(c==' '||c=='\n'||c=='\t'){
if(state=IN){
line[nw-1]=len;
len=0;
}
state=OUT;
}
else {
len++;
if(state==OUT) {
state=IN;
++nw;
}
}
}
return nw;
}
#include<stdio.h>
#define MAXMUBER 1000
int countAndLengthOfWords(int line[],int lim);
int maxLen(int line[],int lim,int & nw);
int main()
{
int line[MAXMUBER];
int max;
int nw;
int i;
int keep;
max=maxLen(line,MAXMUBER,nw);
keep=max;
printf("---打印输入中单词长度的垂直方向的直方图---\n\n\n\n");
while(max>0){
for(i=0;i<nw;i++)
if(line[i]/max)
printf(" *");
else
printf(" ");
if(i==nw)
putchar('\n');
max--;
}
for(i=0;i<nw;i++)
printf("%3d",i+1);
if(i=nw)
putchar('\n');
for(i=0;i<nw;i++)
printf("%3d",line[i]);
if(i=nw)
putchar('\n');
printf(" %d %d ",nw,keep);
return 0;
}
int maxLen(int line[],int lim,int &nw)
{
int max=0;
int i;
if((nw=countAndLengthOfWords(line,lim ))>0){
for(i=0;i<nw;i++)
if(line[i]>max)
max=line[i];
}
return max;
}
#define IN 1
#define OUT 0
int countAndLengthOfWords(int line[],int lim)
{
int c,nw,len,state;
state=OUT;
nw=len=0;
while((c=getchar())!=EOF&&nw<=lim) {
if(c==' '||c=='\n'||c=='\t'){
if(state=IN){
line[nw-1]=len;
len=0;
}
state=OUT;
}
else {
len++;
if(state==OUT) {
state=IN;
++nw;
}
}
}
return nw;
}
执行:
sadfsadfsajdfjsadklfjsadkl kjsdaf ksadjfklsadjfklsadjf kasdjfjskaldfj sdajkf
sadf
dsjf
fj jjdf fj jfds fjd fjs
fsdj
jfa
^Z
---打印输入中单词长度的垂直方向的直方图---
*
*
*
*
*
*
*
*
*
*
*
*
* *
* *
* *
* *
* *
* *
* *
* *
* * *
* * *
* * *
* * *
* * *
* * *
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
26 6 0 14 0 0 0 0 0 0 0 0 0 0 0
15 26 Press any key to continue
错处:lin[4...14]=0,显然不合理.