WordCount(Java实现)

Github项目地址:https://github.com/linzworld/word-count

项目相关要求

题目描述

Word Count

  1. 实现一个简单而完整的软件工具(源程序特征统计程序)。

  2. 进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。

  3. 进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。

WC 项目要求

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

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

    wc.exe [parameter] [file_name]

  3. 基本功能列表:

    wc.exe -c file.c //返回文件 file.c 的字符数(完成)

    wc.exe -w file.c //返回文件 file.c 的词的数目 (完成)

    wc.exe -l file.c //返回文件 file.c 的行数(完成)

  4. 扩展功能:

    -s 递归处理目录下符合条件的文件。(未完成) -a 返回更复杂的数据(代码行 / 空行 / 注释行)。(完成)

    空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。

    代码行:本行包括多于一个字符的代码。

    注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:

    } //注释 在这种情况下,这一行属于注释行。

  5. 高级功能:

-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。

需求举例:   wc.exe -s -a *.c(未完成)

返回当前目录及子目录中所有*.c 文件的代码行数、空行数、注释行数。

工作的完成度统计

列出各类功能下面的详细需求。

基本功能

支持 -c (完成) 支持 -w (完成) 支持 -l (完成)

扩展功能

支持 -s 参数 (未完成) 支持 -a 参数 (完成) 支持各种文件的通配符(*,?)

高级功能

基本的Windows GUI 程序操作 (未完成) 支持通过图形界面选取文件 支持通过图形界面展现文件的信息

 


解题思路以及注意事项

  1. 整个项目分为两个部分,一个是控制台应用,一个是JavaFX图形化页面(未完成),共用同一套处理的功能函数。

  2. 刚开始看到题目的时候,一直都是在脑子里面构想,没有过多地去用代码实际操作;

  3. 做好功能函数的封装

  4. 通配符的匹配

  5. IO流的使用


关键代码or设计说明


//获取文件行数
public static void lineCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  line = 0;
  while(reader.readLine()!=null) {
      line ++;
  }
  reader.close();
}
//获取单词数目,单词只能匹配连续的英文字母
public static void wordCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  word=0;

  String currentLine;
  Pattern pattern = Pattern.compile(RegexConst.WORD_REGEX);
  Matcher matcher;
  while((currentLine=reader.readLine())!=null) {
      currentLine=currentLine.trim();
      matcher= pattern.matcher(currentLine);
      while(matcher.find()) {
          word++;
      }
  }
  reader.close();
}
//获取字符数,这里的字符是包括空白字符的,所以直接使用每次读取的行字符串的长度进行累加
public static void charCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  character=0;
  String currentLine;
  while((currentLine=reader.readLine())!=null) {
      character +=currentLine.length();
  }
  reader.close();
}
//空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
public static void nullLineCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  nullLine =0;
  String currentLine;
  //使用正则表达式
  while((currentLine=reader.readLine())!=null) {
      if(currentLine.matches(RegexConst.nullLineRegex)) {
          nullLine++;
      }
  }
  reader.close();
}


//注释行的统计
public static void commentLineCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  commentLine=0;
  String currentLine;
  while((currentLine=reader.readLine())!=null) {
      if(currentLine.matches(RegexConst.Single_Line_Note_REGEX)) {
          commentLine++;
      }else if(currentLine.matches(RegexConst.Block_Note_Start_REGEX)) {
          commentLine++;
          while((currentLine=reader.readLine())!=null) {
              if(currentLine.matches(RegexConst.Block_Note_End_REGEX)) {
                  commentLine++;
                  break;
              }else {
                  commentLine++;
              }
          }
      }
  }
}

//代码行数
public static void codeLineCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  lineCount(fileName);
  nullLineCount(fileName);
  commentLineCount(fileName);
  codeLine = line - nullLine - commentLine;
}

命令选择

//选择命令,并执行
public static void selectCommand(String command,String fileName) throws IOException
{
    System.out.println("当前文件名为:"+fileName);
    // 实例化workCount
    switch (command) {//判断命令并执行
        case "-c":
            BasicService.charCount(fileName);
            System.out.println("字符数:"+BasicService.character);
            break;
        case "-l":
            BasicService.lineCount(fileName);
            System.out.println("行数:"+BasicService.line);
            break;
        case "-w":
            BasicService.wordCount(fileName);
            System.out.println("单词数:"+BasicService.word);
            break;
        case "-a":
            BasicService.codeLineCount(fileName);
            BasicService.nullLineCount(fileName);
            BasicService.commentLineCount(fileName);
            System.out.println("代码行数:"+BasicService.codeLine);
            System.out.println("空行数:"+BasicService.nullLine);
            System.out.println("注释行数:"+BasicService.commentLine);
            break;
        case "-s":
            //BasicService.getAllFilefileName(fileName);
            break;
        default:
            System.out.println("当前命令格式不存在! "+command);
            break;
    }
}

测试运行

 

 

 

 PSP

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

 

Planning

计划

 60

 70

 

· Estimate

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

 60

 70

 

Development

开发

 620

 1465

 

· Analysis

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

 50

 60

 

· Design Spec

· 生成设计文档

 40

 400

 

· Design Review

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

 30

 40

 

· Coding Standard

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

 10

 15

 

· Design

· 具体设计

 150

 300

 

· Coding

· 具体编码

 240

 500

 

· Code Review

· 代码复审

 40

 50

 

· Test

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

 60

100

 

Reporting

报告

 130

 140

 

· Test Report

· 测试报告

 50

 60

 

· Size Measurement

· 计算工作量

 40

 20

 

· Postmortem & Process Improvement Plan

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

 40

 60

 

合计

 

 910

 1675

 


  • 项目小结

    1. 通过这个java项目,我意识到实际开发和设想中的时间是不一样的,实际开发中存在各种各样的问题,而且我在IO流这方面的知识掌握得不够全面,对于文件的操作还是不太会,导致后面的一个功能没做出来。

    2. 没有做好时间的规划,开发中没有充分集中注意力,导致效率的降低。

    3. 过于精益求精,导致手高眼低的情况的发生。

     

原文地址:https://www.cnblogs.com/linzworld/p/12563547.html