#include
/* NUM_CHARS should really be CHAR_MAX but K&R haven't covered that at this stage in the book /
#define NUM_CHARS 256
int main(void)
{
int c;
long freqarr[NUM_CHARS + 1];
long thisval = 0;
long maxval = 0;
int thisidx = 0;
for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++)
{
freqarr[thisidx] = 0;
}
while((c = getchar()) != EOF)
{
if(c < NUM_CHARS)
{
thisval = ++freqarr[c];
if(thisval > maxval)
{
maxval = thisval;
}
}
else
{
thisval = ++freqarr[NUM_CHARS];
if(thisval > maxval)
{
maxval = thisval;
}
}
file:///E|/我的下载资料/程序设计/c语言/The_C...guage中文英文答案/C程序设计语言(第二版)课后答案.htm(第 16/246 页)2008-5-15 11:00:46
The C Programming Language Exercise
}
for(thisval = maxval; thisval > 0; thisval--)
{
printf("%4d |", thisval);
for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++)
{
if(freqarr[thisidx] >= thisval)
{
printf("");
}
else if(freqarr[thisidx] > 0)
{
printf(" ");
}
}
printf("\n");
}
printf(" +");
for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++)
{
if(freqarr[thisidx] > 0)
{
printf("-");
}
}
printf("\n ");
for(thisidx = 0; thisidx < NUM_CHARS; thisidx++)
{
if(freqarr[thisidx] > 0)
{
printf("%d", thisidx / 100);
}
}
printf("\n ");
for(thisidx = 0; thisidx < NUM_CHARS; thisidx++)
{
if(freqarr[thisidx] > 0)
{
printf("%d", (thisidx - (100 * (thisidx / 100))) / 10 );
}
}
printf("\n ");
for(thisidx = 0; thisidx < NUM_CHARS; thisidx++)
{
if(freqarr[thisidx] > 0)
{
printf("%d", thisidx - (10 * (thisidx / 10)));
}
}
if(freqarr[NUM_CHARS] > 0)
{
printf(">%d\n", NUM_CHARS);
}
printf("\n");
return 0;
file:///E|/我的下载资料/程序设计/c语言/The_C...guage中文英文答案/C程序设计语言(第二版)课后答案.htm(第 17/246 页)2008-5-15 11:00:46
The C Programming Language Exercise
}
我不明白在编写坐标时,为什么要将thisidx除以100或10来计算
编写一个程序,打印输入中各个字符出现频度的直方图
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答
悬赏问题
- ¥50 永磁型步进电机PID算法
- ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
- ¥88 找成都本地经验丰富懂小程序开发的技术大咖
- ¥15 如何处理复杂数据表格的除法运算
- ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
- ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
- ¥200 uniapp长期运行卡死问题解决
- ¥15 latex怎么处理论文引理引用参考文献
- ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
- ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?