统计字母出现次数,打印柱状图

来源:互联网 发布:java md5密钥加密解密 编辑:程序博客网 时间:2024/05/20 09:27

编写一个程序接收4行大写字母组成的字符串(每行字符串中字符的个数不能超过72),打印每个字母(不考虑空格、标点符号、分隔符和数字)在这4行字符串文本输入中出现的字数,并按照要求的输出格式显示结果。

【输入】

 共4行,每行输入的都是大写字母构成的字符串,并且每行长度不超过72

【输出】

 1行或者多行;使用*号表示字母出现的次数,其中每个大写字母之间用空格进行分割。要求每一行的结尾不要输出不必要的空格,在空行部分不要输入其他的符号(比如,等)。

【样例输入】

THE QUICKBROWN FOX JUMPED OVER THE LAZY DOG.

THIS ISAN EXAMPLE TO TEST FOR YOUR

HISTOGRAMPROGRAM

HELLO!

【样例输出】

 

仅实现主要功能。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 统计字母柱状图{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("请输入四行英文大写字母,每行字母不超过72个:");            string input = Console.ReadLine();            input += Console.ReadLine();            input += Console.ReadLine();            input += Console.ReadLine();            char[] inputArray = input.ToCharArray();            char[] Letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',                                  'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };            int[] count = new int[Letters.Length];            //统计各个字母出现的个数            for (int j = 0; j < Letters.Length; j++)            {                for (int k = 0; k < inputArray.Length; k++)                {                    if (Letters[j] == inputArray[k])                    {                        count[j]++;                    }                }            }            //for (int i = 0; i < count.Length; i++)            //{            //    Console.WriteLine(Letters[i] + "的个数为:" + count[i]);            //}            //Console.ReadLine();//输出统计出的各个字母的个数            //找出出现次数最多的字母及个数,定义一个矩阵。            int max = count.Max();            int row = max;            int [,] Layout=new int[max,26];            //给矩阵赋值,目的是为了根据矩阵输出柱状图。            //输出柱状图,0输出空格,1输出“*”            for (int l = 0; l < max; l++)            {                for (int m = 0; m < Letters.Length; m++)                {                    if (count[m] < row)                    {                        Layout[l, m] = 0;                    }                    else                    {                        Layout[l, m] = 1;                    }                                    }                row--;            }            //输出柱状图            for (int x = 0; x < max; x++)            {                for (int y = 0; y < Letters.Length; y++)                {                    if (Layout[x, y] == 0)                    {                        Console.Write("  ");                    }                    else                        Console.Write("* ");                }                Console.WriteLine();            }            //输出字母            for (int z = 0; z < Letters.Length; z++)            {                Console.Write(Letters[z]+" ");            }            Console.ReadLine();                               }           }}