/*******************************************Form Events*************************************************************/ //When load File button is clicked... private void UI_ToolStrip_LoadFile_Click(object sender, EventArgs e) { //If user opens file dialog and checks ok if (UI_openFileDialog.ShowDialog().Equals(DialogResult.OK)) { StreamReader srInput = new StreamReader(UI_openFileDialog.FileName); string line = ""; try { //While end of file has not been reached while ((line = srInput.ReadLine()) != null) { //Create a package that reads the line and splits of space characters Package pack = new Package(line.Split(' ')); //If the global package list already has this package with the same name, merges it based on the index at which it is found in the list if (packageList.Contains(pack)) { packageList[packageList.FindIndex(a => a.Equals(pack))].MergePackage(pack); } //Otherwise, it is a new package, add it to the list else { packageList.Add(pack); } } } catch (Exception er) { MessageBox.Show("Error Loading File: " + er.Message, "Load File", MessageBoxButtons.OK, MessageBoxIcon.Error); } //Show File output into list view ShowSelectedLoad(); } }
private void UI_button_Read_Click(object sender, EventArgs e) { if (UI_openFileDialog.ShowDialog() == DialogResult.OK) { //Variable declarations---------------------------------------------------------- var sReadIn = ""; List <string> sList; Dictionary <string, int> lineSumDict = new Dictionary <string, int>(); Dictionary <int, List <string> > sumListDict = new Dictionary <int, List <string> >(); StreamReader sr = new StreamReader(UI_openFileDialog.FileName); //------------------------------------------------------------------------------- //Read in all info, close stream sReadIn = sr.ReadToEnd(); sr.Close(); //Split the string, on \r \n sList = sReadIn.Split(new char[] { '\n', '\r' }).ToList(); //Remove empty lines, or only whitespace sList.RemoveAll(line => line.Length == 0 || char.IsWhiteSpace(line[0])); //Load the dictionary with <line, sum of each character> sList.ForEach(line => lineSumDict[line] = line.Sum(character => character)); //Flipping the key/values, making a list of the keys foreach (var kvp in lineSumDict) { //Our dictionary doesn't have this key yet if (!sumListDict.ContainsKey(kvp.Value)) { //Make a new list sumListDict[kvp.Value] = new List <string>(); } //Add to our list sumListDict[kvp.Value].Add(kvp.Key); } //Order by our key, now the int sum of characters sumListDict = sumListDict.OrderBy(k => k.Key).ToDictionary(k => k.Key, v => v.Value); //Display Code------------------------------------------------------------- UI_label.Text = ""; UI_label.Text += $"Lowest ASCII Sum: {sumListDict.First().Key}\n"; UI_label.Text += $"Lowest String: {sumListDict.First().Value.Min()}\n"; UI_label.Text += $"Highest ASCII Sum: {sumListDict.Last().Key}\n"; UI_label.Text += $"Highest String: {sumListDict.Last().Value.Max()}/{new string(sumListDict.Last().Value.Max().OrderBy(c => c).ToArray())}"; } }