示例#1
0
        public void ToFile(string Fpath, string Outpath)
        {
            //throw new NotImplementedException();
            excute                   doCount   = new excute();
            int                      countword = doCount.CountWord(Fpath);                                   //调用计算总单词数方法,结果保存在countword
            int                      countchar = doCount.CountChar(Fpath);                                   //调用计算总字符数方法,结果保存在countchar
            int                      countline = doCount.CountLine(Fpath);                                   //调用计算行数方法,结果保存在countline
            StreamWriter             sw        = null;
            Dictionary <string, int> a         = doCount.SortDictionary_Desc(doCount.CountFrequency(Fpath)); //调用计算频数并排序的方法,将结果保存到dictionary字典中

            if (Outpath == null)
            {
                //sw = new StreamWriter(@"");//在默认路径创建写文件流
                Console.WriteLine("请指定输出路径");
            }
            if (Outpath != null)
            {
                sw = new StreamWriter(Outpath); //在自定义path路径创建写文件流
            }
            Console.SetOut(sw);                 //结果写入文件
            Console.WriteLine("characters:" + countchar);
            Console.WriteLine("words:" + countword);
            Console.WriteLine("lines:" + countline);
            Console.WriteLine("Fre:");
            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();
        }
示例#2
0
        public int CountWord(string Fpath)
        {
            //throw new NotImplementedException();
            excute doCount = new excute();
            Dictionary <string, int> dictionary = CountFrequency(Fpath);
            int CountWord = 0;

            foreach (KeyValuePair <string, int> dic in dictionary)//遍历字典里面的每一个单词,结果为总单词数
            {
                CountWord += dic.Value;
            }
            return(CountWord);
        }
示例#3
0
        static void Main(string[] args)
        {
            StreamWriter sw      = null;
            Process      count   = new excute();
            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"://没有实现
                 *  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创建写文件流
                }
                Console.WriteLine("characters:" + count.CountChar(path));
                Console.WriteLine("words:" + count.CountWord(path));
                Console.WriteLine("lines:" + count.CountLine(path));
                sw.WriteLine(String.Format("characters:" + count.CountChar(path)));
                sw.WriteLine(String.Format("words:" + count.CountWord(path)));
                sw.WriteLine(String.Format("lines:" + count.CountLine(path)));

                if (GetNum != null)//将输出指定数量的单词数,并写入文件
                {
                    int i = 0;
                    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();
            }
        }