/// <summary> /// 找到同时存在于字典和输入文件的单词并统计频率, /// 分别按照频率降序和字典升序输出。 /// </summary> /// <param name="filename">输入文件。</param> /// <param name="dictionaryFile">字典文件。</param> /// <param name="minLength">最小长度。</param> public static void LookUpDictionary(string filename, string dictionaryFile, int minLength) { // 初始化字典 var sr = new StreamReader(File.OpenRead(dictionaryFile)); var words = sr.ReadToEnd().Split(new[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); var dictionary = new BinarySearchSt <string, int>(); for (var i = 0; i < words.Length; i++) { if (words[i].Length > minLength) { dictionary.Put(words[i], i); } } sr.Close(); // 读入单词 var srFile = new StreamReader(File.OpenRead(filename)); var inputs = srFile.ReadToEnd().Split(new[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); srFile.Close(); var stDictionary = new BinarySearchSt <int, string>(); var stFrequency = new BinarySearchSt <string, int>(); foreach (var s in inputs) { if (stFrequency.Contains(s)) { stFrequency.Put(s, stFrequency.Get(s) + 1); } else if (dictionary.Contains(s)) { stFrequency.Put(s, 1); stDictionary.Put(dictionary.Get(s), s); } } // 输出字典序 Console.WriteLine("Alphabet"); foreach (var i in stDictionary.Keys()) { var s = stDictionary.Get(i); Console.WriteLine(s + "\t" + stFrequency.Get(s)); } // 频率序 Console.WriteLine("Frequency"); var n = stFrequency.Size(); for (var i = 0; i < n; i++) { var max = ""; stFrequency.Put(max, 0); foreach (var s in stFrequency.Keys()) { if (stFrequency.Get(s) > stFrequency.Get(max)) { max = s; } } Console.WriteLine(max + "\t" + stFrequency.Get(max)); stFrequency.Delete(max); } }