WordCount

WordCount

github项目地址:

https://github.com/sweshixing/WordCount

PSP表格:

PSP2.1PSP阶段预计耗时(分钟)实际耗时(分钟)
Planning计划4030
.Estimate.估计这个任务需要多长时间4030
Development开发8401130
.Analysis.需求分析(包括学习新技术)70100
.Design Spec.生成设计文档3030
.Design Review.设计复审(和同事审核设计文档)4040
.Coding Standard.代码规范(为目前的开发制定合适的规范)3020
.Design.具体设计5060
.Coding.具体编码400500
.Code Review.代码复审100200
.Test.测试(自我修改,修改代码,提交修改)120180
Reporting报告190200
.Test Report.测试报告120120
.Size Measurement.计算工作量3030
.Postmortem & Process Improvement Plan.事后总结,并提出过程改进计划4050
合计10701360

解题思路:

刚开始看到题目的时候第一时间想到的是像当初编译实验的时候一样用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

参考文献链接:

转载于:https://www.cnblogs.com/Shi-Xing/p/8630806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值