第二次作业重交

一、Gitee项目地址

   https://gitee.com/xuezhangDD/wc.git

二、基本思路  

  实现了基本功能:统计字符数、单词数、行数,以及将统计结果输入到默认文件中。根据args[]传入的参数,条件语句判断,来决定进行什么功能。

三、代码设计

  统计字符数:

public string charCount(string fileName)
            {
                
                string charCount = "0";
                string c = "字符数:";
                try
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open);  //打开文件
                    charCount = Convert.ToString(fs.Length);  //读取字符数
                    fs.Close();

                    /*foreach(char ch in fileName)  //只显示标点数
                    {
                        charCount++;
                    }*/

                    Console.WriteLine("字符数:" + charCount);
                    c += charCount;
                    return c;

                }
                catch
                {
                    Console.WriteLine("文件打开失败");
                    c += charCount;
                    return c;
                }
            }

在网上查阅后,开始我用了foreach遍历的方式 ,但只显示标点数,后来改成了C#里面自带的Length函数。 

 

  统计单词数:

public string wordCount(string fileName)
            {
                string wordCount = "0";
                string w = "单词数:";
                try
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open);
                    StreamReader sr = new StreamReader(fs, Encoding.Default);
                    string file = Convert.ToString(sr.ReadToEnd());  //读取文件
                    fs.Close();
                    sr.Close();
                    char[] separator = { ',', ' ', '' };  //逗号及空格为分隔符
                    string[] word = file.Split(separator, StringSplitOptions.RemoveEmptyEntries);  //按指定字符分隔字符串
                    wordCount = Convert.ToString(word.Length);
                    Console.WriteLine("单词数:" + wordCount);
                    w += wordCount;
                    return w;
                }
                catch
                {
                    Console.WriteLine("文件打开失败");
                    w += wordCount;
                    return w;
                }
            }

 以中英文逗号及空格为分隔符,分隔后的字符串长度为单词数。

 

    统计行数:

public string lineCount(string fileName)
            {
                string lineCount = "0";
                string l = "行数:";
                try
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open);
                    StreamReader sr = new StreamReader(fs, Encoding.Default);
                    string file = Convert.ToString(sr.ReadToEnd());  //读取文件
                    fs.Close();
                    sr.Close();
                    char[] separator = { '\n' };  //换行符为分隔符
                    string[] line = file.Split(separator, StringSplitOptions.RemoveEmptyEntries);  //按指定字符分隔字符串
                    lineCount = Convert.ToString(line.Length);
                    Console.WriteLine("行数:" + lineCount);
                    l += lineCount;
                    return l;
                }
                catch
                {
                    Console.WriteLine("打开文件失败");
                    l += lineCount;
                    return l;
                }
            }

 开始想到用分号分隔,然后发现在很多情况下不适用,于是以换行符为分隔符,分隔后的字符串长度为行数。

 

  写入统计结果:

public static void Print(string result)
            {
                try
                {
                    //写入数据到指定文件
                    StreamWriter sw = new StreamWriter("result.txt",true);
                    sw.WriteLine(result);
                    Console.WriteLine("数据写入成功");
                    sw.Close();
                }
                catch
                {
                    Console.WriteLine("数据写入失败");
                }
            }

 

四、软件测试

测试文件

 

 

 

 

 

五、 总结

       由于时间原因及技术问题,这次作业写得很简陋,程序还不能同时执行多条命令。第一次接触命令行程序,开始并没有想到直接打开程序目录里的exe文件,而是通过运行菜单的cmd窗口运行程序,最后在网上查找了很久也没来得及修改。这一次作业也让我清楚的认识到自己和优秀同学间的差距,会加倍的努力学习,作业后续也会逐步改进。

 

posted on 2018-09-30 21:38  小象404  阅读(81)  评论(1编辑  收藏  举报