实现wc部分功能 java

GitHub地址https://github.com/carlylewen/ruangong

相关要求

  • 基本功能

    • wc.exe -c file.c     //返回文件 file.c 的字符数(实现)
    • wc.exe -w file.c    //返回文件 file.c 的词的数目 (实现)
    • wc.exe -l file.c      //返回文件 file.c 的行数(实现)
  • 扩展功能

    •    -s   递归处理目录下符合条件的文件。(未实现)
    •     -a   返回更复杂的数据(代码行 / 空行 / 注释行)。(实现)
  • 高级功能

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

PSP

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

   

· Estimate

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

60  85

Development

开发

   

· Analysis

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

 180  210

· Design Spec

· 生成设计文档

 120  80

· Design Review

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

 60 40

· Coding Standard

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

 60  30

· Design

· 具体设计

 180  160

· Coding

· 具体编码

 360  350

· Code Review

· 代码复审

 60  30

· Test

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

 120  60

Reporting

报告

 60  30

· Test Report

· 测试报告

 60  60

· Size Measurement

· 计算工作量

 60  30

· Postmortem & Process Improvement Plan

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

 20  15

合计

   1340  1095

设计说明

主函数: 通过键盘输入获取命令,将命令分割为命令参数和文件名,调用统计函数功能。

统计函数:读取文件,通过readline()读取每一行的内容,根据readline的功能记录行数; 根据所读每一行的长度计算字符数;再将所读的数据根据空格切割拼接计算;空行、 注释行、代码行则用正则判断计算。

 

使用说明

需求举例:

-c    [file.txt]   返回文件 file.txt 的字符数

-w   [file.txt]   返回文件 file.txt 的单词数

-l     [file.txt]   返回文件 file.txt 的行数

-a    [file.txt]   返回文件 file.txt 的空行、注释行、代码行数

-x    打开图形界面选择文件,返回文件 file.txt 的单词数,行数,字符数空行、注释行、代码行数

代码

项目目录:

主函数:

 1 public static void main(String[] args) {
 2         // TODO Auto-generated method stub
 3         command = " ";// 存储用户命令
 4         while (true) {
 5             int flag1 = 0;
 6             System.out.println("請输入指令及路径:");
 7             Scanner s = new Scanner(System.in);// 从键盘输入
 8             command = s.nextLine();// 获取输入命令
 9             // 图形化
10             if (command.equals("-x")) {
11                 tuxing();
12                 continue;
13             }
14             String[] split = command.split(" ");// 分割命令
15             int messlength = split.length;
16             sparameter = new String[messlength - 1];
17             // 获取命令参数
18             for (int i = 0; i < messlength - 1; i++) {
19                 sparameter[i] = split[i];
20 
21             }
22             // 获取文件名
23             filename = split[messlength - 1];
24 
25             extend();// 统计功能
26             output();// 输出
27 
28         }
29 
30     }

统计函数:(基本功能与空行、注释行、代码行计算)

 1 private static void extend() {
 2         cchar = 0;
 3         cword = 0;
 4         cline = 0;
 5         spaceline = 0;
 6         codeline = 0;
 7         noteline = 0;
 8         boolean q=false;
 9         File file = new File(filename);
10         if (file.exists()) {
11             try {
12                 FileInputStream filein = new FileInputStream(file);// 输入流读取文件
13                 
14                 BufferedReader bread = new BufferedReader(new InputStreamReader(filein));
15                 String line = "";
16                 StringBuffer buffer = new StringBuffer();
17                 while ((line = bread.readLine()) != null) {
18                     cline++;
19                     buffer.append(line);
20                     cchar += line.length();
21                     line=line.trim();
22 
23                     // 空行,注释行,代码行
24                     String begin="\\s*/\\*.*";
25                     String end =".*\\*/\\s*";
26                     String x= "//.*";
27                     String space="\\s*";
28                     if(line.matches(begin)&&line.matches(end)) {
29                         ++noteline;
30                     }
31                     if(line.matches(begin)) {
32                         ++noteline;
33                         q=true;
34                     }
35                     else if(line.matches(end)) {
36                         ++noteline;
37                         q=false;
38                     }
39                     else if(line.matches(space)) {
40                         ++spaceline;
41                     }
42                     else if(line.matches(x)) {
43                         ++noteline;
44                     }
45                     else if(q) {
46                         ++noteline;
47                     }
48                     else {
49                         ++codeline;
50                     }
51                     
52 
53 
54                 }
55                 cword = buffer.toString().split("\\s+").length;
56                 bread.close();
57                 
58                 filein.close();
59 
60             } catch (FileNotFoundException e) {
61                 // TODO: handle exception
62                 e.printStackTrace();
63             } catch (UnsupportedEncodingException e) {
64                 e.printStackTrace();
65             } catch (IOException e) {
66                 // TODO Auto-generated catch block
67                 e.printStackTrace();
68             }
69 
70         } else {
71             System.out.println("failed!!");
72         }
73     }

图形界面函数:

 1 private static void tuxing() {
 2         // TODO Auto-generated method stub
 3         flag = 1;
 4         chooser = new JFileChooser();
 5         int value = chooser.showOpenDialog(null);
 6         if (value == JFileChooser.APPROVE_OPTION) {
 7             File ff = chooser.getSelectedFile();
 8             filename = ff.getAbsolutePath();
 9             extend();
10             output();
11         }
12     }

 测试:

基本功能:
 
测试文件:
 测试结果:

扩展功能:
 
测试文件:

测试结果:

高级功能:

测试文件:

 

测试结果:

代码覆盖率

进行代码覆盖率测试

 

代码覆盖率:95.2%

 

总结:

 从一开始计划的时候,先确定了基本功能的实现用什么方法,也重新看了java的文件流和I/O流等,然后再学习正则表达式,如何去判断空行,代码行和注释行,这次项目让我重新复习了许多知识点,也学会更加规范和简洁的编写代码。在这次作业中,我采用了java语言来实现,在完成代码后,也学习了如何比较规范的多方面测试代码以及测试代码覆盖率。

不足:在这次实践中,没有完成递归功能,由于在实现过程中一直出现文件未授权访问的情况,在多方求解后依然无法解决,因此放弃了递归的功能。

总的来说,这一次实践让我认识了,编程不仅仅是编写代码,它包括从一开始的设计构思,到编写实现功能,以及最后的运行测试优化,也希望能从以后学习到更多知识和技能。

猜你喜欢

转载自www.cnblogs.com/chenjiaman/p/9637312.html