yymjiayou 2015-01-23 08:02 采纳率: 0%
浏览 2343

编写一个程序,打印输入中各个字符出现频度的直方图

#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来计算

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-01-23 09:29
    关注

    进行坐标变换,要不然图标会显得过大。

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站