Example #1
0
        public void WriteToTxt(string path, string outPath)//将结果写入文件,path为读入文件路径(以下path均为读入文件路径),outPath为输入结果文件路径
        {
            DoCount                  doCount   = new DoCount();
            int                      countword = doCount.CountWord(path);                                   //调用计算总单词数方法,结果保存在countword
            int                      countchar = doCount.CountChar(path);                                   //调用计算总字符数方法,结果保存在countchar
            int                      countline = doCount.CountLine(path);                                   //调用计算行数方法,结果保存在countline
            StreamWriter             sw        = null;
            Dictionary <string, int> a         = doCount.SortDictionary_Desc(doCount.CountFrequency(path)); //调用计算频数并排序的方法,将结果保存到dictionary字典中

            if (outPath == null)
            {
                sw = new StreamWriter(@"E:\博客\201731062509\output.txt");//在默认路径创建写文件流
            }
            if (outPath != null)
            {
                sw = new StreamWriter(outPath); //在自定义path路径创建写文件流
            }
            Console.SetOut(sw);                 //结果写入文件
            Console.WriteLine("字符数:" + countchar);
            Console.WriteLine("单词数:" + countword);
            Console.WriteLine("行数:" + countline);
            Console.WriteLine("出现次数:");
            foreach (KeyValuePair <string, int> pair in a)//遍历a字典里面的每一条信息
            {
                string key   = pair.Key;
                int    value = pair.Value;
                Console.WriteLine("{0}  {1}", key, value);
            }
            sw.Flush();
            sw.Close();
        }
Example #2
0
        public int CountWord(string path)//计算总单词个数
        {
            DoCount doCount = new DoCount();
            Dictionary <string, int> dictionary = doCount.CountFrequency(path);
            int AllWord = 0;

            foreach (KeyValuePair <string, int> dic in dictionary)//遍历字典里面的每一个单词,结果为总单词数
            {
                AllWord += dic.Value;
            }
            return(AllWord);
        }