一、Github地址:

https://github.com/duwei1996/wc

二、PSP2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

30 30

· Estimate

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

30 30

Development

开发

540 900

· Analysis

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

60 120

· Design Spec

· 生成设计文档

0 0

· Design Review

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

0 0

· Coding Standard

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

60 120

· Design

· 具体设计

60 60

· Coding

· 具体编码

240 360

· Code Review

· 代码复审

60 120

· Test

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

60 60

Reporting

报告

90 120

· Test Report

· 测试报告

30 45

· Size Measurement

· 计算工作量

30 30

· Postmortem & Process Improvement Plan

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

30 45
 

合计

700 1050

三、设计思路

1.程序设计两个主要方法baseFunction()和extendFunction(),分别用于实现基本功能和扩展功能

2.设计辅助方法如countChar()、countLine()等供上面两个方法调用

3.根据接收的命令参数,判断执行相应的操作

四、主要代码

1.main函数

  从输入参数获取要处理的文件, 并对文件做基本功能和扩展功能的处理

public static void main(String[] args) throws Exception{ File file = new File(args[args.length-1]);     baseFunction(args,file);extendFunction(args,file);
}

2.baseFunction函数和baseCount函数

  调用baseCount方法,根据参数,输出相应结果

public static void baseFunction(String[] strings, File file)throws IOException{if (file == null || !file.exists())throw new FileNotFoundException(file + ",文件不存在!");baseCount(file);List<String> list = Arrays.asList(strings);if (list.contains("-c")){//countChar()System.out.println(file.getName() + "," + result1); } if (list.contains("-w")&& !list.contains("-e")){ //countWord() System.out.println(file.getName() + "," + result2); } if (list.contains("-l")){ //counLine() System.out.println(file.getName() + "," + result3); } if (list.contains("-o")){ String content = new String(file.getName() + "\r\n" + result1 + "\r\n" + result2 + "\r\n" + result3); Path outPath = Paths.get("output.txt"); File output = new File(outPath.toString()); Files.write(outPath,content.getBytes()); } }

  baseCount方法用于读取文件内容,并计算字符、单词和行数

 public static void baseCount(File file) throws IOException{BufferedReader br = helper(file);String s = null;while ((s = br.readLine()) != null){s = br.readLine();countChar(s);countWord(s);countLine(s);}}

3.extendFunction函数和extendCount函数

  调用baseCount方法,根据参数,输出相应结果  

public static void extendFunction(String[] strings,File file)throws IOException{extendCount(file);List<String> list = Arrays.asList(strings);if (list.contains("-s")){//recFile(file);}if (list.contains("-a")){System.out.println(file.getName() +",代码行 / 空行 / 注释行:" + codeLineNum + "/" + blankLineNum + "/" + annotationLineNum); } if (list.contains("-w")&&list.contains("-e")){ baseCount(file); String s1 = list.get(list.indexOf("-w")+1); String s2 = list.get(list.indexOf("-e")+1); File file1 = new File(s1); File file2 = new File(s2); int sameNum = stopList(file1,file2); System.out.println(file.getName() +",单词数:" + (wordNum - sameNum)); } }

  extendCount方法

public static void extendCount(File file) throws IOException{exCountLine(file);recFile(file);}

  exCountLine方法

public static void exCountLine(File file) throws IOException {if (file == null || !file.exists())throw new FileNotFoundException(file + ",文件不存在!");//fileCount ++;   // 文件数累加if (file.isDirectory()) {recFile(file);} else {BufferedReader bufr = null; try { // 将指定路径的文件与字符流绑定 bufr = new BufferedReader(new InputStreamReader(new FileInputStream(file))); } catch (FileNotFoundException e) { throw new FileNotFoundException(file + ",文件不存在!" + e); } // 定义匹配每一行的正则匹配器 Pattern annotationLinePattern = Pattern.compile("((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+", Pattern.MULTILINE + Pattern.DOTALL); // 注释匹配器(匹配单行、多行、文档注释)  Pattern blankLinePattern = Pattern.compile("^\\s*$"); // 空白行匹配器(匹配回车、tab键、空格)  Pattern codeLinePattern = Pattern.compile("(?!import|package).+;\\s*(((//)|(/\\*+)).*)*", Pattern.MULTILINE + Pattern.DOTALL); // 代码行匹配器(以分号结束为一行有效语句,但不包括import和package语句) // 遍历文件中的每一行,并根据正则匹配的结果记录每一行匹配的结果 String line = null; try { while ((line = bufr.readLine()) != null) { if (annotationLinePattern.matcher(line).find()) { annotationLineNum++; } if (blankLinePattern.matcher(line).find()) { blankLineNum++; } if (codeLinePattern.matcher(line).matches()) { codeLineNum++; } } } catch (IOException e) { throw new RuntimeException("读取文件失败!" + e); } finally { try { bufr.close(); // 关闭文件输入流并释放系统资源 } catch (IOException e) { throw new RuntimeException("关闭文件输入流失败!"); } } } }

  recFile函数,实现递归处理文件夹

public static void  recFile(File file) throws IOException{File[] files = file.listFiles(new FileFilter() {public boolean accept(File pathname) {return pathname.getName().endsWith(".c") || pathname.isDirectory();}});for (File target : files) {extendCount(target);}}

  stopList函数,实现禁用单词计数;

public static int stopList(File file1,File file2) throws IOException{String[] strings1 = word(file1);String[] strings2 = word(file2);return same(strings1,strings2);}public static String[] word(File file) throws IOException{BufferedReader br = helper(file); String s = null; String[] content = null; while ((s=br.readLine())!=null){ s = br.readLine(); String a = ","; s.replaceAll(a," "); content = s.split(" "); } return content; } public static int same(String[] a,String[] b){ ArrayList<String> same = new ArrayList<String>(); ArrayList<String> temp = new ArrayList<String>(); for (int i = 0; i < a.length; i++) { temp.add(a[i]); //把数组a中的元素放到Set中,可以去除重复的元素  } for (int j = 0; j < b.length; j++) { //把数组b中的元素添加到temp中 //如果temp中已存在相同的元素,则temp.add(b[j])返回false if(!temp.add(b[j])) same.add(b[j]); } return same.size(); }

五、测试

1.基本功能

wc.exe -c file.c     //返回文件 file.c 的字符数
wc.exe -w file.c     //返回文件 file.c 的单词总数
wc.exe -l file.c     //返回文件 file.c 的总行数
wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

测试文本

命令与结果

2.扩展功能

wc.exe -s            //递归处理目录下符合条件的文件
wc.exe -a file.c     //返回更复杂的数据(代码行 / 空行 / 注释行)
wc.exe -e stopList.txt  // 停用词表,统计文件单词总数时,不统计该表中的单词

禁用列表

命令与结果

注:另外两个功能尚未实现

六、体会

  通过编程,更深地体会到需求设计的重要性;通过测试,更加了解需求与产品之间的差别,以及测试的必要性和重要性。

七、参考文献

http://www.cnblogs.com/ningjing-zhiyuan/p/8563562.html

转载于:https://www.cnblogs.com/duwei1996/p/8613783.html

软测第二周作业WordCount相关推荐

  1. 软件质量与测试--第二周作业 WordCount

    软件质量与测试--第二周作业 WordCount Github地址: https://github.com/RicardoDZX/WordCount PSP: PSP2.1 PSP 阶段 预估耗时 ( ...

  2. 第二周作业 wordcount

    Github地址 https://github.com/DolittleQZ/CountWord PSP2.1表格 PSP2.1 PSP 阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...

  3. 2019年春季学期第二周作业(文件指针)

    2019年春季学期第二周作业(基础作业) 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始).并将最大值和对应的最小下标数值写入文件. 输入: 请建立以自 ...

  4. 软件工程 第二周作业

    ##软件工程第二周作业 提出问题 1. 一般来说,想要自己的程序跑得又快又好,就要减少函数的反复调用,但有所得则必有所失,效能提高就有可能伴随着程序的稳定性的降低,这两者应该如何权衡呢? 2. 关于5 ...

  5. 2017-2018-1 20179215《Linux内核原理与分析》第二周作业

    20179215<Linux内核原理与分析>第二周作业 这一周主要了解了计算机是如何工作的,包括现在存储程序计算机的工作模型.X86汇编指令包括几种内存地址的寻址方式和push.pop.c ...

  6. 学习linux第二周作业

    第二周作业: 本周作业内容: 1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. touch,rm,mv,cp,file,ls,chmod,chown,ln,rename, ...

  7. 序列模型第二周作业1:Operations on word vectors

    来自吴恩达深度学习系列视频:序列模型第二周作业1:Operations on word vectors.如果英文对你来说有困难,可以参照:[中文][吴恩达课后编程作业]Course 5 - 序列模型 ...

  8. 20189221 2018-2019-2 《密码与安全新技术专题》第二周作业

    20189221 2018-2019-2 <密码与安全新技术专题>第二周作业 课程:<密码与安全新技术专题> 班级: 201892 姓名: 郭开世 学号:20189221 上课 ...

  9. 「数据结构」普林斯顿算法课第二周作业

    「数据结构」普林斯顿算法课第二周作业 Algorithm I, Princeton 编程作业: Deques and Randomized Queues 思路 Deque.java Randomize ...

最新文章

  1. 2021-10-14 yolov5踩坑!!!经验大赏
  2. 面向对象的程序设计之原型模式
  3. C++ dynamic_cast操作符
  4. 数学建模——智能优化之粒子群模型详解Python代码
  5. 网络分层模型OSI和TCP/IP四层模型
  6. VTK:命名颜色用法实战
  7. Java sqlite事务方法,Java SQLiteDatabase.insert方法代码示例
  8. 彻底理解链接器:二,符号决议
  9. CV Code | 本周新出计算机视觉开源代码汇总(含目标跟踪、语义分割、姿态跟踪、少样本学习等)...
  10. 推行法定数字货币,现有支付宝/微信等支付系统,会否被数字货币支付系统替代并超越?
  11. 我最讨厌哪种数据分析师?这四点全中就可以辞职走人了
  12. Mosquitto搭建Android推送服务番外篇一:各种报错解决
  13. Linux常用命令详解2
  14. 【从零开始学架构-李运华】08|架构设计三原则
  15. dialogArguments
  16. FastReport报表控件使用技巧总结
  17. 保健品消费者需求调研内容及设计
  18. python统计套利_【独家发布】期货市场内外盘低频统计套利基于Python
  19. 工业企业产值产量电子台账操作指南(第一版)
  20. python炒股软件开发_Python之路day03-习题+作业-股票查询程序开发

热门文章

  1. C#= 栈模仿堆的操作
  2. [教程指导]索尼官方4.0.3系统一键root方法! [复制链接]
  3. HTML、CSS知识点总结,浅显易懂。
  4. 前端学习总结——CSS布局方式之传统布局
  5. eslint 禁用命令
  6. 算法 --- 平衡二叉树
  7. 符合skyline的3dml网络发布服务
  8. hbase里插入big int数据用Phoenix查看的报错问题
  9. 验证VSPHERE5 支持大于2TB磁盘
  10. 【第二十七章】 springboot + zipkin(brave-okhttp实现)