WordCount( Java )

Github项目地址:https://github.com/Sabot1203/WordCount

一. 题目描述

  1. 实现一个简单而完整的软件工具(源程序特征统计程序)。
  2. 进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。
  3. 进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。

二.项目要求

  1. wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

  2. 实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。

  3. 具体功能要求:
    程序处理用户需求的模式为:wc.exe [parameter] [file_name]

三.解题思路

  1. 题目涉及文件的读取解析,选择java程序设计语言主要使用io流部分知识解决较为简单。
  2. 关于java程序转化为exe文件,及命令参数存储在args中相关知识参考网上知识即可解决

四.设计实现及代码说明

将各功能模块封装进WordCount类,再由主函数调用实现相应功能

选用BufferedInputStream和BufferedOutputStream,利用缓冲可以加快效率和速度

实现流程为:

  • 读取文件信息
  • 处理文件信息
  • 将结果写入文件
public class WordCount {
 
    //统计字符数
    public void  CountChars() throws IOException
    {
        ...
    }
    
    //统计总行数
    public void CountLines() throws IOException
    {
        ...
    }
    
    //返统计总词数
    public void CountWords() throws IOException
    {
        ...
    }
    //统计 代码行/空行/注释行 的行数
    public void CountLinesByKind()throws IOException
    {
        ...
    }
    
    //使用停用词文件 统计单词个数
    public void CountWordsWithLimite() throws IOException
    {
        ...
    }
}

关键代码:

public class Main {
  	· · ·
    public static void analyseCommand(String[] args) throws IOException
    {    
	    · · ·
        if(args.length==0)//需要用户输出参数
        {
            System.out.println("Command codes are needed !");
        }
        else
        {
            for(int i=0;i<args.length;i++)
            {
                commands.add(args[i]);
                if(!args[i].matches("^-.*"))//不是命令符号
                {
                    if(args[i].contains("."))//是文件名或目录
                    {
                        if(!new File(args[i]).exists())//文件不存在
                        {
                            System.out.println("The file named "+args[i]+" does not exist");
                            System.exit(0);
                        }
                        else
                        {
                            commandsList.add(commands);
                            commands=new ArrayList<>();
                        }
                    }
                    else//指令有错
                    {
                        System.out.println("The "+(i+1)+"th code("+args[i]+") must begin with '-'");
                        System.exit(0);
                    }
                }
            }
        }
        commandAction(commandsList);
    }
    
    public static void commandAction(List<List<String>> commandList) throws IOException
    {    
        · · ·
        
        for(List<String> commands:commandList)
        {
            if(commands.contains("-o"))
            {
                output=commands.get(commands.size()-1);
            }
            else if(commands.contains("-e"))
            {
                stop=commands.get(commands.size()-1);
            }
            else
            {
                input=commands.get(commands.size()-1);
            }
        }
        
        WordCount wc=new WordCount(input,stop,output);
        
        for(List<String> commands:commandList)
        {
            for(int i=0;i<commands.size()-1;i++)
            {
                switch(commands.get(i))
                {
                    case "-c":wc.CountChars();
                    break;
                    case "-w":wc.CountWords();
                    break;
                    case "-l":wc.CountLines();
                    break;
                    case "-s":wc.CountLinesByKind();
                    break;
                    case "-e":wc.CountWordsWithLimite();
                    break;
                    case "-o":break;
                    default:System.out.println("No such command code");
                }
            }
        }
    }
}
    //代码行/空行/注释行 的行数
    public void CountLinesByKind()throws IOException
    {
        . . .
        while((strLine=input.readLine())!=null)
        {
            all_lines++;
            strLine.replaceAll("
", "");//去除换行符和空格 便于后面操作
            strLine.replaceAll("
", "");
            strLine=strLine.trim();
            strLine.replaceAll(" ", "");
            if(InNoteLines==true)
            {
                note_lines++;
                if(strLine.endsWith("*/")||strLine.endsWith("*/}"))
                {
                    InNoteLines=false;
                }
            }
            else if(strLine.startsWith("/*")||strLine.startsWith("{/*")) //进入注释行
            {
                note_lines++;
                if(!strLine.endsWith("*/")&&!strLine.endsWith("*/}"))//本行未注释结束
                {
                    InNoteLines=true;
                }
            }
            else if(strLine.startsWith("//")||strLine.startsWith("{//"))
            {
                note_lines++;
            }
            else if(strLine.equals("")||strLine.equals("{")||strLine.equals("}"))
            {
                blank_lines++;
            }
        }
        code_lines=all_lines-blank_lines-note_lines;
        ...

    }

五.测试运行

我们建立起一系列测试文件。如下:

  1. 空文件
  2. 只有一个字符的文件
  3. 只有一个词的文件
  4. 只有一行的文件
  5. 一个典型的源文件

在这里插入图片描述

测试结果如下:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

六.项目总结

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 30 15
· Estimate · 估计这个任务需要多少时间 300 320
Development 开发 230 275
· Analysis · 需求分析 (包括学习新技术) 30 15
· Design Spec · 生成设计文档 10 10
· Design Review · 设计复审 (和同事审核设计文档) 10 5
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 10 10
· Design · 具体设计 10 10
· Coding · 具体编码 120 180
· Code Review · 代码复审 10 15
· Test · 测试(自我测试,修改代码,提交修改) 30 20
Reporting 报告 40 30
· Test Report · 测试报告 20 10
· Size Measurement · 计算工作量 10 10
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 10 10
合计 300 320

计划计划时间比实际计划时间短,动手开发时间比计划开发时间长

原文地址:https://www.cnblogs.com/sabot/p/12539343.html