软测第二周作业WordCount
一、Github地址:
https://github.com/duwei1996/wc
二、PSP2.1表格
| PSP2.1 | PSP階段 | 預(yù)估耗時 (分鐘) | 實際耗時 (分鐘) |
| Planning | 計劃 | 30 | 30 |
| · Estimate | · 估計這個任務(wù)需要多少時間 | 30 | 30 |
| Development | 開發(fā) | 540 | 900 |
| · Analysis | · 需求分析 (包括學(xué)習(xí)新技術(shù)) | 60 | 120 |
| · Design Spec | · 生成設(shè)計文檔 | 0 | 0 |
| · Design Review | · 設(shè)計復(fù)審 (和同事審核設(shè)計文檔) | 0 | 0 |
| · Coding Standard | · 代碼規(guī)范 (為目前的開發(fā)制定合適的規(guī)范) | 60 | 120 |
| · Design | · 具體設(shè)計 | 60 | 60 |
| · Coding | · 具體編碼 | 240 | 360 |
| · Code Review | · 代碼復(fù)審 | 60 | 120 |
| · Test | · 測試(自我測試,修改代碼,提交修改) | 60 | 60 |
| Reporting | 報告 | 90 | 120 |
| · Test Report | · 測試報告 | 30 | 45 |
| · Size Measurement | · 計算工作量 | 30 | 30 |
| · Postmortem & Process Improvement Plan | · 事后總結(jié), 并提出過程改進計劃 | 30 | 45 |
| ? | 合計 | 700 | 1050 |
三、設(shè)計思路
1.程序設(shè)計兩個主要方法baseFunction()和extendFunction(),分別用于實現(xiàn)基本功能和擴展功能
2.設(shè)計輔助方法如countChar()、countLine()等供上面兩個方法調(diào)用
3.根據(jù)接收的命令參數(shù),判斷執(zhí)行相應(yīng)的操作
四、主要代碼
1.main函數(shù)
從輸入?yún)?shù)獲取要處理的文件, 并對文件做基本功能和擴展功能的處理
public static void main(String[] args) throws Exception{ File file = new File(args[args.length-1]); baseFunction(args,file);extendFunction(args,file); }?
2.baseFunction函數(shù)和baseCount函數(shù)
調(diào)用baseCount方法,根據(jù)參數(shù),輸出相應(yīng)結(jié)果
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方法用于讀取文件內(nèi)容,并計算字符、單詞和行數(shù)
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函數(shù)和extendCount函數(shù)
調(diào)用baseCount方法,根據(jù)參數(shù),輸出相應(yīng)結(jié)果
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() +",單詞數(shù):" + (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 ++; // 文件數(shù)累加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); // 代碼行匹配器(以分號結(jié)束為一行有效語句,但不包括import和package語句) // 遍歷文件中的每一行,并根據(jù)正則匹配的結(jié)果記錄每一行匹配的結(jié)果 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(); // 關(guān)閉文件輸入流并釋放系統(tǒng)資源 } catch (IOException e) { throw new RuntimeException("關(guān)閉文件輸入流失敗!"); } } } }recFile函數(shù),實現(xiàn)遞歸處理文件夾
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函數(shù),實現(xiàn)禁用單詞計數(shù);
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]); //把數(shù)組a中的元素放到Set中,可以去除重復(fù)的元素 } for (int j = 0; j < b.length; j++) { //把數(shù)組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 的字符數(shù) wc.exe -w file.c //返回文件 file.c 的單詞總數(shù) wc.exe -l file.c //返回文件 file.c 的總行數(shù) wc.exe -o outputFile.txt //將結(jié)果輸出到指定文件outputFile.txt測試文本
命令與結(jié)果
?
2.擴展功能
wc.exe -s //遞歸處理目錄下符合條件的文件 wc.exe -a file.c //返回更復(fù)雜的數(shù)據(jù)(代碼行 / 空行 / 注釋行) wc.exe -e stopList.txt // 停用詞表,統(tǒng)計文件單詞總數(shù)時,不統(tǒng)計該表中的單詞禁用列表
命令與結(jié)果
? ? ?
? ? ?
注:另外兩個功能尚未實現(xiàn)
六、體會
通過編程,更深地體會到需求設(shè)計的重要性;通過測試,更加了解需求與產(chǎn)品之間的差別,以及測試的必要性和重要性。
?
七、參考文獻
?http://www.cnblogs.com/ningjing-zhiyuan/p/8563562.html
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/duwei1996/p/8613783.html
總結(jié)
以上是生活随笔為你收集整理的软测第二周作业WordCount的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ1085: [SCOI2005]
- 下一篇: pytorch的dataset用法详解