<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Linux Forum &mdash; K&R, изд. 3, исп., или начал изучать Си.]]></title>
	<link rel="self" href="https://linuxforum.ru/extern.php?action=feed&amp;tid=41114&amp;type=atom" />
	<updated>2023-08-27T19:57:57Z</updated>
	<generator>PunBB</generator>
	<id>https://linuxforum.ru/viewtopic.php?id=41114</id>
		<entry>
			<title type="html"><![CDATA[K&R, изд. 3, исп., или начал изучать Си.]]></title>
			<link rel="alternate" href="https://linuxforum.ru/viewtopic.php?pid=471761#p471761" />
			<content type="html"><![CDATA[<p>Всем привет! Давненько ничего не писал <img src="https://linuxforum.ru/img/smilies/ab.gif" alt="ab" /> В общем, начал изучать Си. Дошел до упражнения 1.13, написал программу:<br /></p><div class="codebox"><pre><code>#include &lt;stdio.h&gt;
#define MAXCOUNT 1000
#define OUTW 0
#define INW 1
#define DELIMITER &quot;  &quot;

/* Write a program that makes a word length histo */

int main() {
  int wordlen[MAXCOUNT];
  int c, i, j, wc, m, state;
  for (i = 0; i &lt; MAXCOUNT; ++i) wordlen[i] = 0;
  c = i = j = wc = m = 0;

  state = OUTW;
  while ((c = getchar()) != EOF &amp;&amp; wc &lt; MAXCOUNT - 1 ) {
    if (c == &#039; &#039; || c == &#039;\t&#039; || c == &#039;\n&#039;) {
      state = OUTW;
      ++wordlen[wc];
      ++wc;
    }
    else if (state == OUTW)
      state = INW;
    else ++wordlen[wc];
  }

  printf(&quot;Building histograms. First, horizontal:\n&quot;);
  for (i = 0; i &lt; wc; ++i) {
    printf(&quot;word %d: &quot;, i);
    for (j = 0; j &lt; wordlen[i]; ++j) putchar(&#039;+&#039;);
    putchar(&#039;\n&#039;);
  }

  printf(&quot;Then, vertical:\n&quot;);
  /* what&#039;s the longest word? */
  m = 0;
  for (i = 0; i &lt; wc; ++i)
    if (wordlen[i] &gt; m) m = wordlen[i];

  for (i = 0; i &lt; m; ++i) {
    for (j = 0; j &lt; wc; ++j) {
      if (wordlen[j] &gt;= m) putchar(&#039;+&#039;);
      else putchar(&#039; &#039;);
      printf(DELIMITER);
    }
    putchar(&#039;\n&#039;);
    --m;
  }
  /* the legend under histogram */
  for (i = 0; i &lt; wc; ++i)
    printf(&quot;%d%s&quot;, i, DELIMITER);
  putchar(&#039;\n&#039;);
}</code></pre></div><p>Программа работает, но пока не придумал, как сделать горизонтальную гистограмму в том же стиле, что и вертикальная. Вот результат работы программы: </p><div class="codebox"><pre><code>./1.13_words_histo 
this is another words
Building histograms. First, horizontal:
word 0: ++++
word 1: ++
word 2: +++++++
word 3: +++++
Then, vertical:
      +     
      +     
      +  +  
+     +  +  
0  1  2  3  </code></pre></div><p>Покритикуйте <img src="https://linuxforum.ru/img/smilies/ab.gif" alt="ab" /></p>]]></content>
			<author>
				<name><![CDATA[yars]]></name>
				<uri>https://linuxforum.ru/profile.php?id=22799</uri>
			</author>
			<updated>2023-08-27T19:57:57Z</updated>
			<id>https://linuxforum.ru/viewtopic.php?pid=471761#p471761</id>
		</entry>
</feed>
