系统分析与设计——WordCount

成果:

  https://gitee.com/ZMLJZ/codes/0k19q4upgmrfde265l7vx36

作业要求:

  根据WordCount的需求描述,先编程实现,再编写单元测试,最后撰写博客。每个同学独立完成,至少实现以下需求说明中的基本功能。

WordCount需求说明:

  WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。

基本功能:

  wc.exe -c file.c     //返回文件 file.c 的字符数

  wc.exe -w file.c     //返回文件 file.c 的单词总数

  wc.exe -l file.c     //返回文件 file.c 的总行数

  wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

PSP表格:

P2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 10

 10

Estimate

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

 15

 20

Development

开发

 120

 100

· Analysis

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

 5

 6

· Design Spec

· 生成设计文档

 5

· Design Review

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

 5

· Coding Standard

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

 10

· Design

· 具体设计

 15

10 

· Coding

· 具体编码

 120

150 

· Code Review

· 代码复审

 20

15 

· Test

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

 20

20 

Reporting

报告

 10

10 

· Test Report

· 测试报告

10 

15 

· Size Measurement

· 计算工作量

10 

· Postmortem & Process Improvement Plan

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

20 

15 

 

合计

 405

 395

 

解题思路:

  读取文件,然后根据逗号,空格,等符号将文件内容分离,遍历后将统计结果存入Count对象内,然后将统计结果写入txt文件。

  最后将java代码转化成exe文件。

 

类图:

      

代码说明:

  读取C语言文件,创建WordCount类并将文件地址传入

        //读入test.c文件
        String inputFile = "D:\\test\\test.c";
        WordCount wc = new WordCount();
        wc.doCount(inputFile);            

   根据传入的C语言文件进行处理,并统计字符数,单词数和行数,写入Count对象

  public void doCount(String inputFile) throws IOException {
        String txt = "";
        String[] buffer;
        File dir = new File(inputFile);
        BufferedReader bf = new BufferedReader( new FileReader(dir) );
        while( (txt = bf.readLine()) != null ){
            buffer = txt.split(", |  |\t |\n");//根据字符切分
            for(int i = 0 ; i < buffer.length ; i++){
                if( !buffer[i].equals(""))
                    count.setWordNumber( count.getWordNumber()+1 );//统计单词数
            }
            count.setLineNumber( count.getLineNumber()+1 );//统计行数
            count.setCharNumber( count.getCharNumber() + txt.length() );//统计字符数
        }
        bf.close();
    }

  根据Count内存的统计结果,将结果写入txt文件

        //将结果写入output.txt
        File resultFile = new File("D:\\test\\output.txt");
        resultFile.createNewFile();
        
        BufferedWriter out = new BufferedWriter( new FileWriter(resultFile) );
        out.write("字符数:"+wc.getCount().getCharNumber());
        out.newLine();
        out.write("单词数:"+wc.getCount().getWordNumber());
        out.newLine();
        out.write("行数:"+wc.getCount().getLineNumber()); 
        
        out.flush(); 
        out.close(); 

 

测试实际过程:(用例+运行结果截图)

 用例一:

 

用例二:

 

用例三:

 

 

 用例四:

 

用例五:

 

用例六:

 

用例七:

 

用例八:

 

用例九:

 

 

用例十:

 

参考文献连接:https://blog.csdn.net/qq_28718481/article/details/78848191

 

posted on 2018-09-24 15:00  谨之  阅读(199)  评论(0编辑  收藏  举报

导航