C-Print a histogram of the frequencies of different characters in its input

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

/*
 * Write a program to print a histogram of the frequencies of different characters 
 * in its input.
 *
 * Histogram_CharFreq.c - by FreeMan
 */

#include <stdio.h>

int main()
{
	int freqs[94];
	int height = 0;
	int c, i, k;

	for (i = 0; i <= 93; ++i)
	{
		freqs[i] = 0;
	}

	while ((c = getchar()) != EOF)
	{
		if (33 <= c && c <= 126)
		{
			++freqs[c - 33];
			if (freqs[c - 33] >= height)
			{
				height = freqs[c - 33];
			}
		}
	}

	for (i = height; i > 0; --i)
	{
		printf("%4d|", i);
		for (k = 0; k <= 93; ++k)
		{
			if (freqs[k] >= i)
			{
				putchar('*');
			}
			else
			{
				putchar(' ');
			}
		}
		putchar('\n');
	}

	printf("    +");
	for (i = 0; i <= 93; ++i)
	{
		putchar('-');
	}
	printf("\n    ");

	for (i = 0; i <= 93; ++i)
	{
		printf("%c", i + 33);
	}
	putchar('\n');

	return 0;
}

// Here's the output of the program when given its own source as input:
/*
  45|                                                                        *
  44|                                                                        *
  43|                                                                        *
  42|                                                                        *
  41|                                                                        *
  40|                                                                        *
  39|                                                                        *
  38|                                                                        *
  37|                                                                        *
  36|                                                                        *        *
  35|                                                                        *        *
  34|                                                                        *        *
  33|                                                                        *        *
  32|                                                                        *        *
  31|                                                                        *        *
  30|                                                                        *        * *
  29|                                                                        *        * *
  28|                                                                        *        * *
  27|                                                                    *   *        * *
  26|                          *                                         *   *        * *
  25|                          *                                         *   *        * *
  24|                          *                                         *   *        * *
  23|                          *                                         **  *        * *
  22|                          *                                         **  *        * *
  21|       **                 *                                         **  *        * *
  20|       **                 *                                         ** **    *   * *
  19|       **                 *                                       * ** **    *   * *
  18|       **                 * *                                     * ** **    *   * *
  17|       **                 * *                                     * ** **    *   * *
  16|       **                 * *                                   * * ** **    *   * *
  15|       **                 * *                                   * * ** **    *   * *
  14|       **         *       * *                                   * * ** **    *   * *
  13|       **         *       * *                                   * * ** **    *   ***
  12|       ** *       *       * *                                   * * ** **    *** ***
  11|       ** *       *       * *                                   * * ** **    *** ***      * *
  10|      *** *       *       * *                                   * * ** **    *** ***      * *
   9|      *** *       *       * *                                   * * ** **    *** ****     * *
   8| *    *** *    *  *       * *                                   * * *****    ********     * *
   7| *    *** * *  *  *       ***                                   * * *****    ********     * *
   6| *    ***** *  *  *       ***                             * *   * * *****    ********     * *
   5| *    ***** *  *  *     * ***                             * *   * * ***** *  ********     * *
   4| *    *******  *  *     * ****                            * *   * ******* * *********     * *
   3| *    ******** *  *     * ****       *                    ***   * ******* ***********     * *
   2| *  ************  **    * ****       *                    ***   * ******* ***********     * *
   1|*** **************** *  * ****    * ** *    * *       *   *** * ********* *********** * * ***
	+----------------------------------------------------------------------------------------------
	!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
*/

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/112367348