github项目传送门:https://github.com/NgYanYee/WC
wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。
实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:
wc.exe [parameter] [file_name]
基本功能列表:
扩展功能:
根据项目主要功能去网上查询实现功能用到的一些类和方法,遇到自己没有接触过的方法就去了解实现功能与原理。
基本解决
//识别命令是否包含-s命令 String commandList[] = command.nextLine().split("\\s"); boolean hasSArg = Arrays.asList(commandList).stream().anyMatch(s -> s.equals("-s")); if(hasSArg) { String pathname = commandList[commandList.length - 2]; String fileFilter = commandList[commandList.length - 1]; List<String> paths = new ArrayList<String>(); paths = getAllFilePaths(new File(pathname), paths, fileFilter); if(paths != null) { for(int i = 0; i < paths.size(); i++) { operation(paths.get(i), commandList); } } } else { // System.out.println("不含有s"); operation(commandList[commandList.length - 1], commandList); }
private static List<String> getAllFilePaths(File basePath,List<String> filePaths,String fileFilter){ File[] files = basePath.listFiles(); if(files == null) { System.out.println("该目录下无文件,请检查命令格式"); return filePaths; } for(File f:files) { if(f.isDirectory()) { // 找到文件夹,继续寻找下一目录 getAllFilePaths(f, filePaths, fileFilter); } else { if((f.getName() == fileFilter) || f.getName().endsWith(fileFilter)) { //找到匹配文件,添加到路径数组 filePaths.add(f.getPath()); } } } return filePaths; }
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "utf-8"); BufferedReader bReader = new BufferedReader(isr);
String string = null; while((string = bReader.readLine()) != null) { string = string.trim(); count += string.length(); }
boolean blank = true; String string = null; while((string = bReader.readLine()) != null) { string = string.trim(); for (char c : string.toCharArray()) { if ((c >= ‘a‘ && c <= ‘z‘) || (c >= ‘A‘ && c <= ‘Z‘)){ if (blank){ blank = false; count++; } }else if ( c == ‘-‘ && !blank){ }else { blank = true; } } }
while(bReader.read()!= -1) { String string = bReader.readLine(); count++; }
public static void codeCount(String filePath) { int code = 0; int codeComments = 0; int codeBlanks = 0; try { InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath)); BufferedReader bReader = new BufferedReader(isr); boolean comm = false; String s = null; while((s = bReader.readLine()) != null) { if(s.startsWith("/*") && s.endsWith("*/")) { codeComments++; } else if(s.trim().startsWith("//")) { codeComments++; } else if(s.startsWith("/*") && !s.endsWith("*/")) { codeComments++; comm = true; } else if(!s.startsWith("/*") && s.endsWith("*/")) { codeComments++; comm = false; } else if(comm) { codeComments++; } else if(s.trim().length() < 1) { codeBlanks++; } else { code++; } } isr.close(); System.out.println("代码行数: " + code); System.out.println("注释行数: " + codeComments); System.out.println("空行数: " + codeBlanks); } catch (Exception e) { // TODO: handle exception } }
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
30 | 60 |
· Estimate |
估计这个任务需要多少时间 |
20 | 10 |
Development |
开发 |
670 | 600 |
· Analysis |
· 需求分析 (包括学习新技术) |
120 | 200 |
· Design Spec |
· 生成设计文档 |
60 | 40 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 | 20 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 | 30 |
· Design |
· 具体设计 |
120 | 60 |
· Coding |
· 具体编码 |
210 | 180 |
· Code Review |
· 代码复审 |
40 | 30 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
60 | 40 |
Reporting |
报告 |
100 | 120 |
· Test Report |
· 测试报告 |
40 | 40 |
· Size Measurement |
· 计算工作量 |
30 | 30 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
30 | 40 |
合计 |
800 | 780 |
原文:https://www.cnblogs.com/NgYanYee/p/9649001.html