第二次作业

github:https://github.com/pp807421627/wordC

PSP:

PSP2.1PSP阶段预估耗时 (分钟)实际耗时(分钟)
Planning 计划 15 15
· Estimate · 估计这个任务需要多少时间 15 15
Development 开发 705 1535
· Analysis · 需求分析 (包括学习新技术) 120 600
· Design Spec · 生成设计文档 30 60
· Design Review · 设计复审 (和同事审核设计文档) 0 0
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 15 15
· Design · 具体设计 60 60
· Coding · 具体编码 240 480
· Code Review · 代码复审 120 240
· Test · 测试(自我测试,修改代码,提交修改) 120 80
Reporting 报告 165 165
· Test Report · 测试报告 60 60
· Size Measurement · 计算工作量 45 45
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 60 60
  合计 885 1715

解题思路:

拿到题目,直接的感觉就是这个东西看上去很简单,但想做好的话怕是要花些时间。既然推荐使用java,那就把java看一下,虽然不熟但勉强能用。

然后就到了认真阅读需求分析的阶段,这里是我此次作业花费大量时间的主要原因,读过需求分析之后,感觉很难受,需求的许多边界都没有说请,但是也只有先编码然后等着

明确需求,删删改改越写越难受。

思路很清晰,根据字符,换行,空格,/,/*等关键字符,更新记录属性即可。

代码结构:

解析命令模块->文件读取->内容分析->文件写入

关键代码:

记录参数的表:

// boolean[0]:input -c boolean[0] set true,else set false
// boolean[1]:input -w boolean[0] set true,else set false
// boolean[2]:input -l boolean[0] set true,else set false
// boolean[3]:input -o boolean[0] set true,else set false
// boolean[4]:input -s boolean[0] set true,else set false
// boolean[5]:input -a boolean[0] set true,else set false
// boolean[6]:input -e boolean[0] set true,else set false
boolean[] parameter = {false, false, false, false, false, false, false,};

 

命令解析:

Cmd(String[] args){
if(args.length == 0) {
throw new IllegalArgumentException("please input args(parameter and file)");
}

// analysis command
for(int i = 0; i < args.length; i++) {
char[] tmp = args[i].toCharArray();
// parameter
if(tmp[0] == '-') {
switch(tmp[1]) {
case 'c': parameter[0] = true; break;
case 'w': parameter[1] = true; break;
case 'l': parameter[2] = true; break;
case 'o': {
parameter[3] = true;
outputFileFlag = true;
break;
}
case 's': {
parameter[4] = true;
TPFlag = true;
break;
}
case 'a': parameter[5] = true; break;
case 'e':{
parameter[6] = true;
StopFileFlag = true;
break;
}
default : throw new IllegalArgumentException("parameter beyond 'c w l o s a e'");
}
// store filename
}else if(tmp[0] != '-') {
if(outputFileFlag == true) {
outputFile = args[i];
outputFileFlag = false;

}else if(TPFlag == true){
TPd = args[i];
TPFlag = false;

}else if(StopFileFlag == true){
StopFileName = args[i];
StopFileFlag = false;

}else {
filename = args[i];
}
}
}

读文件:

// read
StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(mCmd.getFilename()));
String s = null;
while((s = br.readLine())!=null){
result.append(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}

 

分析内容:

// count
int charNum = 0;
int wordNum = 0;
int lineNum = 0;
int codeLine = 0;
int notesLine = 0;
int blankLine = 0;
boolean spaceFlag = false;
boolean singleNotesFlag = false;
boolean notesFlag = false;
for(int i = 0; i < content.length(); i++) {
// count char
charNum++;
// count word
if((content.toCharArray())[i] == ' ') {
spaceFlag = true;
}else {
if(spaceFlag == true) {
wordNum++;
spaceFlag = false;
}
}

// count line
if((content.toCharArray())[i] == '\n') {
lineNum++;
singleNotesFlag = false;
if(notesFlag == true) {
notesLine++;
// count codeLine
}else {
codeLine++;
}
}


// count notesLine
if((content.toCharArray())[i] == '\\' &&
(content.toCharArray())[i] == '\\') {
if(singleNotesFlag == false) {
notesLine++;
singleNotesFlag = true;
}
}
if((content.toCharArray())[i] == '\\' &&
(content.toCharArray())[i] == '*') {
notesFlag = true;
}


// count blankLine
if((content.toCharArray())[i] == '\n' &&
(content.toCharArray())[i + 1] == '\r' &&
(content.toCharArray())[i + 2] == '\n') {
blankLine++;
}
}

递归处理目录:

private static List<String> getAllFilePaths(File filePath,List<String> filePaths, String targetFile){
File[] files = filePath.listFiles();
if(files == null){
return filePaths;
}
for(File f:files){
if(f.isDirectory()){
filePaths.add(f.getPath());
getAllFilePaths(f,filePaths, targetFile);
}else{
if(wildcardMatch(f.getPath(), targetFile ))
filePaths.add(f.getPath());
}
}
return filePaths;
}

简单通配符匹配:

private static boolean wildcardMatch(String pattern, String str) {
int patternLength = pattern.length();
int strLength = str.length();
int strIndex = 0;
char ch;
for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
ch = pattern.charAt(patternIndex);
if (ch == '*') {
// *
while (strIndex < strLength) {
if (wildcardMatch(pattern.substring(patternIndex + 1),
str.substring(strIndex))) {
return true;
}
strIndex++;
}
} else if (ch == '?') {
// ?
strIndex++;
if (strIndex > strLength) {
return false;
}
} else {
if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {
return false;
}
strIndex++;
}
}
return (strIndex == strLength);
}

写文件:

 

// write to outputFile
File fout = new File(mCmd.outputFile);
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(mCmd.getFilename() + ": charNum " + charNum);
bw.newLine();
bw.write(mCmd.getFilename() + ": wordNum" + wordNum);
bw.newLine();
bw.write(mCmd.getFilename() + ": lineNum" + lineNum);
bw.newLine();
if(mCmd.parameter[5] == true) {
bw.write(mCmd.getFilename() + ": notesLine " + notesLine);
bw.newLine();
bw.write(mCmd.getFilename() + ": codeLine" + codeLine);
bw.newLine();
bw.write(mCmd.getFilename() + ": blankLine " + blankLine);
bw.newLine();
}
bw.close();

 

设计测试过程

 

整体结构

 

 

计数过程

计数过程不跟据输入的参数决定统计哪些项,所有的项总是统计的,只是会根据参数输入不同决定输出哪些项。

此处线性结构。

 

设计测试用例

文件hello.c,test.c,stopList.txt

hello.c:

#include <stdio.h>

test.c:

/*

* a test file

*/

#include <stdio.h>

int main(int argc, char *argv)

{

    // int i;

    int i = 0;

    // test -w 

   word1,word2;

   word1 word2    ,     , ,,  ;

    // test -l

    \n\r \n \r

    // test -a  

 {//  

 }//

 //{

 //}

 /*

    int i //

    //////////////////////

    

int i;

*/

 

 

    printf("........");

}

stopList.txt:

if

 

测试用例

wc.exe -c notExistfile.c

wc.exe -c hello.c  output.txt -o

wc.exe -c hello.c stopList.txt -e

wc.exe -c -w -l -s *.c -e stopList.txt -o output.txt       A->B->D->G->H->G->I->J->I->K

wc.exe -s *.c -o output.txt                              A->C->D->G->H->G->I->J->I->K

wc.exe -c -w -l hello.c                                    A->C->D->E->F

wc.exe -c hello.c -o output.txt                         A->B->D->E->F

wc.exe -a hello.c -o output.txt                         A->B->D->E->F 

wc.exe -w  hello.c -e stopList.txt -o output.txt  A->C->D->E->F

wc.exe -c -w -l test.c -e stopList.txt -o output.txt  A->C->D->E->F

 

 

 

 参考文献:

http://blog.csdn.net/Laichilueng/article/details/71128494

http://sunlujing.iteye.com/blog/1706919

 

总结:

作业和面试宣讲会还有一些找工作的准备事情,在时间上产生了极大的冲突,导致时间不足,代码一顿瞎写,代码写完看哪个代码写的,丑到炸,如果可以真不想往github上传,自我检讨...这代码真的是写的太渣了。

希望下次事情不会赶到一起。

posted @ 2018-03-22 16:33  sajds  阅读(137)  评论(2)    收藏  举报