C#如何在控制台应用程序中制作水平条形图

时间:2019-05-14 23:42:13

标签: c#

我需要制作一个水平条形图,以表示直方图字典中数字的出现。

我尝试弄乱Console.BackgroundColor,但是,这显然只会使线条背景颜色变成蓝色。

static void Main(string[] args)
        {
            string Speach;
            Speach = "I say to you today, my friends, so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. " +
            "I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident: that all men are created equal. " +
            "I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood. " +
            "I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice. " +
            "I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character. " +
            "I have a dream today. I have a dream that one day, down in Alabama, with its vicious racists, with its governor having his lips dripping with the words of interposition and nullification; one day right there in Alabama, little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. " +
            "I have a dream today. I have a dream that one day every valley shall be exalted, every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see it together. " +
            "This is our hope. This is the faith that I go back to the South with. With this faith we will be able to hew out of the mountain of despair a stone of hope. With this faith we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. " +
            "With this faith we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom together, knowing that we will be free one day. " +
            "This will be the day when all of God's children will be able to sing with a new meaning, My country, 'tis of thee, sweet land of liberty, of thee I sing. Land where my fathers died, land of the pilgrim's pride, from every mountainside, let freedom ring. " +
            "And if America is to be a great nation this must become true. So let freedom ring from the prodigious hilltops of New Hampshire. Let freedom ring from the mighty mountains of New York. Let freedom ring from the heightening Alleghenies of Pennsylvania! " +
            "Let freedom ring from the snowcapped Rockies of Colorado! Let freedom ring from the curvaceous slopes of California! But not only that; let freedom ring from Stone Mountain of Georgia! " +
            "Let freedom ring from Lookout Mountain of Tennessee! Let freedom ring from every hill and molehill of Mississippi. From every mountainside, let freedom ring. " +
            "And when this ha   ppens, when we allow freedom to ring, when we let it ring from every village and every hamlet, from every state and every city, we will be able to speed up that day when all of God's children, black men and white men, Jews and Gentiles, Protestants and Catholics, will be able to join hands and sing in the words of the old Negro spiritual, Free at last! free at last! thank God Almighty, we are free at last!";

            Console.WriteLine("Histogram");
            Console.WriteLine();
            Console.WriteLine("1. Show Histogram");
            Console.WriteLine("2. Search for Word");
            Console.WriteLine("3. Exit");
            Console.WriteLine("Choice?");
            string choice = Console.ReadLine();
            int choiceInt = Int32.Parse(choice);

            string[] SpeachSplit = Speach.Split();
            Dictionary<string, int> Historgram = new Dictionary<string, int>();


            if (choiceInt == 1)
            {    
                    var result = Speach.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                    .Select(x => Regex.Replace(x.ToLower(), "[^a-zA-Z0-9-]", ""))
                                    .GroupBy(x => x)
                                    .ToDictionary(x => x.Key, x => x.Count());

                ConsoleColor BarColor;
                Console.BackgroundColor = ConsoleColor.Blue;
                BarColor = Console.BackgroundColor;

                foreach (var item in result.OrderByDescending(x => x.Value))
                        Console.WriteLine($"{item.Key} {BarColor}  {item.Value}");


            }
            else if(choiceInt == 2)
            {
                Console.WriteLine("Search word to find?");
                string wordSearch = Console.ReadLine();

                if (Historgram.TryGetValue(wordSearch, out int value))
                {
                    Console.WriteLine($"{wordSearch}, value = {0}.", wordSearch);
                }
                else
                {
                    Console.WriteLine($"{wordSearch} is not found.");
                }
            }
            else
            {
                Console.ReadLine();

            }
            Console.ReadLine();

我尝试查找并阅读许多有关如何执行此操作的文章,但这似乎非常复杂。我只是C#的初学者,所以我对如何使此单杠感到困惑。最后,它应该看起来像这样: enter image description here

一个简化的问题:如何制作一个直方图来表示直方图字典中数字的出现?

1 个答案:

答案 0 :(得分:1)

好吧,您不会使用icacls,而是使用老式的foreach loop for而不是Write

WriteLine

这是假设它将适合控制台。

这里还有其他问题,您的单词右对齐,并且还需要学习如何在控制台中为文本着色。但是,请把这些有趣的细节留给您吧。

更新

...

// print x amount of something
for(var i = 0; i < wordlength; i++)
   Console.Write("#"); 

// this creates the new line
Console.WriteLine(wordCount); 

用法

public static void Print(List<KeyValuePair<string,int>> list)
{

   // get the max length of all the words so we can align
   var max = list.Max(x => x.Key.Length);

   foreach (var item in list)
   {
      // right align using PadLeft and max length
      Console.Write(item.Key.PadLeft(max));

      Console.Write(" ");

      // change color
      Console.BackgroundColor = ConsoleColor.DarkBlue;

      // Write the bars
      for (var i = 0; i < item.Value; i++)
         Console.Write("#");

      // change back
      Console.BackgroundColor = ConsoleColor.Black;

      Console.Write(" ");
      // this creates the new line
      Console.WriteLine(item.Value);
   }
}

输出

enter image description here