WordCount(结对)

合作者:201631062314,201631062214

码云地址:https://gitee.com/dsjyun/Word-Count-three

一、代码互审:

    第一次都是实现了基本功能,没有完成扩展功能,这次还有个高级功能,于是讨论了后续功能如何实现。

我们的意见基本一致,认为高级功能需要窗体来实现比较简单,于是决定用C#语言来实现。

二、部分代码

 

using System;
using System.Diagnostics;



namespace WordCount
{
    class program
    {
        static void Main(string[] args)
        {
            Console.Write("wc.exe -c file.c\t返回文件 file.c 的字符数\n" +
                          "wc.exe -w file.c\t返回文件 file.c 的单词总数\n" +
                          "wc.exe -l file.c\t返回文件 file.c 的总行数\n" +
                          "wc.exe -a file.c\t返回更复杂的数据(代码行/空行/注释行)\n" +
                          "wc.exe -o output.txt\t将结果输出到指定文件output.txt\n" +
                          "wc.exe -e stopList.txt\t停用词表,统计文件单词总数时,不统计该表中的单词\n" +
                          "wc.exe -s\t循环执行所有.c文件\n");
            Wordcount wc = new Wordcount();
            while (true)
            {
                Console.WriteLine("--------------------------");
                Console.WriteLine("输入命令:");


                string str = Console.ReadLine();


                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start(); // 开始监视代码运行时间

                

                wc.ExecutiveCommand(str);

                stopwatch.Stop(); // 停止监视
                TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
                string hours = timespan.TotalHours.ToString("#0.00000000 "); // 总小时
                string minutes = timespan.TotalMinutes.ToString("#0.00000000 "); // 总分钟
                string seconds = timespan.TotalSeconds.ToString("#0.00000000 "); // 总秒数
                string milliseconds = timespan.TotalMilliseconds.ToString("#0.00000000 "); // 总毫秒数
                Console.Write("运行时间  "+timespan);

            }
        }

    }
}
主函数,性能测试

 

using System;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace WordCount
{
    class Wordcount
    {
        int charNum = 0;
        int wordNum = 0;
        int lineNum = 0;
        string strResult = "";




        string[] stopWord;
        public void ExecutiveCommand(string strCommand)
        {
            string[] strOperate = strCommand.Split(' ');
            string fileName = "file.c";//默认操作文件

            //获取.c操作文件
            for (int i = 0; i < strOperate.Length; ++i)
            {
                if (strOperate[i] != "-c" && strOperate[i] != "-w" &&
                    strOperate[i] != "-l" && strOperate[i] != "-a" &&
                    strOperate[i] != "-o" && strOperate[i] != "-e" &&
                    strOperate[i] != "-s" && System.IO.Path.GetExtension(strOperate[i]).Equals(".c"))
                {
                    fileName = strOperate[i];
                }
            }
            //循环操作
            for (int i = 0; i < strOperate.Length; ++i)
            {
                if (strOperate[i].Equals("-c"))
                {
                    charNum = GetCharNum(fileName);
                    SaveResult(fileName + ",字符数:" + charNum);
                }
                else if (strOperate[i].Equals("-w"))
                {
                    wordNum = GetWordNum(fileName);
                    SaveResult(fileName + ",单词数:" + wordNum);
                }
                else if (strOperate[i].Equals("-l"))
                {
                    lineNum = GetRowNum(fileName);
                    SaveResult(fileName + ",行数:" + lineNum);
                }
                else if (strOperate[i].Equals("-a"))
                {
                    SaveResult(fileName + ",代码行数:" + GetCodeRowNum(fileName));
                    SaveResult(fileName + ",空白行数:" + GetBlankRowNum(fileName));
                    SaveResult(fileName + ",注释行数:" + GetNoteRowNum(fileName));
                }
                else if (strOperate[i].Equals("-o"))
                {
                    OutputResult(strOperate[i + 1]);
                }
                else if (strOperate[i].Equals("-e"))
                {
                    GetStopList(strOperate[i + 1]);
                }
                else if (strOperate[i].Equals("-s"))
                {
                    ExeAllFile();
                }
            }
        }

        //获取字符数
        public int GetCharNum(string fileName)
        {
            // 打开文件 
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            // 读取文件的 byte[] 
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            string strfile = Encoding.UTF8.GetString(bytes);// 字节向字符串转化
            return strfile.Length - GetRowNum(fileName) + 1;//换行符占两个字符
        }
        //获取单词数
        public int GetWordNum(string fileName)
        {
            // 打开文件 
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            // 读取文件的 byte[] 
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            string strfile = Encoding.UTF8.GetString(bytes);// 字节向字符串转化
            int wNum = 0;
            string strword = "";
            strfile += '\n';//在最后添加换行符
            for (int i = 1; i < strfile.Length; ++i)
            {
                if (strfile[i].Equals(' ') || strfile[i].Equals(',') || strfile[i].Equals('\n'))
                {
                    if (strfile[i - 1].Equals(' ') == false && strfile[i - 1].Equals(',') == false && strfile[i - 1].Equals('\n') == false)
                    {
                        ++wNum;
                        //停用词
                        if (stopWord != null)
                        {
                            strword = strword.ToLower();
                            for (int j = 0; j < stopWord.Length; ++j)
                            {
                                if (strword == stopWord[j])
                                {
                                    --wNum;
                                }
                            }
                        }
                        strword = "";
                    }
                }
                else
                {
                    strword += strfile[i];//记录单词
                }
            }
            return wNum;
        }
        //获取行数
        public int GetRowNum(string fileName)
        {
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            int rNum = 0;
            while (sr.ReadLine() != null)
            {
                ++rNum;
            }
            return rNum;
        }
        //获取代码行/空行/注释行
        public int GetCodeRowNum(string fileName)
        {
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string str;
            int codeNum = 0;
            int noteNum = 0;
            int blankNum = 0;
            while ((str = sr.ReadLine()) != null)
            {
                noteNum = 0;
                blankNum = 0;
                for (int i = 0; i < str.Length; ++i)
                {
                    if (i < str.Length - 1 && str[i].Equals('/') && str[i + 1].Equals('/'))
                    {//注释点
                        noteNum = i;
                        break;
                    }
                    if (str[i].Equals(' ') || str[i].Equals('\t') || str[i].Equals('{') || str[i].Equals('}'))
                    {//空白数
                        ++blankNum;
                    }
                }
                if (noteNum != 0 && noteNum > blankNum)
                {
                    ++codeNum;
                }
                else if (noteNum == 0 && blankNum < str.Length)
                {
                    ++codeNum;
                }
            }
            return codeNum;
        }
        public int GetBlankRowNum(string fileName)
        {
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string str;
            int num = 0;
            int noteNum = 0;
            int blankNum = 0;
            while ((str = sr.ReadLine()) != null)
            {
                noteNum = 0;
                blankNum = 0;
                for (int i = 0; i < str.Length; ++i)
                {
                    if (i < str.Length - 1 && str[i].Equals('/') && str[i + 1].Equals('/'))
                    {//注释点
                        noteNum = i;
                        break;
                    }
                    if (str[i].Equals(' ') || str[i].Equals('\t') || str[i].Equals('{') || str[i].Equals('}'))
                    {//空白数
                        ++blankNum;
                    }
                }
                if (noteNum != 0 && noteNum == blankNum)
                {
                    ++num;
                }
                else if (str.Length == 0 || noteNum == 0 && blankNum == str.Length)
                {
                    ++num;
                }
            }

            return num;
        }
        public int GetNoteRowNum(string fileName)
        {
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string str;
            int noteNum = 0;
            while ((str = sr.ReadLine()) != null)
            {
                for (int i = 0; i < str.Length; ++i)
                {
                    if (i < str.Length - 1 && str[i].Equals('/') && str[i + 1].Equals('/'))
                    {//注释点
                        ++noteNum;
                        break;
                    }
                }
            }
            return noteNum;
        }
        //写入结果
        public void OutputResult(string fileName)
        {
            FileStream fileStream = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fileStream);
            //开始写入
            sw.WriteLine(strResult);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fileStream.Close();
        }
        //输出并存结果
        public void SaveResult(string str)
        {
            Console.WriteLine(str);
            strResult += str + "\r\n";
        }
        //获取停用单词
        public void GetStopList(string fileName)
        {
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string strfile = sr.ReadToEnd();
            strfile = strfile.ToLower();
            stopWord = strfile.Split(' ');
            for (int i = 0; i < stopWord.Length; ++i)
            {
                Console.Write(stopWord[i] + "\t");
            }
            Console.WriteLine();
        }
        //循环处理目录下所有.c文件
        public void ExeAllFile()
        {
            string rootPath = Directory.GetCurrentDirectory();//当前路径
            DirectoryInfo sfolder = new DirectoryInfo(rootPath);
            foreach (FileInfo file in sfolder.GetFiles("*.c"))
            {
                SaveResult("\n");
                SaveResult(file.Name + ",字符数:" + GetCharNum(file.Name));
                SaveResult(file.Name + ",单词数:" + GetWordNum(file.Name));
                SaveResult(file.Name + ",总行数:" + GetRowNum(file.Name));
                SaveResult(file.Name + ",代码行数:" + GetCodeRowNum(file.Name));
                SaveResult(file.Name + ",空白行数:" + GetBlankRowNum(file.Name));
                SaveResult(file.Name + ",注释行数:" + GetNoteRowNum(file.Name));
            }
        }
    }
}
主程序

 

 

 

三、基本功能与扩展功能的测试:

1.数字、单词、标点符号测试正常

 

 

 

 

 

2.运算符、空行测试,发现空行算作了一个单词

 

扩展功能,测试正常

四、性能测试

  

通过显示后台运行时间发现单一命令的执行逐渐加快 ,三条命令同时执行的时间也远小于分别执行的时间相加。于是在命令执行的基础上加上文本的输出,发现文本写入用时最多。

 

再经过两次测试发现程序有后台存储功能,记录了运行的信息,第二次运行时读取了运行过的结果,所以时间加快了很多。

五、结论

 本次作业是在上次作业的基础上完成的,不仅实现了拓展功能,更是让我们两个人协作完成,通过这次作业让我学会了如何与他人更好地协作完成任务,为我以后更好地融入工作团队打下了基础

 

 

 

posted @ 2018-10-21 22:50  代士杰  阅读(165)  评论(0编辑  收藏  举报