第三次作业

一、项目地址

  我的项目地址:https://github.com/JPL1988/WordCount.git

  结对伙伴地址:https://www.cnblogs.com/l123456l/p/10628055.html

二、结对过程

    这次作业是和老龙一起合作的,他对于项目的整体布局和细节提出了很好的建议,虽然项目比较简陋,但是都付出了很大的努力。

 

三、

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 10 10

· Estimate

· 估计这个任务需要多少时间

   

Development

开发

 150  180

· Analysis

· 需求分析 (包括学习新技术)

 60  80

· Design Spec

· 生成设计文档

 40  30

· Design Review

· 设计复审 (和同事审核设计文档)

 30  15

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 30  20

· Design

· 具体设计

 30  20

· Coding

· 具体编码

 40  35

· Code Review

· 代码复审

 30  30

· Test

· 测试(自我测试,修改代码,提交修改)

 30  20

Reporting

报告

 20  20

· Test Report

· 测试报告

 20  15

· Size Measurement

· 计算工作量

 30  20

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 20  15
 

合计

 540  510

四、解题思路  

    1,通过命令行启动程序,识别文件

    2,把文件全部读取进一个字符串,通过字符串长度求文件字符数

    3,遍历字符串,同时统计单词总数

    4,将字符串分割为单个字符串,用dictionary储存字符串和字符串出现次数

    5,遍历dictionary一次找到一个出现频率最高的字符串,然后找到10个字符串

    6,通过比较字符大小进行字典排序

五、设计实现过程

    countword类: OutPut方法:调用其它方法并实现输出

           CountLines方法:统计总行数

           SplitString方法:将每个单词分割出来

           ComputeWords方法:所有单词的个数

           CountTimes方法:某个单词出现的次数

           

    Program类:Main方法:传入文本路径并执行程序

六、代码规范链接

   https://blog.csdn.net/qq_31606375/article/details/77783328

七、改进程序性能

八、代码说明

    分离出每个单词

 

public void SplitString(string FileTxt)
        {
            int i = 0;
            int Chars = 0;
            int count = 0;
            string temp = null;
            while (i < FileTxt.Length)
            {
                if (FileTxt[i] == ' ' || (FileTxt[i] > '0' && FileTxt[i] < '9'))
                {
                    while ((FileTxt[i] > '0' && FileTxt[i] < '9'))
                    {
                        i++;
                        Chars++;
                    }
                    if (Chars != 0)
                    {
                        temp = FileTxt.Substring(i - Chars, Chars).ToLower();
                        if (dictionary.ContainsKey(temp))
                        {
                            dictionary.TryGetValue(temp, out count);
                            dictionary.Remove(temp);
                            dictionary.Add(temp, ++count);
                        }
                        else
                        {
                            if (temp.Length >= 4)
                                dictionary.Add(temp, 1);
                        }
                        if (FileTxt[i] == ' ')
                            Chars = 0;
                        else
                            Chars = 1;
                    }
                }
                else
                {
                    Chars++;
                }
                i++;
            }
        }

 

    统计行数

public int CountLines(String filepath)
        {
            Stopwatch sw = new Stopwatch();
            int lines = 0;
            //按行读取
            sw.Restart();
            using (var sr = new StreamReader(filepath))
            {
                string ls = "";
                //循环读取直到最后一行
                while ((ls = sr.ReadLine()) != null)
                {
                    //若是换行符行数不变
                    if (ls.Length != 0)
                        lines++;
                }
            }
            sw.Stop();
            return lines;
        }

    统计字符总数

public int ComputeWords(string FileTxt)
        {
            int count = 0;
            int result = 0;
            int i = 0;
            //遍历整个文件字符串
            while (i<FileTxt.Length)
            {
                if(FileTxt[i] == '\n')
                {
                    FileTxt.Remove(i,1);
                }
                //若不是文件分隔符,则单词长度加1,否则判断前字符串是否是单词
                if(FileTxt[i]==' '||(FileTxt[i]>'0'&&FileTxt[i]<'9'))
                {
                    while ((FileTxt[i] > '0' && FileTxt[i] < '9'))
                    {
                        i++;
                        count++;
                    }
                    if (count >= 4)
                    {
                        result++;
                    }
                    if (FileTxt[i] == ' ')
                        count = 0;
                    else
                        count = 1;
                }
                else
                {
                    count++;
                }
                i++;
            }
            return result;
        }

 

 

九、心路历程与收获

  通过这次结对编程学会了讨论分析并改进程序,感受到了合作的好处。

 

posted @ 2019-04-03 22:01  Faith_信仰  阅读(187)  评论(1编辑  收藏  举报