PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
|
|
· Estimate |
· 估计这个任务需要多少时间 |
|
|
Development |
开发 |
|
|
· Analysis |
· 需求分析 (包括学习新技术) |
|
|
· Design Spec |
· 生成设计文档 |
|
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
|
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
|
|
· Design |
· 具体设计 |
|
|
· Coding |
· 具体编码 |
|
|
· Code Review |
· 代码复审 |
|
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
|
|
Reporting |
报告 |
|
|
· Test Report |
· 测试报告 |
|
|
· Size Measurement |
· 计算工作量 |
|
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
|
|
合计 |
1895 |
|
1.1 WC 项目要求
空行:
代码行:
注释行:
public enum CountImpl implements Count { OP_C{ @Override public int op(byte[] fileByte) { if(fileByte==null) return 0; //文件为空 String fileStr = new String(fileByte); String pattern = "\\S"; return FileUtil.searchStr(pattern, fileStr); } }, OP_W{ @Override public int op(byte[] fileByte){ if(fileByte==null) return 0; String fileStr = new String(fileByte); String REGEX = "[a-zA-Z]+\\W"; return FileUtil.searchStr(REGEX, fileStr); } }, OP_L{ @Override public int op(byte[] fileByte){ if(fileByte==null) return 0; String REGEX = "\\n"; String fileStr = new String(fileByte); return FileUtil.searchStr(REGEX, fileStr)+1;//最后一行通常无回车 } }; }
/** * 批量处理文件 * @param subModel 选择的处理模式 * @param filePath 文件绝对路径 * @throws IOException 文件不存在 */ public static void batchProCount(String subModel, String filePath) throws IOException { File file = new File(filePath); int num=0; if(file.isFile()){//如果是文件 byte[] fileByte = FileUtil.fileToByte(filePath); num = CountImpl.valueOf(subModel).op(fileByte);//根据模式的不同自动分辨使用哪个函数 System.out.println("文件路径:"+file.getAbsolutePath()); System.out.println("计算结果:"+ num +"\n"); }else if(file.isDirectory()){//如果是目录 File[] fileList = file.listFiles(); if(fileList==null) {//如果该目录下无文件 System.out.println("文件不存在"); return; } for (File value : fileList) { batchProCount(subModel, value.getAbsolutePath()); //递归处理 } } } /** *计算文件代码行数、注释行数、空白行数 * @param filePath 文件路径 * @return num[0]为代码行数,num[1]为注释行数,num[2]为空白行数 * @throws IOException 文件不存在 */ public static int[] proCount(String filePath) throws IOException { int[] num = new int[3]; File file = new File(filePath); if(!file.exists()) { System.out.println("文件不存在"); return num; //文件不存在 } BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); int codeLineNum=0; int noteLineNum=0; int blankLineNum=0; String fileLine; while ((fileLine=bufferedReader.readLine())!=null){ fileLine = fileLine.trim(); //计算注释行 if(fileLine.startsWith("/") || fileLine.startsWith("*")){ noteLineNum++; } //计算空行 else if(fileLine.trim().isEmpty() || fileLine.trim().length()==1){ blankLineNum++; } //计算代码行 else { codeLineNum++; } } bufferedReader.close(); num[0] = codeLineNum; num[1] = noteLineNum; num[2] = blankLineNum; return num; }
@FXML public void open(ActionEvent actionEvent) throws IOException { File file = OpenUtil.chooseFile(); long start = System.currentTimeMillis(); if(file==null){ warn.setText("你没有打开任何文件噢~"); return; }else{ path.setText(file.getAbsolutePath()); int[] num; num = WordCount.wc("-c", file.getAbsolutePath()); charNum.setText(String.valueOf(num[0])); num = WordCount.wc("-w", file.getAbsolutePath()); wordNum.setText(String.valueOf(num[0])); num = WordCount.wc("-l", file.getAbsolutePath()); lineNum.setText(String.valueOf(num[0])); num = ProWordCount.proCount(file.getAbsolutePath()); codeNum.setText(String.valueOf(num[0])); noteNum.setText(String.valueOf(num[1])); blankNum.setText(String.valueOf(num[2])); } long runTime = TimeUtil.getRunTime(start); runtime.setText(runTime +"ms"); }
package com.demo; import java.io.*; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WordCounter { public static void main(String[] args) throws IOException { /* Scanner scan = new Scanner(System.in); String model = ""; String filePath = ""; WordCounter.wc(model, filePath); */ System.out.println(wc("-l", "C:\\Users\\15191\\Desktop\\imageTest\\testBrunch\\test\\C_language_Class2_1_utf8.txt")); }