打印基于字长直方图(C) [英] Print a Histogram based on word lengths (C)

查看:106
本文介绍了打印基于字长直方图(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个K&安培; R运动(1-13)...


  

写一个程序打印直方图
  的话在其输入端的长度。
  这是很容易得出的柱状图
  水平线条;垂直
  取向是更具挑战性。


本节是关于阵列,并且说实话,我不知道我完全理解它。一切都到了这一点是相当容易掌握,这是不是。

反正我想首先做单杠的直方图。一旦我得到了下来,我会尽量垂直,但现在我甚至不知道在哪里用简单的版本开始。 (我睡了,醒来时,仍然无法得到它。)

我画了什么程序的例子将输出:

  -------------------------------------- --------------------------
001 | XX
002 | XXXX
003 | X
004 | XXXXXXXXXX
005 | XXXXXXXXXXXXXXXXXXXXXXXXX
006 | XXXX
007 | X
008 |
009 | XXXXXXXXX
010 | XXX
大于10 | XXXX
-------------------------------------------------- --------------

和试图打破它(程序)下来的部分。这是我想出了:


  

      
  1. 打印顶部边框

  2.   
  3. 打印类,PRINT X各自时间条件为真,PRINT NEWLINE,
      重复。

  4.   
  5. 打印底部边框

  6.   

不过,我想想越少,我认为这是它如何工作(因为的getchar()经过一个字符的时间,它不会能涨回去把一λ在正确的类别。)或者...

...我真的很困惑,我将​​如何解决这个问题。以下是据我已经能够得到code明智的:

 的#include<&stdio.h中GT;#定义MAXWORDLENGTH 10//打印输入字的长度的柱状图。单杠版本INT主要(无效)
{
  INT℃;
  而((C =的getchar())!= EOF){  }  返回0;
}

有人能帮忙告诉我吗?不一定与code,也许只是伪code,或用一些,从明智的话至于是什么,我需要做的,所想的什么。这款刚刚在路上一个真正的大石头,我想获得过去吧:/

(我会回来检查30分钟)


解决方案

我爱伪code!有一些很好的想法存在,但你仍然没有订购程序的权利。

就像你说的你自己,你不能阅读文本,回去在特定行打印一个X。如果我们确定它不能这样做,那么就没有选择,但要知道直方图的所有值事先。

所以,你应该觉得你的程序包含两个部分(你会做出这种划分几乎在你写的每一个程序):首先,这将使计算的一部分;然后一部分将输出它们在一定的格式(直方图)。

这个技巧应该让你开始!如果您需要进一步的帮助,下面的评论

This is a K&R exercise (1-13)...

"Write a program to print a histogram of the length of words in its input. It is easy to draw the histogram with bars horizontal; a vertical orientation is more challenging."

The section was about arrays, and to be honest, I'm not sure I fully understood it. Everything up to this point was fairly easy to grasp, this was not.

Anyway I'm trying to do a histogram with horizontal bars first. Once I got that down I'll try vertical, but right now I'm not even sure where to begin with the easy version. (I slept on it, woke up, and still couldn't get it.)

I drew an example of what the program would output:

----------------------------------------------------------------
001|XX
002|XXXX
003|X
004|XXXXXXXXXX
005|XXXXXXXXXXXXXXXXXXXXXXXXX
006|XXXX
007|X
008|
009|XXXXXXXXX
010|XXX
>10|XXXX
----------------------------------------------------------------

And tried to break it (the program) down in sections. This is what I came up with:

  1. PRINT TOP BORDER
  2. PRINT CATEGORY, PRINT X EACH TIME CONDITION IS TRUE, PRINT NEWLINE, REPEAT.
  3. PRINT BOTTOM BORDER

But the more I think about it the less I think that's how it would work (because getchar() goes through one character at a time, and it wouldn't be able to go back up to put a X in the right category.) Or...

... I'm just really confused as to how I would solve this problem. Here's as far as I've been able to get code wise:

#include <stdio.h>

#define MAXWORDLENGTH 10

// print a histogram of the length of words in input. horizontal bar version

int main(void)
{
  int c;
  while ((c = getchar()) != EOF) {

  }

  return 0;
}

Could someone help enlighten me? Not necessarily with the code, maybe just pseudo code, or with some "words from the wise" as to what I need to do, or think, or something. This has just been a really big stone in the road and I'd like to get past it :/.

(I'll check back in 30 minutes)

解决方案

I loved the pseudo-code! Some good thinking there, but you're still not ordering your program right.

As you said yourself, you can't read the text, go back and print an X in a particular row. If we establish that it can't be done, then there's no choice but to know all the values of the histogram beforehand.

So you should think your program as having two parts (and you'll make this kind of division in practically every program you write): first, a part that will make calculations; and then a part that will output them in a certain format (the histogram).

This tip should get you started! If you need further help, comment below.

这篇关于打印基于字长直方图(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆