功能二实现过程

 

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace wf
{
    class Program
    {

        static void Main(string[] args)
        {

            string textFileName;
            Console.Write(">type ");
            textFileName = Console.ReadLine();
            StreamReader sreader = new StreamReader(@"C:\Users\dell-pc\desktop\test3.txt");
            string afile = sreader.ReadToEnd();
            afile = afile.ToLower();//转换为小写字母
            //将空格跟标点符号定义出来
            char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' };
            //分隔char[c]后产生的数组
            string[] S = afile.Split(c);
            //建立哈希表
            Hashtable ha = new Hashtable();
            for (int i = 0; i < S.Length; i++)
            {
                if (ha.ContainsKey(S[i]))
                {
                    ha[S[i]] = (int)ha[S[i]] + 1;
                }
                else
                {
                    ha.Add(S[i], 1);
                }
            }
            string[] arrKey = new string[ha.Count];//存键
            int[] arrValue = new int[ha.Count];//存值
            ha.Keys.CopyTo(arrKey, 0);
            ha.Values.CopyTo(arrValue, 0);
            Console.WriteLine();
            Console.WriteLine(">wf -s test.txt");
            Console.WriteLine("total " + ha.Count);
            Console.WriteLine();
            Array.Sort(arrValue, arrKey);//排序
            int n = 0;
            for (int i = arrKey.Length - 1; i >= 0; i--)
            {
                if ((string)arrKey[i] != "")
                {
                    if (n<10)
                    {
                        //输出前10位多的单词,右对齐
                        Console.Write(arrKey[i].ToString().PadRight (12));
                        Console.WriteLine(arrValue[i].ToString());
                        n=n+1;
                    }
                }
            }
        }
    }
}

 

 

我发现对于功能一的实现我是直接指定了文件的路径,在这种情况下无论输入什么都是打开指定URL的这个文件,那么>type后面输入什么都不重要,无论是test还是test.txt都会正常运行,所以我想做出一些改进,先获取文件名,在获取文件名的路径,但是又报错了,现在正在修改中:

通过百度得知Console.ReadLine()的作用是从标准输入流读取下一行字符,有些人说是避免无输入让程序闪退,所以我直接在后面加“.txt”应该是不对的,修改中~

最后还是选择直接把路径写在代码里,这样实现效果跟作业要求结果一样,比较简单,只是换个文件需要改一下代码里的源路径。。。

实现效果如下:

 

代码我会在deadline之前放到我的guihub上   https://github.com/YueYuan1995

算了我还是直接把代码粘上来吧这样比较方便(笑哭)

 

posted on 2017-09-18 11:53  袁玥  阅读(215)  评论(0编辑  收藏  举报

导航