第三次作业WordCount拓展

(1)合作者:201631062323徐建敏        201631062223 林布尔

(2)代码地址:https://gitee.com/xjm861710023/wordcount_development

(3)本次作业链接地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187

一、拓展代码

(1)主函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wc
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "";    //输入字符串
            WC wordcount = new WC(); //引用WC类
            Char[] c = {' '};//分隔符
            while (msg != "exit")
            {
                msg = Console.ReadLine();    //输入操作字符串
                string[] msgSplit = msg.Split(c, StringSplitOptions.RemoveEmptyEntries);//将字符串按空格分割
                wordcount.Operator(msgSplit);
            }
        }
    }
}

(2)WC类

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

namespace wc
{
    public class WC
    {
        public string filename;    // 文件名
        public string[] filenames;   //多个文件名
        public string stopwordfile;    //停用词文件名
        public string[] msgSplit; //所有参数数组
        public string outfile;   //输出文件名
        public string str;       //输出字符串
        public int iccount;      // 字符数
        public int iwcount;      // 单词数
        public int ilcount;      // 总行数
        public int inulllinecount;  //空行数
        public int icodelinecount;  //代码行数
        public int inotelinecount;  //注释行数
        //引入输入字符串,并对字符串进行处理
        public void Operator(string[] msgSplit)
        {
            this.msgSplit = msgSplit;    //引入操作数组
            int Length;
            Length = msgSplit.Length;
            foreach (string s in msgSplit)
            {
                if (s == "-e")
                {
                    Length = Length - 2;
                }
                else if (s == "-o")
                {
                    Length = Length - 2;
                }
            }
            this.filename = msgSplit[Length - 1];//获取文件名
            if (msgSplit[0] == "wc.exe")
            {
                foreach (string s in msgSplit)
                {
                    if (s == "-s")
                    {
                        //获取所有后缀名符合的文件
                        string fileDir = Environment.CurrentDirectory;
                        filenames = Directory.GetFiles(fileDir, filename);
                    }
                    else if (s == "-e")
                    {
                        //获取停用词表文件
                        for (int i = 0; i < msgSplit.Length; i++)
                        {
                            if (msgSplit[i] == "-e")
                            {
                                stopwordfile = msgSplit[i + 1];
                            }
                        }
                    }
                }
                if (filenames == null)
                {
                    SuperCount(filename);
                    BaseCount(filename, stopwordfile);
                    Display(filename);
                }
                else
                {
                    for (int i = 0; i < filenames.Length; i++)
                    {
                        SuperCount(filenames[i]);
                        BaseCount(filenames[i], stopwordfile);
                        Display(filenames[i]);
                    }
                }
            }
            else
            {
                Console.WriteLine("输入指令不存在!请重新输入。。。");
            }
        }
        // 统计基本信息:字符数 单词数 行数
        public void BaseCount(string filename,string stopwordfile)
        {
            try
            {
                // 打开文件
                FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                StreamReader sr = new StreamReader(file);
                int nChar;
                int charcount = 0;
                int wordcount = 0;
                int linecount = 0;
                //定义一个字符数组
                char[] symbol = { ' ', '\t','\r', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '<','>','+' ,'-','*', '='};
                while ((nChar = sr.Read()) != -1)
                {
                    charcount++;     // 统计字符数
                    if (nChar == '\n')
                    {
                        linecount++; // 统计行数
                    }
                }
                sr.Close();
                file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                sr = new StreamReader(file);
                string words = sr.ReadToEnd();
                string[] word = words.Split(symbol, StringSplitOptions.RemoveEmptyEntries);//将文件内所有单词分割
                if (stopwordfile != null)
                {
                    //有停用词表
                    FileStream stopfile = new FileStream(stopwordfile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    StreamReader stopsr = new StreamReader(stopfile);
                    string stopwords = stopsr.ReadToEnd();
                    string[] stopword = stopwords.Split(symbol, StringSplitOptions.RemoveEmptyEntries);//将停用词表内所有单词分割
                    foreach (string s in word)
                    {
                        foreach (string stop in stopword)
                        {
                            if (s != stop)
                            {
                                wordcount++;//统计单词数
                            }
                        }
                    }
                    stopsr.Close();
                }
                else
                {
                    //没有停用词表
                    wordcount = word.Length;
                }
                sr.Close();
                iccount = charcount;
                iwcount = wordcount;
                ilcount = linecount + 1;
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }
        // 统计代码行 空行 注释行 
        public void SuperCount(string filename)
        {
            try
            {
                // 打开文件
                FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                StreamReader sr = new StreamReader(file);
                String line;
                int nulllinecount = 0;
                int codelinecount = 0;
                int notelinecount = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    //   除去每行开头多余空格和格式控制字符
                    line = line.Trim(' ');
                    line = line.Trim('\t');
                    //   空行
                    if (line == "" || line.Length <= 1)
                    {
                        nulllinecount++;
                    }
                    //   注释行
                    else if (line.Substring(0, 2) == "//" || line.Substring(1, 2) == "//")
                    {
                        notelinecount++;
                    }
                    // 代码行
                    else
                    {
                        codelinecount++;
                    }
                }
                inulllinecount = nulllinecount;
                icodelinecount = codelinecount;
                inotelinecount = notelinecount;
                sr.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }
        // 打印信息
        public void Display(string filename)
        {
            //将操作命令以-c,-w,-l的顺序输出
            foreach (string s in msgSplit)
            {
                if (s == "-c")              //遍历第一次找-c命令,有的话输出字符数
                {
                    Console.WriteLine(filename + ",字符数:{0}", iccount);
                    str = filename + ",字 符 数:" + iccount + "\r\n";
                    File.AppendAllText("result.txt", str, Encoding.Default);
                }
            }
            foreach (string s in msgSplit)
            {
                if (s == "-w")             //遍历第二次找-w命令,有的话输出单词数
                {
                    Console.WriteLine(filename + ",单词数:{0}", iwcount);
                    str = filename + ",单 词 数:" + iwcount + "\r\n";
                    File.AppendAllText("result.txt", str, Encoding.Default);
                }
            }
            foreach (string s in msgSplit)
            {
                if (s == "-l")              //遍历第三次找-l命令,有的话输出行数
                {
                    Console.WriteLine(filename + ",行 数:{0}", ilcount);
                    str = filename + ",总 行 数:" + ilcount + "\r\n";
                    File.AppendAllText("result.txt", str, Encoding.Default);
                }
            }
            foreach (string s in msgSplit)
            {
                if (s == "-a")            //遍历第四次找-a命令,有的话输出代码行/空行/注释行
                {
                    Console.WriteLine(filename + ",代码行/空行/注释行:{0}/{1}/{2}", icodelinecount, inulllinecount, inotelinecount);
                    str = filename + ",代码行/空行/注释行:" + icodelinecount + '/' + inulllinecount + '/' + inotelinecount + "\r\n";
                    File.AppendAllText("result.txt", str, Encoding.Default);
                }
            }
            foreach (string s in msgSplit)
            {
                if (s == "-o")               //遍历第四次找-o命令,有的话将结果存入输出文件
                {
                    this.outfile = msgSplit[msgSplit.Length - 1];   //引入输出文件名
                    foreach (string st in msgSplit)
                    {
                        if (st == "-c")          //遍历第一次找-c命令,有的话将字符数存入输出文档
                        {
                            str = filename + ",字 符 数:" + iccount + "\r\n";
                            File.AppendAllText("" + outfile + "", str, Encoding.Default);
                        }
                    }
                    foreach (string st in msgSplit)
                    {
                        if (st == "-w")           //遍历第二次找-w命令,有的话将单词数存入输出文档
                        {
                            str = filename + ",单 词 数:" + iwcount + "\r\n";
                            File.AppendAllText("" + outfile + "", str, Encoding.Default);
                        }
                    }
                    foreach (string st in msgSplit)
                    {
                        if (st == "-l")            //遍历第三次找-l命令,有的话将行数存入输出文档
                        {
                            str = filename + ",总 行 数:" + ilcount + "\r\n";
                            File.AppendAllText("" + outfile + "", str, Encoding.Default);
                        }
                    }
                    foreach (string st in msgSplit)
                    {
                        if (st == "-a")          //遍历第四次找-a命令,有的话将代码行/空行/注释行存入输出文档
                        {
                            str = filename + ",代码行/空行/注释行:" + icodelinecount + '/' + inulllinecount + '/' + inotelinecount + "\r\n";
                            File.AppendAllText("" + outfile + "", str, Encoding.Default);
                        }
                    }
                }
            }
            Console.WriteLine();
        }
    }
}

(3)单元测试代码

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace TestWC
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestBaseCount()
        {
            //测试统计字符数,单词书,行数
            wc.WC wordcount = new wc.WC();
            string filename = "F://徐建敏//wc//wc//bin//Debug//file1.c";
            string stopwordfile = "F://徐建敏//wc//wc//bin//Debug//stop.txt";
            wordcount.BaseCount(filename, stopwordfile);
            int ccount = wordcount.iccount;
            int wcount = wordcount.iwcount;
            int lcount = wordcount.ilcount;
            Assert.AreEqual(87, ccount);
            Assert.AreEqual(10, wcount);
            Assert.AreEqual(7, lcount);
        }
        [TestMethod]
        public void TestOperator()
        {
            //测试对输入字符串进行引入并处理
            wc.WC wordcount = new wc.WC();
            string[] msgSplit = { "wc.exe", "-c", "-w", "-l", "F://徐建敏//wc//wc//bin//Debug//file1.c"};
            wordcount.Operator(msgSplit);
            string filename = wordcount.filename;
            string stopword = wordcount.stopwordfile;
            string[] filenames = wordcount.filenames;
            Assert.AreEqual("F://徐建敏//wc//wc//bin//Debug//file1.c", filename);
            Assert.AreEqual(null, stopword);
            Assert.AreEqual(null, filenames);
        }
        [TestMethod]
        public void TestSuperCount()
        {
            //测试统计代码行 空行 注释行
            wc.WC wordcount = new wc.WC();
            string filename = "F://徐建敏//wc//wc//bin//Debug//file1.c";
            wordcount.SuperCount(filename);
            int codecount = wordcount.icodelinecount;
            int nullcount = wordcount.inulllinecount;
            int notecount = wordcount.inotelinecount;
            Assert.AreEqual(4, codecount);
            Assert.AreEqual(2, nullcount);
            Assert.AreEqual(1, notecount);
        }
        [TestMethod]
        public void TestDisplay()
        {
            //测试输出
            wc.WC wordcount = new wc.WC();
            string[] msg={"wc.exe","-c","F://徐建敏//wc//wc//bin//Debug//file1.c"};
            wordcount.msgSplit = msg;
            string filename = "F://徐建敏//wc//wc//bin//Debug//file1.c";
            wordcount.Display(filename);
            string result = wordcount.str;
            Assert.AreEqual("F://徐建敏//wc//wc//bin//Debug//file1.c,字 符 数:0\r\n", result);
        }
    }
}

二、拓展功能展示

第三次作业WordCount拓展_第1张图片

第三次作业WordCount拓展_第2张图片

 

 

三、静态代码检查

第三次作业WordCount拓展_第3张图片

四、单元测试

第三次作业WordCount拓展_第4张图片

五、个人总结

通过这次作业,学会了使用VS自带的单元测试工具。同时,结队编程互相讨论,互相提供灵感,对编程的帮助很大。但是实在没有找到静态代码测试工具,只能看VS自带的错误提示,没有学习到静态代码工具的使用,有一些遗憾。

你可能感兴趣的:(第三次作业WordCount拓展)