1. 程式人生 > >軟件測試第二周個人作業WordCount程序實現

軟件測試第二周個人作業WordCount程序實現

需要 讀寫 odin getprop porting match out enter 根據

GitHub地址:https://github.com/Guchencc/WordCount

一.PSP表格

PSP2.1

PSP階段

預估耗時

(分鐘)

實際耗時

(分鐘)

Planning

計劃

· Estimate

· 估計這個任務需要多少時間

300

440

Development

開發

· Analysis

· 需求分析 (包括學習新技術)

20

30

· Design Spec

· 生成設計文檔

10

10

· Design Review

· 設計復審

(和同事審核設計文檔)

10

10

· Coding Standard

· 代碼規範 (為目前的開發制定合適的規範)

10

10

· Design

· 具體設計

20

30

· Coding

· 具體編碼

120

240

· Code Review

· 代碼復審

10

30

· Test

· 測試(自我測試,修改代碼,提交修改)

20

30

Reporting

報告

20

20

· Test Report

· 測試報告

10

10

· Size Measurement

· 計算工作量

10

10

· Postmortem & Process Improvement Plan

· 事後總結, 並提出過程改進計劃

10

10

合計

270

440

二、解題思路

  了解完題目後,首先回顧了java與文件讀寫以及目錄文件查詢等相關函數。改程序是輸入命令行然後進行相關操作的,那麽首先就需要解析作為主函數參數傳入的命令行參數,解析過程中需要區分文件path和命令以及登記相關變量的值,解析完後對變量filelist中的文件進行計數,大體思路如此。

三、程序設計實現過程

  本程序實現只用了一個主類WordCount類,其屬性如下:

  private String filename; //當前計數處理的文件名

  private int charcount=0;//字符計數器

  private int wordcount=0;//單詞計數器

  private int linecount=0;//行計數器

  private int codeLinecount=0;//代碼行計數器

  private int blankLinecount=0;//空行計數器

  private int commentLinecount=0;//註釋行計數器

  private ArrayList<String> optlist=new ArrayList<>();//存儲輸入的命令

  private ArrayList<String> stoplist=new ArrayList<>();//存儲讀取的停用詞

  private ArrayList<String> filelist=new ArrayList<>();//存儲輸入的計數文件

  private String suffix;//存儲輸入的通配符後綴

  private String outFileName;//存儲輸入的輸出文件名

  private String stopFileName;////存儲當前處理的的停用詞文件名

  相關函數如下:

  main()為程序主函數。

  CommandParser(String[ ] args)函數接受來自主函數的args數組,解析命令行參數並給WordCount類中的相關屬性賦值。

  Count()函數根據解析後的屬性賦值進行計數操作,它會計算所有filelist動態數組中的文件,並根據oplist動態數組中的命令選擇輸出項。

  outprint()函數根據oplist動態數組中的命令選擇數據進行文本輸出

  resetcount()函數重置各類計數器

  readStopFile()讀取輸入的停用詞文件中的停用詞並添加進stoplist動態數組

  findAllFiles()函數遞歸處理輸入的文件路徑(包含*.XXX)獲取所有以XXX為文件格式的文件路徑,並添加進filelist動態數組。

四、代碼說明

  

public void CommandParser(String[] args) { //命令行參數解析,
for(int i=0;i<args.length;i++){
args[i]=args[i].toLowerCase();
if (args[i].equals("-c")||args[i].equals("-l")||args[i].equals("-w")||args[i].equals("-a")||args[i].equals("-s")) //將計數命令直接添加進oplist
optlist.add(args[i]);
else if (args[i].equals("-e")){ //如果輸入的命令包含-e 則後面必定是停用詞文件路徑
i++;
if (args.length>i) {
if (args[i].equals("-o") || args[i].equals("\n")) {
System.out.println("未輸入停用單詞表文件名!");
return;
}
}else {
System.out.println("未輸入停用單詞表文件名!");
return;
}
stopFileName=args[i];
optlist.add(args[--i]);
readStopFile();
}
else if (args[i].equals("-o")){ //如果輸入的命令包含-o 則後面必定是輸出文件路徑
if(++i==args.length) {
System.out.println("未輸入輸出文件名!");
return;
}
outFileName=args[i];
optlist.add(args[--i]);
}
else if (args[i].equals(outFileName)||args[i].equals(stopFileName));
else if (optlist.contains("-s") && args[i].matches(".*\\*[.](txt|c|py|java|cpp)$")){ //根據正則表達式匹配輸入的含有通配符的路徑
String root="";
suffix=args[i].substring(args[i].indexOf(‘.‘)+1,args[i].length()); //獲取想要獲取的文件後綴名
root=args[i].replaceAll("\\*[.](txt|c|py|java|cpp)$",""); //獲取想要查找含有通配符格式的文件根目錄
if (root.length()<1) //如果沒有指定目錄則從當前目錄開始查找
root=System.getProperty("user.dir");
findAllFiles(root);//查找匹配通配符格式的所有文件
}
else if (!optlist.contains("-s")) //如果輸入的命令行中無-s 則輸入文件名不包含通配符,而是具體的文件路徑,直接添加進filelist中
filelist.add(args[i]);
}
}


public void Count() throws Exception {
String str="";
boolean isstop=false;
for (int i=0;i<filelist.size();i++){ //遍歷待統計文件數組,並根據相關名詞定義進行計數
String path=filelist.get(i);
filename=path.substring(path.lastIndexOf(‘\\‘)+1,path.length());
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(filelist.get(i))));
while((str=br.readLine())!=null) {
if (str.trim().replaceAll("(//.*|/\\*.*\\*/|/\\*.*|\\*/|\\{|\\})","").length()>1)
codeLinecount++;
if (str.trim().matches("^(//|/\\*).*") || str.trim().matches("^[!-~](//.*|/\\*.*\\*/)") || str.trim().matches("\\*/")){
commentLinecount++;
}
if(str.matches("\\s*") || (str.trim().length()==1 && (str.trim().charAt(0)>0x21 && str.trim().charAt(0)<0x7F)))
blankLinecount++;

linecount++;
charcount+=str.length();
String[] words=str.trim().split("\\s|,");
if (optlist.contains("-e")){ //如果命令包含-e 則遍歷單詞,若遇到與stoplist中相同的單詞則不進行word計數
for(String word:words) {
for (String stopword : stoplist) {
if (word.equals(stopword))
isstop = true;
}
if (!isstop && !word.equals(""))
wordcount++;
isstop = false;
}
}
else {
for (String word:words)
if (!word.equals("")) //如果命令不包含-e 則直接對單詞計數
wordcount++;
}
isstop=false;
}
charcount=charcount+linecount-1; // \r\t

if (optlist.contains("-c")){ //根據oplist中的統計命令選擇輸出結果
System.out.println(filename+","+"字符數:"+charcount);
}
if (optlist.contains("-w")){
System.out.println(filename+","+"單詞數:"+wordcount);
}
if (optlist.contains("-l")){
System.out.println(filename+","+"行數:"+linecount);
}
if (optlist.contains("-a")){
System.out.println(filename+","+"代碼行/空行/註釋行:"+codeLinecount+"/"+blankLinecount+"/"+commentLinecount);
}
outprint(); //記錄統計結果
resetCount(); //重置計數器
}
}

public void outprint(){
File file=null;
String str="";
if (!optlist.contains("-c")&&!optlist.contains("-w")&&!optlist.contains("-l")&&!optlist.contains("-a")){
System.out.println("無統計操作,無輸出項!");
return;
}
if(optlist.contains("-o") && outFileName!=null)
file = new File(outFileName); //如果指定輸出文件路徑,則在相應路徑輸出統計結果
else
file = new File("result.txt"); //否則在當前路徑下的result.txt文件中輸出。
try{
FileWriter fw=new FileWriter(file,true);
PrintWriter pw=new PrintWriter(fw);
if(!file.exists()){
file.createNewFile();
}
if (optlist.contains("-c")) //根據oplist中的命令選擇記錄統計結果
str+=filename+","+"字符數:"+charcount+"\r\n";
if (optlist.contains("-w"))
str+=filename+","+"單詞數:"+wordcount+"\r\n";
if (optlist.contains("-l"))
str+=filename+","+"行數:"+linecount+"\r\n";
if (optlist.contains("-a"))
str+=filename+","+"代碼行/空行/註釋行:"+codeLinecount+"/"+blankLinecount+"/"+commentLinecount+"\r\n";
pw.write(str);
pw.close();
fw.close();
}catch (Exception e){
System.out.println("輸出文件失敗!");
}
}

public void readStopFile(){
String str="";
String[] stopwords;
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(stopFileName)));
while ((str = bf.readLine()) != null) { //遍歷輸入的停用詞文件並登記停用詞
stopwords = str.trim().split("\\s");
Collections.addAll(stoplist, stopwords);
}
}catch (Exception e){
System.out.println("讀取停用詞表錯誤!");
}
}

public void findAllFiles(String path){
File file=new File(path);
if (!file.isDirectory()){
String filename=file.getName();
if (filename.substring(filename.indexOf(‘.‘)+1,filename.length()).equals(suffix)) //將文件後綴與提取的通配符後綴比較,若相同則將該文件路徑加入filelist
filelist.add(file.getAbsolutePath());http://blog.csdn.net/zamamiro/article/details/70172900

} else if (file.isDirectory()){
for (File f:file.listFiles())
findAllFiles(f.getAbsolutePath()); //遞歸遍歷文件夾與文件
}
}

五、測試設計過程

還未完成

六、參考文獻鏈接

https://baike.baidu.com/item/正則表達式/1700215?fr=aladdin

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

http://blog.csdn.net/zamamiro/article/details/70172900

http://blog.csdn.net/honjane/article/details/40739337

軟件測試第二周個人作業WordCount程序實現