第2周个人作业:WordCount

1  GitHub address:

https://github.com/flyxie/WordCount

2  PSP:

 

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 10  10 

· Estimate

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

10  10

Development

开发

 690  840

· Analysis

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

 60  120

· Design Spec

· 生成设计文档

 -  -

· Design Review

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

 -  -

· Coding Standard

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

 10  10

· Design

· 具体设计

 30  30

· Coding

· 具体编码

 500  600

· Code Review

· 代码复审

 30  20

· Test

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

 60  60

Reporting

报告

 70  70

· Test Report

· 测试报告

 30  30

· Size Measurement

· 计算工作量

 10  10

· Postmortem & Process Improvement Plan

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

 30  30
 

合计

 770  920

 

3  解题思路:

(1)学习git和GitHub的使用:

参考资料:

Git教程 - 廖雪峰的官方网站

Git和Github简单教程 - schaepher - 博客园

GitHub团队项目合作流程 - schaepher - 博客园

(2)思考如何实现对源程序的识别与其中字符数,行数等的判断

输入命令,识别命令类型,读取文件,执行命令,输出结果

 

4  程序设计实现过程与代码说明:

(1)识别命令类型

 调用args[i].equals() 函数,判断参数类型,调用相应的方法

//循环读取参数args
            for(int i=0;i<last;i++)
                if(args[i].equals("-c")) 
                    {
                        tmp = new String(wc.countCharacter(fileName));
                        System.out.println(tmp);
                        sList.add(tmp);
                    }
                else if(args[i].equals("-w"))
                {
                    tmp = new String(wc.countWord(fileName));
                    System.out.println(tmp);
                    sList.add(tmp);
                }
                else if(args[i].equals("-l"))
                {
                    tmp = new String(wc.countLine(fileName));
                    System.out.println(tmp);
                    sList.add(tmp);
                }
                else if(args[i].equals("-a"))
                {
                    tmp = new String(wc.countA(fileName));
                    System.out.println(tmp);
                    sList.add(tmp);
                }

 

 

(2)识别单词数

 使用BufferedReader 从字符输入流(文件)中读取文本,缓冲各个字符,从而实现字符、数组和行的读取

把源文件每行的字符放入String str,调用length()计算每行字符数,累加到count中

    public String countCharacter(String fileName)
    {
        int count=0;
        String str;
        int c;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while((str=in.readLine())!=null)
                count+=str.length();
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName+" 字符数为:"+count;
    }

(3)识别单词

BufferedReader ,然后用Split()方法提取字符分隔符,把str字符串分割为多个子字符串并存入ss中,再用length计数累加

    public String countWord(String fileName)
    {
        int count=0;
        String str;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while((str = in.readLine())!=null)
            {
                if(!str.isEmpty())
                    {
                        String ss []  = str.trim().split(" |,|    ");
                        count+=ss.length;
                    }
            }
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName+" 单词总数:"+count;
    }

 

(4)识别行数

BufferedReader,然后若该行字符串不为空则count++

    public String countLine(String fileName)
    {
        int count=0;
        String s;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while((s = in.readLine())!=null)
                ++count;
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("请检查文件路径是否正确");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName+" 总行数:"+count;
    }

 

(5)识别代码行 / 空行 / 注释行

BufferedReader,然后在读取每一行时判断该行的类型,

若该行为空或只有空格和格式控制字符或只有不超过一个可显示的字符,则为空行,++space;

若该行可显示的字符以“//”或“}//”为开头,则为注释行;

否则为代码行;

    public String countA(String fileName)
    {
        int count=0,space=0,h=0;
        String s;
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while((s = in.readLine())!=null)
            {
                if(s.isEmpty() || s.trim().length()<=1) ++space;
                else
                {
                    int t=0;
                    while(t<s.length()&&s.charAt(t)==' ') ++t;
                    if((t+1<s.length() && s.charAt(t)=='/' && s.charAt(t+1)=='/')
                            || (t+2<s.length() && s.charAt(t+1)=='/' && s.charAt(t+2)=='/')) ++h;
                    else
                        ++count;
                }
            }
            in.close();
        } catch (FileNotFoundException e1) {
            System.out.println("找不到文件!!!");
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName+" 代码行数/空行数/注释行数:"+count+"/"+space+"/"+h;
    }

 

6 测试设计

(1)对已知需求的测试

wc.exe -c test.txt

wc.exe -w test.txt

wc.exe -l test.txt

wc.exe -a test.txt

wc.exe -c test.txt -o outputFile.txt

(2)对多参数输入的测试

wc.exe -c -w test.txt

wc.exe -c -l test.txt

wc.exe -c -a test.txt

wc.exe -w -l test.txt

wc.exe -w -a test.txt

wc.exe -l -a test.txt

wc.exe -c -w -l test.txt

wc.exe -c -w -l -a test.txt

wc.exe -c test.txt -o outputFile.txt

7  参考文献

 

第2周个人作业:WordCount - wujianjie - 博客园

cherylwu / WCProject

第2周 白盒测试 – 软件质量与测试(2018春季软工1501-03班)

上载作业: 第2周个人作业:WordCount编码和测试 – 软件质量与测试(2018春季软工1501-03班)

8  参考代码

java文件操作代码片断实例实现统计文件中字母出现的个数功能

王志峰、谢良才等同学代码

 

posted @ 2018-03-23 23:36  xiefly  阅读(234)  评论(6编辑  收藏  举报