wordCount
GitHub地址:https://github.com/fightcypress/test
项目相关要求
1.WC 项目要求
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 的行数
扩展要求列表:
wc.exe -s file.c //递归处理目录下符合条件的文件。
wc.exe -a file.c //返回更复杂的数据(代码行 / 空行 / 注释行)。
遇到的困难及解决方法
1.对于Java的工具类实现还不够熟悉 解决方法:通过百度以及查看API查找相关的实现
2.对于单元测试,并没有足够的理解 解决方法:翻阅书籍,以及观看视频学习如何进行单元测试
3.GitHub尚未学会上传代码 解决方法:在网络上吸取前辈的做法,以及询问有过相关经历的同学
收获:这几点困难给我最大的收获就是,在编写程序之前,一定要整理好自己的思路,不要一开始记着手编写程序。正所谓“一口吃不成一个胖子”,罗马也并非一日建成,想要最大效率取得成绩,就一定要有一个清晰的思路,并不是一开始闷头就干;其次,遇到问题一定要学会查找资料,因为百度能解决99%的问题,在编写代码时遇到的问题,在百度上大多数都能找到,如果找不到那就谷歌,实在没有办法了,也不要太执着自己一个人冥思苦想,可以向外界寻求一定的帮助。
关键代码and设计说明
(1)主函数
1 public static void main(String[] args) { 2 System.out.println("输入程序参数列表:"); 3 System.out.println("支持 -c(字符数)"); 4 System.out.println("支持 -w(单词数)"); 5 System.out.println("支持 -l(行数)"); 6 System.out.println("支持 -s(递归处理文件夹)"); 7 System.out.println("支持 -a(空白注释行)"); 8 9 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //用户输入 10 String line = null; 11 String[] commands = null; //输入命令格式:wc.exe -c D:\\Desktop\test.c 12 while (true) { 13 try { 14 if ((line = br.readLine()) != null) { 15 commands = line.split(" "); //空格隔开 16 if ("wc.exe".equals(commands[0])) { //对输入命令进行检查 17 System.out.println("运行wc.exe程序"); 18 if ("-s".equals(commands[1])) { //递归处理文件 19 List<String> s = getfolders(commands[2]); 20 // System.out.println(s); 21 System.out.println("请选择计算类型:"); 22 Scanner scanner = new Scanner(System.in); 23 String command = scanner.nextLine(); 24 switch (command) { 25 case "-c": //计算字符数 26 for(String name:s) { 27 charsCount(name); 28 } 29 break; 30 case "-w": //计算单词数 31 for(String name:s) { 32 wordsCount(name); 33 } 34 break; 35 case "-l": //计算行数 36 for(String name:s) { 37 linesCount(name); 38 } 39 break; 40 case "-a": //计算注释空白行数 41 for(String name:s) { 42 extendCount(name); 43 } 44 break; 45 } 46 47 48 49 } 50 switch (commands[1]) { 51 case "-c": //计算字符数 52 charsCount(commands[2]); 53 break; 54 case "-w": //计算单词数 55 wordsCount(commands[2]); 56 break; 57 case "-l": //计算行数 58 linesCount(commands[2]); 59 break; 60 /* 61 * case "-s": getfolders(commands[2]); 62 */ 63 case "-a": //计算注释空白行数 64 extendCount(commands[2]); 65 break; 66 } 67 } 68 } 69 } catch (FileNotFoundException e) { //其余情况都认为文件异常 70 System.out.println("文件异常"); 71 } catch (IOException e) { 72 System.out.println("读取失败"); 73 } 74 } 75 76 }
(2)递归获取目录下指定后缀的文件
1 // 递归获取目录下指定后缀的文件 2 public static List<String> getfolders(String filepath) { 3 File file = new File(filepath); // 创建对象 4 File[] folders = file.listFiles(); // 获取文件夹列表 5 List<File> result = new ArrayList<File>(); 6 List<String> s = new ArrayList<String>(); 7 8 if (folders.length == 0) { // 判断文件夹列表(为空) 9 return null; 10 } else { // 不为空 11 for (File f : folders) { 12 if (f.isDirectory()) { // 存在子目录(递归文件夹获取) 13 getfolders(f.getAbsolutePath()); 14 } else if (f.getName().endsWith(".c")) { 15 result.add(f); 16 17 } 18 } 19 } 20 21 for (File command : result) { 22 System.out.println(command); 23 s.add(command.getAbsolutePath()); 24 } 25 return s; 26 }
(3)计算单词个数
public static int wordsCount(String filepath) { int num = 0; File file = new File(filepath); BufferedReader bf = null; try { bf = new BufferedReader(new FileReader(file)); //读取文件 String line = ""; while ((line = bf.readLine()) != null) { String[] wordline = line.split("\\W+"); //空格隔开单词 for (String word : wordline) { if(!word.equals("")) //空行单词数不添加 num++; //遍历计算单词个数 } } bf.close(); //关闭流 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("文件异常!"); } System.out.println("文件:"+filepath+" 单词数:" + num); return num; }
(4)计算文件行数
public static int linesCount(String filepath) { int num = 0; File file = new File(filepath); BufferedReader bf = null; try { bf = new BufferedReader(new FileReader(file)); //读取文件 while (bf.readLine() != null) { num++; //计算行数 } bf.close(); //关闭流 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("文件异常!"); } System.out.println("文件:"+filepath+" 行数:" + num); return num; }
(5)计算文件字符数
1 public static int charsCount(String filepath) { 2 int num = 0; 3 File file = new File(filepath); 4 BufferedReader bf = null; 5 try { 6 bf = new BufferedReader(new FileReader(file)); //读取文件 7 String line1, line2; 8 while ((line1 = bf.readLine()) != null) { 9 line2 = line1.replaceAll("\\s+", ""); //将空白符替换掉(删去) 10 num = num + line2.length(); 11 } 12 bf.close(); //关闭流 13 } catch (Exception e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 System.out.println("文件异常!"); 17 } 18 System.out.println("文件:"+filepath+" 字符数:" + num); 19 return num; 20 }
(6)计算空白注释行数
1 public static void extendCount(String filepath) { 2 int emptyCount = 0; 3 int commentCount = 0; 4 int codeCount = 0; 5 File file = new File(filepath); 6 BufferedReader bf = null; 7 try { 8 bf = new BufferedReader(new FileReader(file)); 9 String line; 10 while((line=bf.readLine())!=null) { 11 if(line.length()==0||line.length()==1) { 12 emptyCount++; 13 }else if(line.contains("//")) { 14 commentCount++; 15 }else { 16 codeCount++; 17 } 18 } 19 bf.close(); 20 System.out.println("文件:"+filepath+" 空白行数:"+emptyCount); 21 System.out.println("文件:"+filepath+" 注释行数:"+commentCount); 22 System.out.println("文件:"+filepath+" 代码行数:"+codeCount); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 }
运行结果截图:
PSP
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 40 | 60 |
· Estimate | · 估计这个任务需要多少时间 | 720 | 600 |
Development | 开发 | 360 | 420 |
· Analysis | · 需求分析 (包括学习新技术) | 60 | 90 |
· Design Spec | · 生成设计文档 | 30 | 60 |
· Design Review | · 设计复审 (和同事审核设计文档) | 30 | 60 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 20 | 30 |
· Design | · 具体设计 | 30 | 60 |
· Coding | · 具体编码 | 30 | 60 |
· Code Review | · 代码复审 | 60 | 90 |
· Test | · 测试(自我测试,修改代码,提交修改) | 90 | 120 |
Reporting | 报告 | 20 | 30 |
· Test Report | · 测试报告 | 30 | 50 |
· Size Measurement | · 计算工作量 | 20 | 30 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 15 | 20 |
合计 | 1550 | 1780 |
收获与感悟:
项目开发之前必须要整理好思路,分析需求,不要着手就开始搞,经过此次作业,我发现Java语言的博大,通过查看文档等一系列方法,可以了解到很多平时不知道的方法,而且遇事不要慌张,先思考,不行就去百度,百度很重要!