public void TestMethod1()//测试doCount.CountLine计算文章总行数 { DoCount doCount = new DoCount(); int line = doCount.CountLine("e:\\Text.txt"); Console.WriteLine(line); }
public void TestMethod2()//测试doCount.CountFrequency计算每个单词频数 { DoCount doCount = new DoCount(); Dictionary <string, int> dictionary = doCount.CountFrequency("e:\\Text1.txt"); dictionary = doCount.SortDictionary_Desc(dictionary); foreach (KeyValuePair <string, int> dic in dictionary) { Console.WriteLine("单词:" + dic.Key + "\t " + "\t频数:" + dic.Value); } }
public void TestMethod4()//测试doCount.CountCahr计算文章总的字母个数 { DoCount doCount = new DoCount(); doCount.WriteToTxt("e:\\Text1.txt", "e:\\output.txt"); }
public void TestMethod3()//测试doCount.CountCahr计算文章总的字母个数 { DoCount doCount = new DoCount(); Console.WriteLine(doCount.CountChar("e:\\Text1.txt")); }
static void Main(string[] args) { StreamWriter sw = null; Count count = new DoCount(); string path = null; //读入文件路径标志 string outPath = null; //写出文件路径标志 string GetExNum = null; //限制输出个数的值 string GetNum = null; //指定频数的值 for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "-i": path = args[i + 1]; //寻找是否找到输入文件路径 break; case "-m": //-m参数与数字配套使用,用于设置词组长度 GetExNum = args[i + 1]; break; case "-n": //-n参数与数字搭配使用,用于限制最终输出的单词的个数 GetNum = args[i + 1]; break; case "-o": //-o表示输出路径 outPath = args[i + 1]; break; } } if (path != null) { if (outPath != null) { sw = new StreamWriter(outPath); //在outPath创建写文件流 } if (GetExNum != null) //将查找指定频数的结果输出,并写入文件 { Dictionary <string, int> dictionary = count.CountFrequency(path); dictionary = count.SortDictionary_Desc(dictionary); Console.WriteLine("频数为" + GetExNum + "单词如下:"); foreach (KeyValuePair <string, int> dic in dictionary) { if (dic.Value == int.Parse(GetExNum)) { sw.WriteLine(String.Format("{0,-10} |{2,5}", "单词:" + dic.Key, 0, "频数:" + dic.Value)); Console.WriteLine(String.Format("{0,-10} |{2,5}", "单词:" + dic.Key, 0, "频数:" + dic.Value)); } } } if (GetNum != null)//将输出指定数量的单词数,并写入文件 { int i = 1; Dictionary <string, int> dictionary = count.CountFrequency(path); dictionary = count.SortDictionary_Desc(dictionary); sw.WriteLine("前" + GetNum + "频数的单词如下:"); Console.WriteLine("前" + GetNum + "频数的单词如下:"); foreach (KeyValuePair <string, int> dic in dictionary) { sw.WriteLine(String.Format("{0,-10} |{2,5}", "单词:" + dic.Key, 0, "频数:" + dic.Value)); Console.WriteLine(String.Format("{0,-10} |{2,5}", "单词:" + dic.Key, 0, "频数:" + dic.Value)); i++; if (i == int.Parse(GetNum)) { break; } } } } else { Console.WriteLine("参数错误!"); } if (sw != null) { sw.Flush(); sw.Close(); } //Console.WriteLine("以上均已写入文件!"); }