博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于WordCount的作业
阅读量:4325 次
发布时间:2019-06-06

本文共 6553 字,大约阅读时间需要 21 分钟。

一、开发者:201631062418

二、代码地址:https://gitee.com/YsuLIyan/WordCount

三、作业地址:https://edu.cnblogs.com/campus/xnsy/Test/homework/2203

 

四、基本功能

 

WordCount.exe -c test.txt     //返回文件 test.txt的字符数

 

WordCount.exe -w  test.txt     //返回文件 test.txt的单词总数

 

WordCount.exe -l  test.txt     //返回文件 test.txt的总行数

 

WordCount.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

 

五、扩展功能

-----------------------------------------------------------------------------------------------------------------------------------------------

已经实现:-c -w -l 可以同时使用,如果没有-o命令(或者说-0之后没有接文件名),则默认outputfile.txt作为输出结果的文件(strPath)。并且扫描-0命令之后的接收结果文件名称,如果接收结果文件在该目标文件夹中不存在,则会报路径错误

-----------------------------------------------------------------------------------------------------------------------------------------------

还未实现:接收结果文件在目标文件夹中不存在时,重新创建一个名为(strPath)的接收结果文件--待后期改善。

     在命令读取时,程序对于用户输入的命令进行理解、更多的应答以及合适的补充。

-----------------------------------------------------------------------------------------------------------------------------------------------

六、项目的实现

这是一个专门处理命令的方法类,这也是我比较在意的地方,因为我很注重我写的程序对用户输入的参数的应答。

class CommandRead    {        bool isRows = false;        bool isChara = false;        bool isWord = false;        bool isAimPath = false;        string[] path = new string[4];        string[] commands;        public bool IsRows        {            get { return isRows; }        }        public bool IsChara        {            get { return isChara; }        }        public bool IsWord        {            get { return isWord; }        }        public bool IsAimPath        {            get { return isAimPath; }        }        public string[] Path        {            get { return path; }        }        public void CommandImport(string[] args)        {            commands = args;            if(commands == null)            {                Console.WriteLine("请输入指令");            }            else            {                int Pas = 0;                for (int i = 0; i < commands.Length; i++)                {                    if (commands[i] == "-l" || commands[i] == "-L")//行数                    {                        isRows = true;                    }                    else if (commands[i] == "-w" || commands[i] == "-W")//单词                    {                        isWord = true;                    }                    else if (commands[i] == "-c" || commands[i] == "-C")//字符                    {                        isChara = true;                    }                    else if (commands[i] == "-o" || commands[i] == "-O")//输出                    {                        if (i + 2 == commands.Length)                            isAimPath = true;                    }                    else                    {                        path[Pas] = commands[i];                        Pas++;                    }                }            }        }            }

这是关于读取文件行数的类

class Rows //行数    {        public string ope;        public int Line(string filename)        {            Encoding encod = Encoding.GetEncoding("GB2312");            FileStream fs = new FileStream(filename, FileMode.Open);            StreamReader sr = new StreamReader(fs, encod);            string line;            int CountLine = 0;//记录行数            //当前行不为空            while((line = sr.ReadLine()) != null)            {                CountLine++;            }            //应该先关闭文件再return            sr.Close();            fs.Close();            return CountLine;//注意函数是有返回值的        }    }

这是读取文件符号数的类

class Word//字符    {        public int TotalWord(string filename)        {            Encoding encod = Encoding.GetEncoding("GB2312");            FileStream fs = new FileStream(filename, FileMode.Open);            StreamReader sr = new StreamReader(fs, encod);            int nchar;            int WordCount = 0;            char[] symbol = { ' ', '\t', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{
', '}', '(', ')', '+', '-', '*', '=' };//间隔符 while((nchar = sr.Read())!= -1) { foreach(char c in symbol) { if(nchar == (int)c) { WordCount++; } } } sr.Close(); fs.Close(); return WordCount; } }

这是读取单词数的类

class Character//单词    {        public string ope;        public int TotalWord(string filename)        {            Encoding encod = Encoding.GetEncoding("GB2312");            FileStream fs = new FileStream(filename, FileMode.Open);            StreamReader sr = new StreamReader(fs, encod);            int charcount = 0;            int nchar;            while((nchar = sr.Read())!= -1) //sr.Read()是以特定的编码方式读取流而获取一个接一个的字符,而ReadLine()是读取一行            {                charcount++; // 统计字符数            }            //关闭文件            sr.Close();            fs.Close();            return charcount;        }    }

这是将得到的信息写入指定文件的方法类

class WriteFile    {        public void Write(string theword)        {            FileStream fs = new FileStream("outputfile.txt", FileMode.Create);            //获得字节数            byte[] data = System.Text.Encoding.Default.GetBytes(theword);            //写入            fs.Write(data, 0, data.Length);            //清空缓存区,关闭流            fs.Flush();            fs.Close();        }        public void Write(string path,string theword)        {            bool isInFile = false;            String rootPath = Directory.GetCurrentDirectory();            DirectoryInfo root = new DirectoryInfo(rootPath);            foreach(FileInfo f in root.GetFiles())            {                if(path == f.Name)                {                    isInFile = true;                }            }            if (isInFile != false)            {                FileStream fs = new FileStream(path, FileMode.Create);                //获得字节数                byte[] data = System.Text.Encoding.Default.GetBytes(theword);                //写入                fs.Write(data, 0, data.Length);                //清空缓存区,关闭流                fs.Flush();                fs.Close();            }            else            {                Console.WriteLine("Path Error!");            }        }    }

然后就是主函数

class Program    {                 static void Main(string[] args)        {            string[] cmds = args;            string theWord = "";            int ope1 = 0,ope2 = 0,ope3 = 0;            for(int i =0; i

七、项目测试

test.txt文件内容:

进行测试

测试结果

测试2

测试结果

 

心得感悟:其实刚好我在C#方面对于文件的读写这一块并不是很熟练,但是刚好该题就让我去读写文件,而且我并不是很清楚如何在cmd里面调用.exe以及传入参数。

 

转载于:https://www.cnblogs.com/NIsong/p/9827240.html

你可能感兴趣的文章
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>