WC.exe(Java实现)

  一、GitHub项目地址:https://github.com/nullcjm/mypage

  二、项目相关要求:

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

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

wc.exe [parameter] [file_name]

基本功能列表:

wc.exe -c file.c     //返回文件 file.c 的字符数

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

wc.exe -l file.c      //返回文件 file.c 的行数

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

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

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

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

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

[file_name]: 文件或目录名,可以处理一般通配符。

高级功能:

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

需求举例:
  wc.exe -s -a *.c

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

  三、PSP表格:

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 30

 30

· Estimate

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

 30

 30

Development

开发

 660

 780

· Analysis

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

 240

 300

· Design Spec

· 生成设计文档

 30

 30

· Design Review

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

 30

 30

· Coding Standard

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

 30

 30

· Design

· 具体设计

 30

 30

· Coding

· 具体编码

 180

240 

· Code Review

· 代码复审

 60

 60

· Test

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

 60

 60

Reporting

报告

 120

 140

· Test Report

· 测试报告

 60

 80

· Size Measurement

· 计算工作量

 30

 30

· Postmortem & Process Improvement Plan

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

 30

 30

合计

 810

 950




  四、解题思路:看了项目要求后很快就有了大概的解题思路了,在制作文本编辑器时就有相关的关于行数,字数等的统计功能,主要花了较长时间重新学习相关的Java知识(如:正则表达式等),参考了其他人的解题思路,发现大家的思路都是大同小异,花了一定的时间后终于实现了相关功能。

                                                                                                    
  五、功能实现

    1.   WC类主程序

      import java.io.BufferedReader;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.util.Scanner;

      public class WC {

      public static void main(String[] args) throws IOException{
      while(true) {
      System.out.println("指令说明:");
      System.out.println("**************************************");
      System.out.println("wc.exe -c 返回文件 file.c 的字符数:");
      System.out.println("wc.exe -w 返回文件 file.c 的词的数目 ");
      System.out.println("wc.exe -l 返回文件 file.c 的行数");
      System.out.println("wc.exe -a 返回文件 file.c 的空行,代码行,注释行");
      System.out.println("wc.exe -x 启用图形界面");
      System.out.println("**************************************");
      System.out.println("输入指令:");

      Scanner input=new Scanner(System.in); //从键盘上输入指令并执行
      String commend=input.nextLine();
      switch (commend) {
      case "-c":
      Charcount ch = new Charcount();
      break;
      case "-w":
      Wordcount wc = new Wordcount();
      break;
      case "-l":
      Linecount line = new Linecount();
      break;
      case "-a":
      Complex cp = new Complex();
      break;
      case "-x":
      WCJFrame wf = new WCJFrame();
      break;
      default:
      System.out.println("指令错误,请重新输入:");
      break;
      }

      }
      }
      }

    2. -c指令实现

      public class Charcount {
      String REGEX ="\\S";
      Charcount() throws IOException {
      System.out.println("输入路径:");
      Scanner input=new Scanner(System.in);
      String path=input.nextLine();
      BufferedReader fis =new BufferedReader(new FileReader(path));
      int charcount=0;
      String w;
      Pattern p =Pattern.compile(REGEX);//匹配合适的字符
      while((w=fis.readLine()) != null) {
      Matcher m =p.matcher(w);
      while(m.find()) //找到对应字符时字符数+1

      charcount ++;
      }
      System.out.println("字符数:"+charcount);
      fis.close();

      }
      }

    3. -w指令实现

      public class Wordcount {
      String REGEX ="[a-zA-Z]+\\b"; //判定为单词的正则表达式条件
      Wordcount() throws IOException {
      System.out.println("输入路径:");
      Scanner input=new Scanner(System.in);
      String path=input.nextLine();
      BufferedReader fis =new BufferedReader(new FileReader(path));
      int wordcount =0;
      String w;
      Pattern p =Pattern.compile(REGEX);
      while((w=fis.readLine()) != null) {
      Matcher m =p.matcher(w);
      while(m.find()) //当找到符合条件的内容时单词数+1
      wordcount ++;
      }

      System.out.println("单词数:"+wordcount);
      fis.close();
      }
      }

    4. -l指令实现

      public class Linecount {
      Linecount() throws IOException {
      System.out.println("输入路径:");
      Scanner input=new Scanner(System.in);
      String path=input.nextLine();
      BufferedReader fis =new BufferedReader(new FileReader(path));
      int linecount=0;
      while(fis.readLine()!=null) { //当前行不为空时,行数+1
      linecount++;
      }
      System.out.println("行数:"+linecount);
      fis.close();

      }
      }

    5. -a指令实现

      public class Complex {

      Complex() throws IOException{
      System.out.println("输入路径:");
      Scanner a = new Scanner(System.in);
      String path = a.nextLine();
      BufferedReader fis = new BufferedReader(new FileReader(path));
      int spacecount = 0;
      int notecount = 0;
      int codecount = 0;
      boolean state = false;
      String c;
      while((c=fis.readLine())!=null) {
      if(c.contains("/*")) { //多行注释开始标记
      notecount++;
      state = true;
      }
      else if(state) {
      notecount++;
      if(c.contains("*/")) { //多行注释结束标记
      state = false;}
      }
      else if(c.contains("//")) { //单行注释标记
      notecount++;
      }
      else if(c.trim().length()>1) { //判定为代码行条件
      codecount++;
      }
      else {spacecount++;}
      }

      fis.close();
      System.out.println("空白行:"+spacecount);
      System.out.println("注释行:"+notecount);
      System.out.println("代码行:"+codecount);
      }
      }

  六、功能测试

  1.测试文档

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

  2.-c指令回归测试

  

  3.-w、-a、-x指令测试

  

  4.-x指令测试

  

  

  七、心得回顾

  第一次详细制定计划去完成一个项目,各方面都觉得收获匪浅。不仅回顾了以前学过的知识,还学会了运用刚学会的知识。但在时间安排上还存在很大的问题,这点有待改进。

猜你喜欢

转载自www.cnblogs.com/nullcjm/p/11587364.html