打印输入中各个字符出现的频率,结果存在bug
程序代码://问题:输出结果中,直方图按照由小到大排列,不论所对应的字符类别原本频率
#include<stdio.h>
int max(int x,int y)
{
int z;
z=(x>y)?x:y;
return (z);
}
int main()
{
char input[100];
int i,top;
int letter,space,number,other;//分别用以存放字母数、空格数、数字数及其它字符数
letter=space=number=other=0;
printf("请输入一段字符:\n");
gets(input);//输入一段字符
for(i=0;input[i]!='\0';i++)//计算各类字符的数量
{
if(input[i]>='0' && input[i]<='9') number++;
else if((input[i]>='a' && input[i]<='z')||(input[i]>='A' && input[i]<='Z')) letter++;
else if(input[i]==' ') space++;
else other++;
}
top=max(other,max(number,max(space,letter)));
printf("以下为输入中各字符出现频率的直方图:\n");
for(;top>0;top--)
{
printf("%-4d",top);
if(letter>=top) printf("*\t");
if(space>=top) printf("*\t");
if(number>=top) printf("*\t");
if(other>=top) printf("*\t");
putchar(10);
}
printf(" L\tS\tN\tO\n");
return 0;
}[ 本帖最后由 日寂 于 2014-1-22 21:18 编辑 ]

