WordCount作业

1.git项目地址:

https://github.com/StrangeTang/WCProject

 

2.PSP表格:

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

2025

· Estimate

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

 15 15

Development

开发

 300 350

· Analysis

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

 20 20

· Design Spec

· 生成设计文档

 15 10

· Design Review

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

 15 15

· Coding Standard

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

 20 15

· Design

· 具体设计

 35 30

· Coding

· 具体编码

 200 180

· Code Review

· 代码复审

 20 15

· Test

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

 50 50

Reporting

报告

 60 55

· Test Report

· 测试报告

 15 15

· Size Measurement

· 计算工作量

 10 10

· Postmortem & Process Improvement Plan

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

 30 25
 

合计

 825 830

3.解题思路:

  1)基本功能都是一些java编程,最主要涉及到文件的读写操作以及字符串的相关操作

  2)git上传很简单,在网上一般都能找到上传的方法

  3)jar文件打包网上也有教程,也很简单

  4)最后要注意的事args字符串数组的传值问题,实现-c,-l,-w等功能

4.程序实现过程:

  4.1 java实现文件的读取:

          InputStreamReader isr = new InputStreamReader(new FileInputStream("except.txt"));
          //用来读取文件中的数据
          BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法;
          String s =null;
          s = br.readLine();
          isr.close();//关闭文件

  4.2 实现-c,-l,-w功能

          InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName));
          //用来读取文件中的数据
          BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法;
          while(br.read()!=-1)//read()=-1代表数据读取完毕
          {
           String s = br.readLine();
           if (s!=null)
           {
            countChar += s.length();//字符个数就是字符长度
               countword += s.split(" |,").length;//split() 方法用于把一个字符串分割成字符串数组,字符串数组的长度,就是单词个数
               if (isExcept){//如果有剔除表的话
                   String[] tmp=ExceptNameList();
                   String[] tmpS=s.split(" |,");
                   for (int i =0;i<tmpS.length;++i){
                       for (int j=0;j<tmp.length;++j){
                           if (tmpS[i].equals(tmp[j]))
                               countword--;
                       }
                   }
               }
           }
           countline++;//因为是按行读取,所以每次增加一即可计算出行的数目
          }
          isr.close();//关闭文件
          countChar++;

  4.3 实现-s功能

            File file = new File("G:\\java new projects\\WordCount");
            String[] files = file.list(new FilenameFilter(){//得到该目录之下所有的.c文件
                public boolean accept(File dir,String name){
                    return name.endsWith(".c");
                }
            });                    
            for(int i=0;i<files.length;i++){
          //文件名 输出文件名 是否统计字符数/单词数/行数 是否是全列表统计 是否使用停用表 Counts(files[i],outFileName,cs,ws,ls,isFileList,isExcept);//每调用一次这个函数就代表统计一次相应文件的字符数,单词数以及行数 }

  4.4 实现-o功能

            if (args[i].contains("-o"))
                isChangeOutFile=true;//标记是否使用其他输出文件
            if (args[i].contains(".txt")&&isChangeOutFile)//得到输出文件名
                outFileName="G:/java new projects/WordCount/"+args[i];

  4.5实现-e功能

            if (args[i].contains("-e"))
                isExcept=true;//使用停用表标记

 

//得到停用列表并分割开返回字符串数组
          public static String[] ExceptNameList() throws IOException{
          InputStreamReader isr = new InputStreamReader(new FileInputStream("except.txt"));
          //用来读取文件中的数据
          BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法;
          String s =null;
          s = br.readLine();
          isr.close();//关闭文件
          return s.split(" ");
    }        
               if (isExcept){//如果有剔除表的话
                   String[] tmp=ExceptNameList();//停用表字符串
                   String[] tmpS=s.split(" |,");//统计的文件的字符串
                   for (int i =0;i<tmpS.length;++i){
                       for (int j=0;j<tmp.length;++j){
                           if (tmpS[i].equals(tmp[j]))
                               countword--;
                       }
                   }
               }

5.测试过程:

    //测试字符数,单词数
    public static  void test1() {
        String[] args="wc.exe -c 1.c";
        MainClass.start(args);
    }
    
    //测试单词数目
    public static void test2() {
        String[]args="wc.exe -w 1.c";
        MainClass.start(args);
    }
    
    //测试行数
    public static void test3() {
        String[]args="wc.exe -l 1.c";
        MainClass.start(args);
    }
    //测试输出文件
    public static void test4() {
        String[]args="wc.exe -l -w -c out.txt -o 2.c";
        MainClass.start(args);
    }
    //基本功能联合测试
    public static void test5() {
        String[]args="wc.exe -l -c -w out.txt -o 2.c";
        MainClass.start(args);
    }
    
    //测试处理目录下符合文件
    public static void test6() {
        String[]args="wc.exe -l -c -w -s";
        MainClass.start(args);
    }
    
    //测试停用词表
    public static void test7() {
        String[]args="wc.exe -c -l -w 1.c -e";
        MainClass.start(args);
    }
    
    //高级功能测试
    public static void test8() {
        String[]args="wc.exe -l -c -w -s -o 2.c -e out.txt";
        MainClass.start(args);
    }

6.参考文献连接:

http://blog.csdn.net/sunkun2013/article/details/13167099

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/

https://zhidao.baidu.com/question/580312839.html

 

转载于:https://www.cnblogs.com/StrangeT/p/8612749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值