【发布时间】:2019-05-14 23:42:12
【问题描述】:
我需要制作一个水平条形图来表示我的直方图字典中出现的数字。
我尝试过使用 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# 的初学者,所以我对如何制作这个单杠感到很困惑。最后它看起来像这样:
简化问题:如何制作水平条形图来表示直方图字典中某个数字的出现?
【问题讨论】:
-
想象一下,您不必在 grid paper 上进行编码,一个正方形是一个 字符 长度,并且您有一个单词列表以及与他们相关的数字。你会采取什么步骤?现在这里的复杂性是 paper 只有 x 个单位 宽,那么你要如何在纸上放置大条?
-
@TheGeneral 每次出现单词时,您都会为一个正方形着色,但如何在 foreach 循环中做到这一点?
-
看看github.com/ericlippert/probability/blob/episode30/Probability/…——在这个文件中,我有一些方法可以按照您描述的方式创建水平和垂直直方图;这应该会给你一些灵感。
-
@EricLippert 我不确定这是否是灵感,或者只是直截了当:)
标签: c#