github地址:

https://github.com/qianqincc/wc

 

  • 基本要求

    • [x] -c 统计文件字符数 (实现)

    • [x] -w 统计文件词数 (实现)

    • [x] -l 统计文件行数(实现)

  • 扩展功能

    • [x] -s 递归处理目录下符合条件得文件(实现)
    • [x] -a 返回文件代码行 / 空行 / 注释行(实现)
    • [x] 支持各种文件的通配符(*,?)(实现)
  • 高级功能

    • [ ] -x 图形化界面(未实现)

 

PSP表格

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

30

60

· Estimate

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

30

30

Development

开发

420

480

· Analysis

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

300

360

· Design Spec

· 生成设计文档

40

60

· Design Review

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

60

60

· Coding Standard

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

30

30

· Design

· 具体设计

30

60

· Coding

· 具体编码

200

300

· Code Review

· 代码复审

30

40

· Test

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

30

30

Reporting

报告

80

90

· Test Report

· 测试报告

30

40

· Size Measurement

· 计算工作量

30

25

· Postmortem & Process Improvement Plan

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

30

35

合计

 

740

1200

 

 

解题思路:

刚开始拿到题目的时候,我的想法是需要先把基本功能实现,基本功能的实现需要读文件,需要重新学习IO相关的知识,打算先把主函数的框架搭好,然后再一个一个方法来写。找资料心路历程:不会的先从以前的课本里面学习,没思路的边上网找类似的功能实现,参考思路带来启发。

 

设计实现过程

 

设计了一个类,设计了五个方法,通过在主方法里面调用其余的五个方法。

Main方法里面设计一开始的文件路径、文件名输入提示,以及对于其他方法的实例化。然后再分别设计方法:数字符数counter_char、数词数count_word、数行数count_line、数代码行codeline、递归处理目录下符合条件的文件readfile。

 

代码说明

主函数

   static int count_char(File file) {

        Reader read=null;

        int char_num=0;

        try {

            read =new InputStreamReader(new FileInputStream(file));

            BufferedReader readfile= new BufferedReader(read);

                int tempchar;

                while ((tempchar=readfile.read()) != -1) {

                    if((char)tempchar!='\r'&&(char)tempchar!='\n'){

                        char_num++;

                    }

                }

                readfile.close();

        }

        catch(Exception e){

            System.out.println("指定输入文件不存在");

        }   

        return char_num;

    }

 

 

基本功能-字符数统计

  

 

private static int count_word(File file){

       

        BufferedReader readfile=null;

     

        int w_num=0;

        try{

            readfile = new BufferedReader(new FileReader(file));

            String tempchar;

            while ((tempchar=readfile.readLine()) != null) {

            String[] str=tempchar.split(" ");

            w_num=str.length;

            }

           

            readfile.close();

        }

        catch(Exception e){

            System.out.println("指定输入文件不存在");

        }   

        return w_num;

    }

 

 

 

基本功能-词数统计

 

   private static int count_line(File file) {

       

        Reader read=null;

        try {

           read =new InputStreamReader(new FileInputStream(file));

        } catch (FileNotFoundException e) {

           // TODO 自动生成的 catch 块

           e.printStackTrace();

        }

        BufferedReader readfile= new BufferedReader(read);

        int line=1;

        int templine;

        try {

           while ((templine=readfile.read()) != -1) {

               if((char)templine=='\n'){

                   line++;

               }

 

                }

        } catch (IOException e) {

           // TODO 自动生成的 catch 块

           e.printStackTrace();

        }

        return line;

    }   

   

 

 

基本功能-行数统计

  

  private static int count_line(File file) {

       

        Reader read=null;

        try {

           read =new InputStreamReader(new FileInputStream(file));

        } catch (FileNotFoundException e) {

           // TODO 自动生成的 catch 块

           e.printStackTrace();

        }

        BufferedReader readfile= new BufferedReader(read);

        int line=1;

        int templine;

        try {

           while ((templine=readfile.read()) != -1) {

               if((char)templine=='\n'){

                   line++;

               }

 

                 }

        } catch (IOException e) {

           // TODO 自动生成的 catch 块

           e.printStackTrace();

        }

        return line;

    }   

 

扩展功能-递归处理目录下符合条件的文件。

   

 public static void readfile(File f){

        if(f!=null){

            if(f.isDirectory()){

                File[] fileArray=f.listFiles();

                if(fileArray!=null){

                    for (int i = 0; i < fileArray.length; i++) {

                        //递归调用

                        readfile(fileArray[i]);

                    }

                }

            }

            else{

                System.out.println(f);

            }

        }

    }

}

 

 

扩展功能-统计代码行

 

   private static void code_line(File file)  {

        Reader read=null;

         try {

           read =new InputStreamReader(new FileInputStream(file));

        } catch (FileNotFoundException e1) {

           // TODO 自动生成的 catch 块

           e1.printStackTrace();

        }

         BufferedReader readfile= new BufferedReader(read);

            int whiteLines = 0;

           int commentLines = 0;

           int normalLines = 0;

         boolean comment = false;

           try {

               readfile = new BufferedReader(new FileReader(file));

               String line = "";

               try {

                   while ((line = readfile.readLine()) != null) {

                       line = line.trim();

                       if (line.matches("^[\\s&&[^\\n]]*$")) {

                           whiteLines++;

                       } else if (line.startsWith("/*") && !line.endsWith("*/")) {

                           commentLines++;

                           comment = true;

                       } else if (true == comment) {

                           commentLines++;

                           if (line.endsWith("*/")) {

                               comment = false;

                           }

                       } else if (line.startsWith("//")) {

                               commentLines++;

                       } else {

                           normalLines++;

                       }

                   }

               } catch (IOException e) {

                       e.printStackTrace();

               }

           } catch (FileNotFoundException e) {

                   e.printStackTrace();

           }

           finally {

               if (readfile != null) {

                   try {

                       System.out.println("空行数:"+whiteLines);

                       System.out.println("注释行数:"+commentLines);

                       System.out.println("代码行数:"+normalLines);

                       readfile.close();

                       readfile = null;

                   } catch (IOException e) {

                       e.printStackTrace();

                   }

               }

           }

    }

 

测试运行

1、字符数统计

2、词数统计

 

 

3、行数统计

 

 

4、特殊行数统计

 

5、递归处理目录下符合条件的文件

 

 

代码覆盖率

 

项目小结

本次作业完成时间比预期多了很多,原因有:1、没有时常练习java,所以有比较多的内容以及遗忘,需要花时间重新的复习。 2.有些设计的难度没有估计准确,所以花多了一些时间。

收获:通过这一次的作用,让我学习了软件工程的一种系统的设计软件的流程,并且熟悉了java的设计,收获颇丰。

posted on 2018-09-12 22:45  陈泽超  阅读(202)  评论(0)    收藏  举报