第二周个人作业WordCount总结

一.GitHub作业地址:

https://github.com/ldisthebest/WordCount

二.PSP表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

40 60

· Estimate

· 估计这个任务需要多少时间

840 1070

Development

开发

720 950

· Analysis

· 需求分析 (包括学习新技术)

120 180

· Design Spec

· 生成设计文档

- -

· Design Review

· 设计复审 (和同事审核设计文档)

- -

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

30 30

· Design

· 具体设计

60 60

· Coding

· 具体编码

360 420

· Code Review

· 代码复审

30 60

· Test

· 测试(自我测试,修改代码,提交修改)

120 200

Reporting

报告

120 120

· Test Report

· 测试报告

60  60

· Size Measurement

· 计算工作量

30 30

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

30  30
 

合计

880 1130

三.解题思路

根据本次作业要求,我的大概思路是:输入命令,检测命令,根据命令处理文件(包括读取文件、写入文件)。根据这三步来贯穿整个程序,层次分明,依次递进。在每步都进行完全考虑各种情况,最终实现整个内容。

四.部分代码说明

1.获取指令并判断

 1 Boolean InputBaseInstruction(String[] str) throws IOException{
 2         
 3         for(int strCount = 0;strCount<str.length;strCount++){
 4             keyWord.add(str[strCount]);
 5         }
 6         if(!((String)keyWord.get(0)).equals("wc.exe")){
 7             InstruError();
 8             return false;
 9         }
10         for(int i = 1;i<keyWord.size();i++){
11             if(IsSpecialInstru((String)keyWord.get(i))&&i!=1){
12                 InstruError();
13                 return false;
14             }            
15             if(IsNormalInstru((String)keyWord.get(i))||IsSpecialInstru((String)keyWord.get(i))){
16                 instru.add((String)keyWord.get(i));
17                 if(IsSpecialInstru((String)keyWord.get(i))){
18                     isListStyle = true;
19                 }
20             }
21             else if(IsInstruOfStop((String)keyWord.get(i))){
22                 instru.add((String)keyWord.get(i));
23                 if(++i>keyWord.size()-1){
24                     InstruError();
25                     return false;
26                 }
27                 stopFile = (String)keyWord.get(i);
28             }
29             else if(IsInstruOfOut((String)keyWord.get(i))){
30                 instru.add((String)keyWord.get(i));
31                 if(++i>keyWord.size()-1){
32                     InstruError();
33                     return false;
34                 }
35                 outFile = (String)keyWord.get(i);
36             }
37             else{
38                 //String filePath = (String)keyWord.get(i);
39                 for(int index = 0;index<keyWord.get(i).toString().length();index++){
40                     if(keyWord.get(i).toString().charAt(index) == '*'){
41                         listPath = GetListPath((String)keyWord.get(i));
42                         break;
43                     }
44                 }    
45                 if(listPath.equals("")){
46                     listFiles.add((String)keyWord.get(i));
47                 }
48             }
49         }
50 
51         return true;
52     }

2.找出字符数量

 1 String FindCharacterNum(){
 2         String str = "";
 3         for(int i = 0;i<fileWord.size();i++){
 4             char character[] = fileWord.get(i).toString().toCharArray();
 5             int num = 0;
 6             for(int j = 0;j<character.length;j++){
 7                 if(character[j] != '\r')
 8                     num ++;
 9             }
10             str += listFiles.get(i).toString()+",字符数:"+num+"\n";
11         }
12         
13         return str;
14     }

3.找出代码行/注释行/空行:

String FindlinesInfo(){
        String str = "";
        for(int count = 0;count<fileWord.size();count++){
            int codeLine = 0,emptyLine = 0,noteLine = 0;
            Boolean isNewLine = true,isNoteAfterCode = false;
            char chr[] = fileWord.get(count).toString().toCharArray();
            int i;
            for(i = 0;i<chr.length;i++){
                if(i+1<chr.length&&chr[i] == '/' && chr[i+1] == '/'){
                    if(isNewLine)
                    noteLine ++;
                    while(i<chr.length&&chr[i] != '\n'){
                        i++;
                    }
                    isNewLine = true;
                }
                else if(i+1<chr.length&&chr[i] == '/' && chr[i+1] == '*'){
                    i += 2;
                    while(i<chr.length&&(chr[i] != '*' || chr[i+1] != '/')){
                        if(chr[i] == '\n'){
                            if(isNoteAfterCode){
                                isNoteAfterCode = false;
                            }
                            else
                            noteLine ++;
                            isNewLine = true;
                        }
                        i++;
                    }
                    i++;
                    if(1+i<chr.length&&chr[i+1] != '\r'){
                        isNewLine = false;
                    }
                    else{
                        noteLine++;
                        i += 2;
                        isNewLine = true;
                    }
                        
                }
                else if(i<chr.length-1&&chr[i] != ' ' && chr[i+1] != ' ' && chr[i] != '\r' && chr[i+1] != '\r'){
                    if(!isNoteAfterCode)
                    codeLine++;
                    while(i<chr.length&&chr[i] != '\n'){                    
                        if(i<chr.length-1&&chr[i] == '/'&&chr[i+1] == '*'){
                            isNoteAfterCode = true;
                            i--;
                            break;
                        }
                        i++;
                    }
                    if(i<chr.length&&chr[i] == '\n')
                    isNewLine = true;
                }
                else{
                    if(i<chr.length-1&&chr[i] != '\r'&&chr[i+1] != '\r'){
                        i += 2;
                    }
                    else if(i<chr.length&&chr[i] != '\r'){
                        i++;
                    }
                    while(i<chr.length&&chr[i] == ' '){
                        i++;
                    }
                    if(i<chr.length&&chr[i] == '\r'){
                        if(!isNewLine){
                            noteLine++;
                        }
                        else
                            emptyLine++;
                        isNewLine =true;
                        i++;
                    }
                    else if( i == chr.length){
                        if(!isNewLine){
                            noteLine++;
                        }
                        else
                            emptyLine++;
                    }
                }
                
            }
            if(i == chr.length && chr[i - 1] == '\n'){
                emptyLine++;
            }
            str += listFiles.get(count).toString()+",代码行/空行/注释行:"+codeLine+"/"+emptyLine+"/"+noteLine+"\n";
        }        
        return str;
    }

4.递归遍历得到文件序列:

 

 1 void GetFileList(String path){
 2         if(!isListStyle && !listPath.equals("")){
 3             System.out.println("没有-s指令,错误!");
 4             return;
 5         }
 6         File file = new File(path);
 7         if (file.exists()) {
 8             File[] files = file.listFiles();
 9             if (files.length == 0) {
10                 //System.out.println("文件夹是空的!");
11                 return;
12             } 
13             else {
14                 for (File file2 : files) {
15                     if (file2.isDirectory()) {
16                         GetFileList(file2.getAbsolutePath());
17                     } 
18                     else {
19                         
20                         String tailName = file2.getAbsolutePath().substring(file2.getAbsolutePath().indexOf('.'), file2.getAbsolutePath().length());
21                         if(tailName.equals(/*listPath.substring(listPath.indexOf('.'), listPath.length())*/".c")){
22                             listFiles.add(file2.getAbsolutePath());
23                         }
24                     }
25                 }
26             }
27         } 
28         else {
29             System.out.println("文件不存在!");
30         }
31 
32     }

 

 五.测试用例

1.基础功能测试

1) 测试用例1输入指令:wc.exe -w -c -l test1.c -o result.txt

test1.c文件内容:

int main(){
	int a = b;
}

result.txt输出内容:

test1.c,单词数:7
test1.c,字符数:25
test1.c,总行数:3

 2)测试用例2输入指令:wc.exe -w -c -l test2.c -o result.txt

test2.c文件内容:

ddad das,ada
adq 
w
12 sda*,das

result.txt输出内容:

test2.c,单词数:8
test2.c,字符数:31
test2.c,总行数:4

 3)测试用例3输入指令:wc.exe -w -c -l test3.c -o result.txt

test3.c文件内容:

!@#$%^ &*()_+\t{}|"?><[]\;',.
/~`asdas
asda 
int as
a

 result.txt输出内容:

test3.c,单词数:8
test3.c,字符数:53
test3.c,总行数:5

2.拓展功能测试用例

1)测试用例1输入指令:wc.exe -a  -w  test.c -o result.txt

test.c内容:

#include<stdio.h>
int main()
{
    //shengming
    int a = 0;/*chushihua
    */
    int c = a;
    printf("%d",c);
}

result.txt文件内容:

test.c,代码行/空行/注释行:5/2/2
test.c,单词数:17

2)测试用例2输入内容为:wc.exe -e stopList.txt -w  test.c -o result.txt

test.c文件内容同上;

stopList.txt文件内容:

main a c =

 

result.txt文件内容:

test.c,单词数:12

3)测试用例2输入内容为:wc.exe -s -w F:\softwareQuality&Test\WordCount\bin\*.c -o result.txt

result.txt文件内容如下:

F:\softwareQuality&Test\WordCount\bin\test.c,单词数:17
F:\softwareQuality&Test\WordCount\bin\tests\new\test3.c,单词数:21
F:\softwareQuality&Test\WordCount\bin\tests\test1.c,单词数:4
F:\softwareQuality&Test\WordCount\bin\tests\test2.c,单词数:9

六.参考文献

a:作业内容与要求 http://www.cnblogs.com/llag9810/p/8604262.html

b:文件的读写操作 https://www.yiibai.com/java_io/java_nio_files_content.html

c:java中split函数的用法 https://jingyan.baidu.com/article/9989c746c5d707f648ecfe05.html

d:判断字符串中单词个数 https://www.cnblogs.com/gccbuaa/p/7097332.html

e:jar转exe http://blog.csdn.net/sunkun2013/article/details/13167099

f:测试要求 https://elearning.hust.edu.cn/webapps/blackboard/content/listContent.jsp?content_id=_41037_1&course_id=_16233_1&mode=view#_41137_1

posted @ 2018-03-21 22:04  罗迪  阅读(234)  评论(0编辑  收藏  举报