public void Anword(string txt, int m = 1, int n = 10, string output = "output.txt") { GetDic cl = new countletter(); var word = cl.CountLetter(txt, m); StreamWriter sw = new StreamWriter(output); try { Console.SetOut(sw); CountChar c = new countchar(); CountWords cw = new countwords(); CountLines cls = new countlines(); SortWord sort = new sortwords(); Console.WriteLine("characters: {0}", c.countChar(txt)); Console.WriteLine("lines: {0}", cls.countLines(txt)); Console.WriteLine("words: {0}", cw.coutWords(word)); sort.sortWord(txt, m, n, output = "output.txt"); } catch (Exception e) { Console.WriteLine("输出文件错误!"); } finally { sw.Flush(); sw.Close(); } }
void SortWord.sortWord(string txt, int m = 1, int n = 10, string output = "output.txt") { GetDic cl = new countletter(); var word = cl.CountLetter(txt, m); var list = word.ToList(); list.Sort((x, y) => { if (x.Value == y.Value) { return(x.Key.CompareTo(y.Key)); } return(-x.Value.CompareTo(y.Value)); }); for (int i = 0; i < n; i++) { if (list.Count >= i + 1) { Console.WriteLine("{0}: {1}", list[i].Key, list[i].Value); } } }