软件测试第二周个人作业wordcount程序实现(代码片段)

Guchen Guchen     2022-10-27     387

关键词:

GitHub地址:https://github.com/Guchencc/WordCount

一.PSP表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 

 

· Estimate

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

 300

440

Development

开发

 

 

· Analysis

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

 20

 30

· Design Spec

· 生成设计文档

 10

 10

· Design Review

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

 10

 10

· Coding Standard

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

 10

 10

· Design

· 具体设计

 20

 30

· Coding

· 具体编码

 120

 240

· Code Review

· 代码复审

 10

 30

· Test

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

 20

 30

Reporting

报告

 20

 20

· Test Report

· 测试报告

 10

 10

· Size Measurement

· 计算工作量

 10

 10

· Postmortem & Process Improvement Plan

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

 10

 10

 

合计

 270

440

 

 

二、解题思路

  了解完题目后,首先回顾了java与文件读写以及目录文件查询等相关函数。改程序是输入命令行然后进行相关操作的,那么首先就需要解析作为主函数参数传入的命令行参数,解析过程中需要区分文件path和命令以及登记相关变量的值,解析完后对变量filelist中的文件进行计数,大体思路如此。

 

 

 

 

三、程序设计实现过程

  本程序实现只用了一个主类WordCount类,其属性如下:

  private String filename; //当前计数处理的文件名

  private int charcount=0;//字符计数器

  private int wordcount=0;//单词计数器

  private int linecount=0;//行计数器

  private int codeLinecount=0;//代码行计数器

  private int blankLinecount=0;//空行计数器

  private int commentLinecount=0;//注释行计数器

  private ArrayList<String> optlist=new ArrayList<>();//存储输入的命令

  private ArrayList<String> stoplist=new ArrayList<>();//存储读取的停用词

  private ArrayList<String> filelist=new ArrayList<>();//存储输入的计数文件

  private String suffix;//存储输入的通配符后缀

  private String outFileName;//存储输入的输出文件名

  private String stopFileName;////存储当前处理的的停用词文件名

 

 

 

  相关函数如下:

  main()为程序主函数。

  CommandParser(String[ ] args)函数接受来自主函数的args数组,解析命令行参数并给WordCount类中的相关属性赋值。

  Count()函数根据解析后的属性赋值进行计数操作,它会计算所有filelist动态数组中的文件,并根据oplist动态数组中的命令选择输出项。

  outprint()函数根据oplist动态数组中的命令选择数据进行文本输出

  resetcount()函数重置各类计数器

  readStopFile()读取输入的停用词文件中的停用词并添加进stoplist动态数组

  findAllFiles()函数递归处理输入的文件路径(包含*.XXX)获取所有以XXX为文件格式的文件路径,并添加进filelist动态数组。

 

四、代码说明

  

public void CommandParser(String[] args)  //命令行参数解析,
    for(int i=0;i<args.length;i++)
        args[i]=args[i].toLowerCase();
        if (args[i].equals("-c")||args[i].equals("-l")||args[i].equals("-w")||args[i].equals("-a")||args[i].equals("-s")) //将计数命令直接添加进oplist
            optlist.add(args[i]);
        else if (args[i].equals("-e"))   //如果输入的命令包含-e 则后面必定是停用词文件路径
            i++;
            if (args.length>i) 
                if (args[i].equals("-o") || args[i].equals("\\n")) 
                    System.out.println("未输入停用单词表文件名!");
                    return;
                
            else  
                System.out.println("未输入停用单词表文件名!");
                return;
            
            stopFileName=args[i];
            optlist.add(args[--i]);
            readStopFile();
        
        else if (args[i].equals("-o"))  //如果输入的命令包含-o 则后面必定是输出文件路径
            if(++i==args.length) 
                System.out.println("未输入输出文件名!");
                return;
            
            outFileName=args[i];
            optlist.add(args[--i]);
        
        else if (args[i].equals(outFileName)||args[i].equals(stopFileName));
        else if (optlist.contains("-s") && args[i].matches(".*\\\\*[.](txt|c|py|java|cpp)$"))    //根据正则表达式匹配输入的含有通配符的路径
            String root="";
            suffix=args[i].substring(args[i].lastIndexOf(\'.\')+1,args[i].length());  //获取想要获取的文件后缀名
            root=args[i].replaceAll("\\\\*[.](txt|c|py|java|cpp)$","");   //获取想要查找含有通配符格式的文件根目录
            if (root.length()<1)        //如果没有指定目录则从当前目录开始查找
                root=System.getProperty("user.dir");
            findAllFiles(root);      //查找匹配通配符格式的所有文件
        
        else if (!optlist.contains("-s"))   //如果输入的命令行中无-s  则输入文件名不包含通配符,而是具体的文件路径,直接添加进filelist中
            filelist.add(args[i]);
    

  

  

 


public void Count() throws Exception 
    String str="";
    boolean isstop=false;
    for (int i=0;i<filelist.size();i++)  //遍历待统计文件数组,并根据相关名词定义进行计数
        String path=filelist.get(i);
        filename=path.substring(path.lastIndexOf(\'\\\\\')+1,path.length());
        BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(filelist.get(i))));
        while((str=br.readLine())!=null) 
            if (str.trim().replaceAll("(//.*|/\\\\*.*\\\\*/|/\\\\*.*|\\\\*/|\\\\|\\\\)","").length()>1)
                codeLinecount++;
            if (str.trim().matches("^(//|/\\\\*).*") || str.trim().matches("^[!-~](//.*|/\\\\*.*\\\\*/)") || str.trim().matches("\\\\*/"))
                commentLinecount++;
            
            if(str.matches("\\\\s*") || (str.trim().length()==1 && (str.trim().charAt(0)>0x21 && str.trim().charAt(0)<0x7F)))
                blankLinecount++;

            linecount++;
            charcount+=str.length();
            String[] words=str.trim().split("\\\\s|,");
            if (optlist.contains("-e"))    //如果命令包含-e 则遍历单词,若遇到与stoplist中相同的单词则不进行word计数
                for(String word:words) 
                    for (String stopword : stoplist) 
                        if (word.equals(stopword))
                            isstop = true;
                    
                    if (!isstop && !word.equals(""))
                        wordcount++;
                    isstop = false;
                
            
            else 
                for (String word:words)
                    if (!word.equals(""))   //如果命令不包含-e 则直接对单词计数
                        wordcount++;
            
            isstop=false;
        
        charcount=charcount+linecount-1;    // \\r\\t

        if (optlist.contains("-c"))    //根据oplist中的统计命令选择输出结果
            System.out.println(filename+","+"字符数:"+charcount);
        
        if (optlist.contains("-w"))
            System.out.println(filename+","+"单词数:"+wordcount);
        
        if (optlist.contains("-l"))
            System.out.println(filename+","+"行数:"+linecount);
        
        if (optlist.contains("-a"))
            System.out.println(filename+","+"代码行/空行/注释行:"+codeLinecount+"/"+blankLinecount+"/"+commentLinecount);
        
        outprint();     //记录统计结果
        resetCount();   //重置计数器
    

  


public void outprint()
    File file=null;
    String str="";
    if (!optlist.contains("-c")&&!optlist.contains("-w")&&!optlist.contains("-l")&&!optlist.contains("-a"))
        System.out.println("无统计操作,无输出项!");
        return;
    
    if(optlist.contains("-o") && outFileName!=null)
        file = new File(outFileName);       //如果指定输出文件路径,则在相应路径输出统计结果
    else
        file = new File("result.txt");      //否则在当前路径下的result.txt文件中输出。
    try
        FileWriter fw=new FileWriter(file,true);
        PrintWriter pw=new PrintWriter(fw);
        if(!file.exists())
            file.createNewFile();
        
        if (optlist.contains("-c"))         //根据oplist中的命令选择记录统计结果
            str+=filename+","+"字符数:"+charcount+"\\r\\n";
        if (optlist.contains("-w"))
            str+=filename+","+"单词数:"+wordcount+"\\r\\n";
        if (optlist.contains("-l"))
            str+=filename+","+"行数:"+linecount+"\\r\\n";
        if (optlist.contains("-a"))
            str+=filename+","+"代码行/空行/注释行:"+codeLinecount+"/"+blankLinecount+"/"+commentLinecount+"\\r\\n";
        pw.write(str);
        pw.close();
        fw.close();
    catch (Exception e)
        System.out.println("输出文件失败!");
    

  

  


public void readStopFile()
    String str="";
    String[] stopwords;
    try 
        BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(stopFileName)));
        while ((str = bf.readLine()) != null)      //遍历输入的停用词文件并登记停用词
            stopwords = str.trim().split("\\\\s");
            Collections.addAll(stoplist, stopwords);
        
    catch (Exception e)
        System.out.println("读取停用词表错误!");
    

  

public void findAllFiles(String path)
    File file=new File(path);
    if (!file.isDirectory()) 
        String filename = file.getName();
        if (filename.substring(filename.lastIndexOf(\'.\') + 1, filename.length()).equals(suffix))   //将文件后缀与提取的通配符后缀比较,若相同则将该文件路径加入filelist
            filelist.add(file.getAbsolutePath());

     else if (file.isDirectory())
        for (File f:file.listFiles())
            findAllFiles(f.getAbsolutePath());      //递归遍历文件夹与文件
    

  

  

五、测试设计过程

主要测试文件 test.txt

 

 

(1)基本功能

①统计字符 -c

输入:wc.exe -c test.c

 

②统计单词数 -w

输入:wc.exe -w test.c

③统计行数 -l

输入:wc.exe -l test.c

④输出文件 -o

输入:wc.exe -c -w -l test.c -o output.txt

 

(2)扩展功能

①统计代码行、空行、注释行 -a

输入:wc.exe -a test.c

 

②停用词表 -s

停用词文件stoplist.txt

 

输入:wc.exe -w test.c

 

 输入:wc.exe -w test.c -e stoplist.txt

 

③文件夹遍历处理  -s

 

输入:wc.exe -s -c -w -l -a *.txt

 

输入:wc.exe -s -c -w -l -a *.c

 

 

 输入:wc.exe -s -c C:\\Users\\Guchen\\gitrepository\\untitled1\\bin\\jre1.8.0_111\\*.txt

 

 

 

 

六、参考文献链接

https://baike.baidu.com/item/正则表达式/1700215?fr=aladdin

http://www.cnblogs.com/ningjing-zhiyuan/p/8563562.html

http://blog.csdn.net/zamamiro/article/details/70172900

http://blog.csdn.net/honjane/article/details/40739337

 

第二周个人作业wordcount(代码片段)

一、GitHub地址https://github.com/JasonLiu1105/WordCount二、PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 20 20·Estimate·估计这个任务需要多少时间 2days 3daysDevelopment开发 1day 1day·Analysis·需求 查看详情

软件测试第二周作业wordcount(代码片段)

 软件测试第二周作业wordcountGithub地址https://github.com/mxz96102/word_countPSP2.1表格PSP2.1PSP 阶段预估耗时 (分钟)实际耗时 (分钟)Planning计划2530·Estimate·估计这个任务需要多少时间150252Development开发  ·Analysis· 查看详情

软件质量与测试第二周作业wordcount(代码片段)

第二周作业WordCount一、Github地址https://github.com/llag9810/Software-Quality-and-Testing-Wordcount二、PSP2.1 表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 60 25·Estimate·估计这个任务需要多少时间 30 15Dev 查看详情

软件测试第二周作业wordcounter(代码片段)

Github项目地址WordCounteringithubPSP(PersonalSoftwareProcess)PSP2.1PSP阶段预估耗时实际耗时(分钟)实际耗时(分钟)Planning计划1017Estimate估计这个任务需要多少时间510Development开发545650-Analysis-需求分析(包括学习新技术)120160-DesignSpec-生... 查看详情

软件质量测试第二周wordcount作业(代码片段)

一.github地址https://github.com/WKX121/WC二.PSPPSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 25 25·Estimate·估计这个任务需要多少时间 25  25Development开发300 340·Analysis·需求分析(包括学习新技术)&... 查看详情

第二周作业wordcount(代码片段)

github项目链接https://github.com/liqia/WordCount1.项目简介对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。可执行程序命名为:wc.exe,该程序... 查看详情

wordcount第二周作业

GitHub地址GitHub地址为:https://github.com/Lovegoodstudy/WordCountPSP表格 PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划2020·Estimate·估计这个任务需要多少时间2020 Development开发1100 ·Analysis·需求分析(包括学习新技术)... 查看详情

第二周作业wordcount(代码片段)

https://github.com/HuangDongPeng/WordCount.git1.1 PSPPSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 30·Estimate·估计这个任务需要多少时间 5h 12hDevelopment开发 2h 4h·Analysis·需求分析(包括学习新技 查看详情

软件测试第二周作业(代码片段)

一,github地址https://github.com/lc-xie/WordCount二,PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 25 25·Estimate·估计这个任务需要多少时间 25  25Development开发200 240·Analysis·需求分析(包括学习新... 查看详情

第二周作业一读软件工程

谢谢邹老师给我们带来这么精彩的书,话不多说,强行提问:1.不面向用户的软件,程序员自娱自乐的项目,如何选择开发模式。2.结对编程后,由此可能造成的损失和不了后果由两人承担,具体比例怎么算。3.像微软office办公软... 查看详情

第二周作业三效能测试

效能分析:之前从来没有用工具测试过程序的性能,以前只对程序的性能在理论上分析过,什么时间复杂度,空间复杂度之类的。只对影响程序性能有很粗浅的认识。认为多重循环层数多了性能低,递归算法性能低。今天我实际... 查看详情

软件工程第二周作业

##软件工程第二周作业提出问题1.一般来说,想要自己的程序跑得又快又好,就要减少函数的反复调用,但有所得则必有所失,效能提高就有可能伴随着程序的稳定性的降低,这两者应该如何权衡呢?2.关于5.3.5老板驱动的流程,... 查看详情

第二周个人作业(代码片段)

 1.GitHub地址:https://github.com/YJG1997/wc2.psp表格PSP2.1表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 60 50·Estimate·估计这个任务需要多少时间 30 20Development开发 60 90·Analysis·需求分析(包括学... 查看详情

《构建之法》-第二周

第二章的主要内容是个人技术和流程,由单元测试和个人开发流程组成。 单元测试主要通过举了例子阐述了在多人合作的软件开发中,如何能让自己负责的模块功能定义尽量明确,模块内部的改变不会影响其他模块,而且模... 查看详情

《实时控制软件设计》第二周作业

 状态分析由题目描述,只有两种不同事件,一个是车辆进入,一个是车辆离开,与之对应的传感器信号,起落杆信号,通行灯信号都是一同变化,因此可以简化成两种复合状态。 代码实现 传感器头文件////Createdbyzhuha... 查看详情

第二周作业二每周例行报告

项目类型:个人项目项目完成情况:已完成项目改进:增加需求项目日期:2016.9.12-2016.9.1312号13号项目:四则运算出题程序项目类型:结对项目项目完成情况:已完成项目改进:增加需求项目日期:2016.9.14----2016.9.1514号15号 &n... 查看详情

hust软工1506班第2周作业成绩公布

说明本次公布的成绩对应的作业为:第2周个人作业:WordCount编码和测试 如果同学对作业成绩存在异议,在成绩公布的72小时内(截止日期4月26日0点)可以进行申诉,方式如下:毕博平台的第二周在线答疑区发申诉帖。评分... 查看详情

第二周作业-词频统计

本周作业是词频统计,编程具体要求如下:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/922对实现功能进行简要的介绍:功能一:小文件输入,为表明程序能跑。需要在控制台下输入命令,得到文件中不重复的总单词数。并对单词... 查看详情