word count

1、https://github.com/ahhahahh/word-count

2、PSP

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

 

 

Planning

计划

 30

 30

 

 

· Estimate

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

 10

 30

 

 

Development

开发

 180

 360

 

 

· Analysis

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

 60

 120

 

 

· Design Spec

· 生成设计文档

 30

 60

 

 

· Design Review

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

 30

 30

 

 

· Coding Standard

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

 40

 60

 

 

· Design

· 具体设计

 60

 60

 

 

· Coding

· 具体编码

 360

 720

 

 

· Code Review

· 代码复审

 60

 90

 

 

· Test

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

 180

 180

 

 

Reporting

报告

 60

 60

 

 

· Test Report

· 测试报告

 35

 30

 

 

· Size Measurement

· 计算工作量

 25

 30

 

 

· Postmortem & Process Improvement Plan

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

 60

 80

 

 

 

合计

 995

 1940

 

 

(3)描述解题思路      

刚拿到题目时觉得有点拿难度,首先自己没有系统的学过java,以前喜欢应c写东西,而且看到作业要求特别多,特别繁琐,不过还是硬着头皮做了下来。首先对单词进行计数,这样的问题以前遇到过,也编写过,觉得应该没有问题。这次作业分成三步,首先完成基础功能,然后一步步进行增量开发,完成扩展功能和附加功能。首先查阅了相应的资料,看了如下资料:

    有关PSP的内容:http://www.cnblogs.com/xinz/archive/2011/10/22/2220872.html。

    a. 手把手教你如何把jar文件,打包成jar文件以及转换为exe可执行文件;

    b. java打包成可执行的jar或者exe的详细步骤。

   Word Count in Java language, 三种不同的方法 

在这些资料中开始了解了psp的记录方式,文件打包,有关单词计数的基本方法。

在程序设计过程中,设计了一下几个类:

 (4)程序设计实现过程

首先是 basecount 类,用于计数单词;这里有 charcount(字符计数)wordcount(单词计数)、linecount(行计数)、print(输入到指定文件夹)三个主要函数。

  处理同目录下的文件extendedFun类;包括主要的统计代码行、注释行函数,停用计数函数两个主要函数。main函数整个程序流程的管控,调用解析参数函、计数类来统计内容、最后打印输出。

(5)代码说明  这部分核心代码是在1504班成建伟帮助下完成,有部分引用,自己修改完成,。

public class casecount {

    static  public void count(String action,String thefile){

      //计算字符数

      if(action.equalsIgnoreCase("-c"))

       charcount(thefile);

      else if(action.equals("-w"))

       wordcount(thefile);

      else if(action.equals("-l"))

       linecount(thefile);

     }

    static public void count(String action1,String action2,String thefile){

      count(action1,thefile);

      count(action2,thefile);

    }

    static public void count(String action1,String action2,String action3,String thefile){

     if(thefile.endsWith(".c")){

       count(action1,thefile);

       count(action2,thefile);

       count(action3,thefile);

       }

     else{

      print(action1,action2,thefile);

     }

   }

 

二、统计字符数、单词数和行数

public class basecount {

 //字符数、单词数和行数

    public static String print(String action1,String sourcefile){

     int linecount=1;

     int charcount=0;

     int wordcount=0;

  File file=new File(sourcefile);

  if(file.exists()){

   try{

    FileInputStream fis=new FileInputStream(file);

    InputStreamReader isr=new InputStreamReader(fis,"UTF-8");

    BufferedReader br=new BufferedReader(isr);

    String line=new String("");

    StringBuffer sb=new StringBuffer();

     TreeMap map = new TreeMap<>();

    while((line=br.readLine())!=null)

    {

     linecount++;

     sb.append(line);

     charcount+=line.length();

            String[] split = line.split("\\s++|\\.|,|\\;|\\(|\\)|\\[|\\]|\\<|\\>|\\=|\\-|\\+|\\*|\\/|\\{|\\}"); 

            for (int i = 0; i < split.length; i++) {

//              获取到每一个单词 

                Integer integer = map.get(split[i]); 

//              如果这个单词在map中没有,赋值1 

                if(null==integer){ 

                    map.put(split[i], 1); 

                }else{ 

//                  如果有,在原来的个数上加上一 

                    map.put(split[i], ++integer); 

                } 

            } 

     }

//         遍历,根据key获取所对应的value 

           Set keySet = map.keySet(); 

           for (String string : keySet)

            if(!(string.equals("")))

            wordcount+=map.get(string);

       br.close();

       isr.close();

       fis.close();

   }

   catch(FileNotFoundException e){

    e.printStackTrace();

   }

   catch(UnsupportedEncodingException e){

    e.printStackTrace();

   }

   catch(IOException e){

    e.printStackTrace();

   }

  }

            String message=null;

            if(action1.equals("-l"))

                  message=(sourcefile+",   行数:"+linecount+"\r\n");//换行"\r\n"不是"\n"

            else if(action1.equals("-c"))

              message=(sourcefile+",   字符数:"+charcount+"\r\n");//换行"\r\n"不是"\n"

            else if(action1.equals("-w"))

              message=(sourcefile+",   单词数:"+wordcount+"\r\n");//换行"\r\n"不是"\n"

           return message;

   }

 

(6)测试设计过程

测试包含了白盒测试,

l  保证一个模块中的所有独立路径至少被使用一次

l  对所有逻辑值均需测试

l  在上下边界及可操作范围内运行所有循环

l  检查内部数据结构以确保其有效性

边界测试,对于需求中指出:

空格,水平制表符,换行符,均算字符。

由空格或逗号分割开的都视为单词,且不做单词的有效性校验,例如:thi#,that视为用逗号隔开的2个单词。

因此有"【空格】【逗号】【空格】【逗号】【空格】【逗号】"算多少个字符,根据自己理解,应该是0个字符,有如下测试用例;
#include
int main(){
printf("hello world , , , , ,");
return 0;
}

(1)wc.exe -l test1.c

        输出 test1.c,   行数:5 正确

(2)wc.exe -l -c -w test1.c

      输出   test1.c,   字符数:75
                test1.c,   单词数:15
                test1.c,   行数:5

(3)wc.exe -w -c -l test1.c

      输出 test1.c,   单词数:15

              test1.c,  字符数:75   

              test1.c,  行数:5

打乱顺序,输出顺序改变,结果正确

(4)wc.exe-c test.c 

       未找到指定文件,出错

(5)wc.exe -c -l -w test1.c -o out

        输出文件夹未指定类型,出错

(6)wc.exe -c -l test1.c -o out.txt

          输出到out.txt, test1.c,  字符数:75    ,   test1.c,  行数:5

  (7)  wc.exe  -c -l -s  test.java -e ignore.txt

        走-s,-e路径,使用当前路径

(8)wc.exe -w -l -c -s -a test1.c

    测试所有结果

(9)wc.exe -w -l -c -s -a test1.c -o out.txt

   所有结果输出到指定文件夹

(10)wc.exe -e ignore.txt

   没有输入文件的情况

 

引用资料:

 (1)有关PSP的内容:http://www.cnblogs.com/xinz/archive/2011/10/22/2220872.html。

 (2)

    a. 手把手教你如何把jar文件,打包成jar文件以及转换为exe可执行文件;

    b. java打包成可执行的jar或者exe的详细步骤。

 (3)   Word Count in Java language, 三种不同的方法  http://blog.csdn.net/u014466412/article/details/51771508

 (4)白盒测试 http://blog.csdn.net/u010098159/article/details/50961635

你可能感兴趣的:(word count)