WordCount
github项目地址:
https://github.com/sweshixing/WordCount
PSP表格:
PSP2.1 | PSP阶段 | 预计耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 40 | 30 |
.Estimate | .估计这个任务需要多长时间 | 40 | 30 |
Development | 开发 | 840 | 1130 |
.Analysis | .需求分析(包括学习新技术) | 70 | 100 |
.Design Spec | .生成设计文档 | 30 | 30 |
.Design Review | .设计复审(和同事审核设计文档) | 40 | 40 |
.Coding Standard | .代码规范(为目前的开发制定合适的规范) | 30 | 20 |
.Design | .具体设计 | 50 | 60 |
.Coding | .具体编码 | 400 | 500 |
.Code Review | .代码复审 | 100 | 200 |
.Test | .测试(自我修改,修改代码,提交修改) | 120 | 180 |
Reporting | 报告 | 190 | 200 |
.Test Report | .测试报告 | 120 | 120 |
.Size Measurement | .计算工作量 | 30 | 30 |
.Postmortem & Process Improvement Plan | .事后总结,并提出过程改进计划 | 40 | 50 |
合计 | 1070 | 1360 |
解题思路:
刚开始看到题目的时候第一时间想到的是像当初编译实验的时候一样用JavaScript写,但是却倒在了生成exe文件那一步,试过nexe和node-webkit发现并没有达到理想中的效果,所以用Java实现了本次作业。
本次作业实际上和编译实验的词法分析有些类似,识别文件中的单词数、字符数以及行数以及一些拓展功能,并且将Java程序打包成jar,最后转换成exe文件。
程序设计实现过程:
关键代码:
实现基本功能,统计行数、单词数、以及字符数:
while((c=inputfile.read())!=-1){
chars++;
if(c=='\n'){
lines++;
chars=chars-2;
}
if(space.indexOf(c)==-1){
newlist.append((char)c);
}
if(space.indexOf(c)!=-1){
if(last){
words++;
for(int j=0;j<stopwords.size();j++){
if(newlist.toString().equals(stopwords.get(j))) words--;
}
newlist.delete(0, newlist.length());
}
last=false;
lastword=true;
}else{
lastword=false;
last=true;
}
}
while((c=inputfile.read())!=-1){
chars++;
if(c=='\n'){
lines++;
chars=chars-2;
}
if(space.indexOf(c)!=-1){
if(last)
words++;
last=false;
lastword=true;
}else{
lastword=false;
last=true;
}
}
实现统计代码行/注释行/空行:
while((l=freader.readLine())!=null){
isflag=0;
for(int i=0;i<l.length();i++){
if(l.charAt(i)!=' '&&l.charAt(i)!='/'&&isflag==0&&l.charAt(i)!='\t')
{isflag=1;continue;}
if(l.charAt(i)!=' '&&l.charAt(i)!='/'&&isflag==1)
{isflag=10;break;}
if(l.charAt(i)=='/'&&isflag==0)
{isflag=2;continue;}
if(l.charAt(i)=='/'&&isflag==1)
{isflag=2;continue;}
if(l.charAt(i)=='/'&&isflag==2)
{isflag=11;break;}
}
if(isflag==10||isflag==1)
t1++;
else if(isflag==11)
t3++;
else
t2++;
}
写文件函数:
public static void output(String inputfile,String input,int[] use) throws IOException {//将结果输出到txt
File file = new File(inputfile);
FileWriter out = new FileWriter(file,true);
for(int i=0;i<use.length;i++){
if(use[i]==1){
switch(i){
case 0:out.write(input+",字符数:"+chars+"\r\n");break;
case 1:out.write(input+",单词数:"+words+"\r\n");break;
case 2:out.write(input+",行数:"+lines+"\r\n");break;
case 4:out.write(input+",代码行/空行/注释行:"+t1+"/"+t2+"/"+t3+"\r\n");break;
default:
}
}
}
out.close();
}
测试设计过程:
采用白盒测试方法进行测试,一共设计了几个测试用例,如下:
测试-c
wc.exe -c test.c
测试-l
wc.exe -l test.c
测试-w
wc.exe -w test.c
测试-a
wc.exe -a test.c
测试-o
wc.exe -o test.c -o output.txt
测试-e
wc.exe -w test.c -e stop.txt