Github地址

https://github.com/DolittleQZ/CountWord

PSP2.1表格

  

PSP2.1 PSP 阶段 预估耗时 (分钟) 实际耗时 (分钟)
Planning 计划 15 20
· Estimate · 估计这个任务需要多少时间 20 20
Development 开发  300  500
· Analysis · 需求分析 (包括学习新技术) 60 60
· Design Spec · 生成设计文档 50 30
· Design Review · 设计复审 (和同事审核设计文档) 30 0
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 40 100
· Design · 具体设计 60 60
· Coding · 具体编码 240 300
· Code Review · 代码复审 60 120
· Test · 测试(自我测试,修改代码,提交修改) 40 120
Reporting 报告 60 100
· Test Report · 测试报告 15 20
· Size Measurement · 计算工作量 10 10
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 20 20
  合计    

解题思路

  由于该项目要求使用java语言来实现,并且我上次使用java语言编程可能已经是一两年前,所以我感觉这个项目很难完成,但是这是老师布置的作业,没办法只能硬上了。首先题目要求的是在控制台输入命令来实现对指定文件(或文件夹内文件)单词数、字符数、行数以及代码行、空行和注释行数的统计。

首先在控制台获取用户输入的命令,解析命令,并将命令以空格为分界分段保存在一个字符串数组中;

然后遍历这个数组,针对对于不同的命令,我们可以调用相应的函数来一一实现这些功能。

程序设计实现过程

1.所以可以先写一个CountWords.java程序(即CountWords类),其中包含6个成员变量:

    linecount       //总行数
  wordcount     //单词数
  charcount        //字符数
  nullLinecount     //空行数
  codeLinecount     //代码行
  noteLinecount   //注释行

2.然后在类中添加成员函数

     CountWD函数           //读取文件中字符数、单词数、行数以及代码行、空行和注释行数的统计

     Writefile函数       //写入文件

    PrintResult         //在控制台程序中打印结果

    PrintFile           //将在控制台程序中打印的结果输入OutputFile文件中

    traverseFolder2       //遍历目录下所有文件

3.接着写一个Test.java程序,进行控制台命令的处理

代码说明

  Test.java主程序(部分):这部分是对命令的处理,获取参数并保存在sParameter字符串数组中,以及接下来调用CountWords.java中的方法。

 1 Scanner scan = new Scanner(System.in);
 2             String []sParameter= new String[10];
 3             for(int i=0;i!=sParameter.length;++i)
 4                 sParameter[i]="";
 5             int j=0;
 6             String str=scan.next();
 7             if(str.equals("wc.exe")) {
 8                 str=scan.next();
 9                 if(((str.equals("-c")||str.equals("-a"))||str.equals("-s"))||(str.equals("-w")||str.equals("-l"))){
10                     while(((str.equals("-c")||str.equals("-a"))||str.equals("-s"))||(str.equals("-w")||str.equals("-l"))) {
11                         sParameter[j++] = str;
12                         str = scan.next();
13                     }
14                     String filename = Class.class.getClass().getResource("/").getPath();
15                     String filename1 = "";
16                     if(str.charAt(str.length()-5)=='*') {
17                         if(str.charAt(0)=='*')
18                             cw.traverseFolder2(Class.class.getClass().getResource("/").getPath(), sParameter);
19                         else
20                             {
21                                 str =str.substring(0, str.length()-5);
22
23                                 cw.traverseFolder2(str, sParameter);
24                             }
25                     }
26                     else {
27                         filename = filename + str;
28                         filename1 = str;
29                         cw = cw.CountWd(filename);
30                         cw.PrintResult(sParameter, filename1);
31                     }
32                     str = scan.next();
33                     if(str.equals("-o")) {
34                         sParameter[j++] = str;
35                         str = scan.next();
36                         String filename2 = Class.class.getClass().getResource("/").getPath() + str;
37                         cw.Writefile(sParameter, filename1,filename2);
38                     }

CountWords.java:此类包含接下来要调用的所有方法。

首先是CountWD函数:读取文件中字符数、单词数、行数以及代码行、空行和注释行数的统计

 1 //读取文件中字符数、单词数、行数以及代码行、空行和注释行数的统计
 2     public CountWords CountWd(String sFilename) {
 3         CountWords cw1 = new CountWords(0,0,0,0,0,0);
 4         File file = new File(sFilename);
 5         if(file.exists()) {
 6             try {
 7                 FileInputStream fis = new FileInputStream(file);
 8                 InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
 9                 BufferedReader br = new BufferedReader(isr);
10                 String line = "";
11                 StringBuffer sb  = new StringBuffer();
12                 while ((line = br.readLine()) != null) {
13                     int space = 0,xiegang = 0;
14                     cw1.linecount++;
15                     sb.append(line);
16                     cw1.charcount += line.length();
17                     cw1.charcount++;
18                     for(int i =0;i!=line.length();++i) {
19                         if(line.charAt(i)=='/')
20                             if(line.charAt(i+1)=='/')
21                                 {
22                                     xiegang = 2;
23                                     break;
24                                 }
25                         if(line.charAt(i)!=' ')
26                             ++space;
27                     }
28                     cw1.wordcount = br.toString().split("\40+|,+").length;
29                     if(xiegang == 2 && space <=1)
30                         cw1.noteLinecount++;
31                     else if(space >= 2)
32                         cw1.codeLinecount++;
33                     else cw1.nullLinecount++;
34                 }
35                 cw1.wordcount = sb.toString().split("\40+|,+").length;
36                 br.close();
37                 isr.close();
38                 fis.close();
39             } catch (FileNotFoundException e) {
40                 e.printStackTrace();
41             } catch (UnsupportedEncodingException e) {
42                 // TODO Auto-generated catch block
43                 e.printStackTrace();
44             } catch (IOException e) {
45                 // TODO Auto-generated catch block
46                 e.printStackTrace();
47             }
48         }
49         return cw1;
50     }
51 }

Writefile:写入文件

 1 //写入文件
 2     public String Writefile(String []sParameter,String filename,String filename2) {
 3         String str="";
 4         for (int i = 0; i < sParameter.length; i++) {
 5             if (sParameter[i].equals("-l"))
 6                 str =str+ filename+",行数:"+String.valueOf(linecount)+"\r";
 7             else if (sParameter[i].equals("-w"))
 8                 str =str+ filename+",单词数:"+String.valueOf(wordcount)+"\r";
 9                 else if (sParameter[i].equals("-c"))
10                 str =str+ filename+",字符数:"+String.valueOf(charcount)+"\r";
11                 else if (sParameter[i].equals("-a"))
12                 str =str+ filename+", 代码行/空行/注释行:"+String.valueOf(nullLinecount)+
13                         "/"+String.valueOf(codeLinecount)+
14                         "/"+String.valueOf(noteLinecount)+"\n";
15                 else if(sParameter[i].equals("-o")) {
16                 PrintFile(str, filename2);
17             }
18         }
19         return str;
20     }

PrintResult:在控制台程序中打印结果

 1  //在控制台程序中打印结果
 2     public void PrintResult(String []sParameter,String filename) {
 3         //String str="";
 4         for (int i = 0; i < sParameter.length; i++) {
 5             if (sParameter[i].equals("-l")) {
 6             //    str =str+ filename+",行数:"+String.valueOf(linecount)+"\r";
 7                 System.out.print(filename+",行数:");
 8                 System.out.println(linecount);
 9             } else if (sParameter[i].equals("-w")) {
10                 //str =str+ filename+",单词数:"+String.valueOf(wordcount)+"\r";
11                 System.out.print(filename+",单词数:");
12                 System.out.println(wordcount);
13             } else if (sParameter[i].equals("-c")) {
14                 //str =str+ filename+",字符数:"+String.valueOf(charcount)+"\r";
15                 System.out.print(filename+",字符数:");
16                 System.out.println(charcount);
17             } else if (sParameter[i].equals("-a")) {
18                 //str =str+ filename+", 代码行/空行/注释行:"+String.valueOf(nullLinecount)+
19                         //"/"+String.valueOf(codeLinecount)+
20                     //    "/"+String.valueOf(noteLinecount)+"\n";
21                 //atest.c, 代码行/空行/注释行: 10/3/9
22                 System.out.print(filename+", 代码行/空行/注释行:");
23                 System.out.print(nullLinecount);
24                 System.out.print("/"+codeLinecount);
25                 System.out.println("/"+noteLinecount);
26             }
27         }
28         //return str;
29     }

PrintFile:将在控制台程序中打印的结果输入OutputFile文件中

 1 //将在控制台程序中打印的结果输入OutputFile文件中
 2     public void PrintFile(String content,String OutputFile) {
 3         FileOutputStream fop = null;
 4         File file;
 5         try {
 6             file = new File(OutputFile);
 7             fop = new FileOutputStream(file);
 8             // if file doesnt exists, then create it
 9             if (!file.exists()) {
10                 file.createNewFile();
11             }
12             // get the content in bytes
13
14             byte[] contentInBytes = content.getBytes();
15
16             fop.write(contentInBytes);
17             fop.flush();
18             fop.close();
19
20         } catch (IOException e) {
21             e.printStackTrace();
22         } finally {
23             try {
24                 if (fop != null) {
25                     fop.close();
26                 }
27             } catch (IOException e) {
28                 e.printStackTrace();
29             }
30         }
31     }

traverseFolder2:遍历目录下所有文件

 1 //遍历目录下所有文件
 2     public void traverseFolder2(String path,String []sParameter) {
 3         File file = new File(path);
 4         if (file.exists()) {
 5             File[] files = file.listFiles();
 6             if (files.length == 0) {
 7                 System.out.println("文件夹是空的!");
 8                 return;
 9             } else {
10                 for (int i = 0; i < files.length; i++) {
11                     if (files[i].isDirectory()) {
12                        // System.out.println("文件夹:" + files[i].getAbsolutePath());
13                         traverseFolder2(files[i].getAbsolutePath(),sParameter);
14                     } else {
15                         //System.out.println("文件:" + files[i].getAbsolutePath());
16                         CountWords wc = new CountWords(0,0,0,0,0,0);
17                         wc=wc.CountWd(files[i].getAbsolutePath());
18                         wc.PrintResult(sParameter,files[i].getAbsolutePath());
19                     }
20                 }
21             }
22         } else {
23             System.out.println("文件不存在!");
24         }
25     }

测试设计过程

首先在C:/Users/admin/Desktop/U201517028/abc/文件夹下创建文件,以便后续测试:

char.c

badca!@#$$%&^&*"':;|\/+_=-()

wordline.c

/*public void traverseFolder2(String path,String []sParameter) {*/File file = new File(path);//      if (file.exists()) {File[] files = file.listFiles();if (files.length == 0) {/**/  return;}//ncvfkasfc} else {

stoplist.txt

void file = new out

1.按照基本功能划分进行测试

1、  基本功能

(1)统计字符

wc.exe –c char.c

(2)统计单词和行数

wc.exe –w –l wordline.c

2、  扩展功能

(1)     统计代码行、空行、注释行

wc.exe –a wordline.c

(2)     停用词表

wc.exe –w stoptest.c –e stoplist.txt

(3)     文件夹遍历处理

wc.exe -s -a -w C:/Users/admin/Desktop/U201517028/abc/*.c

2.独立分支测试主程序流程图如下:

有5个判定节点,故总共有6条路径,分别为:

Path1:  1,2,4,5,6,7,9,11,13,14  (经过所有判定节点)

Path2:  1,2,3,10,13,14  (在节点2处执行另一分支)

Path3:  1,2,4,10,13,14  (在节点4处执行另一分支)

Path4:  1,2,4,5,9,11,13,14  (在节点5处执行另一分支)

Path5:  1,2,4,5,6,8,9,11,13,14  (在节点6处执行另一分支)

Path6:  1,2,4,5,6,7.9.12.13.14  (在节点9处执行另一分支)

这些路径对应的用例为:

Path1:  wc.exe -c -w -l -s   C:/Users/admin/Desktop/U201517028/abc/*.c -o output.txt

Path2:  cw.exe -c -w -l -s   C:/Users/admin/Desktop/U201517028/abc/*.c -o output.txt

Path3:  wc.exe -d -c -w -l -s   C:/Users/admin/Desktop/U201517028/abc/*.c -o output.txt

Path4:  wc.exe -c -w -l    char.c -o output.txt

Path5:  wc.exe -c -w -l -s  *.c -o output.txt

Path6:  wc.exe -c -w -l -s   C:/Users/admin/Desktop/U201517028/abc/*.c

3.其他测试
没有该文件:wc.exe -a abc1.c没有文件夹:wc.exe -a F:/bash/*.c空文件测试:wc.exe -c -w -l -a a.c  (假设a.c为一个空文件)大文件测试:wc.exe -c -w -l -a b.c  (假设b.c为一个大文件)其他类型文件测试:wc.exe -c -w -l -a b.txt
 
 
 

查阅资料:

https://wenku.baidu.com/view/7cb4e4096fdb6f1aff00bed5b9f3f90f76c64df0.html

https://www.cnblogs.com/collectionne/p/6815924.html

http://blog.csdn.net/daxiang_zhang/article/details/2149896

http://blog.csdn.net/hpchenqi_16/article/details/48504111

http://blog.csdn.net/alex__0805/article/details/50895222

https://bbs.csdn.net/topics/390567815

https://www.cnblogs.com/zhaoyan001/p/6077492.html

      

转载于:https://www.cnblogs.com/jiangzeming1926/p/8593115.html

第二周作业 wordcount相关推荐

  1. 软件质量与测试--第二周作业 WordCount

    软件质量与测试--第二周作业 WordCount Github地址: https://github.com/RicardoDZX/WordCount PSP: PSP2.1 PSP 阶段 预估耗时 ( ...

  2. 软测第二周作业WordCount

    一.Github地址: https://github.com/duwei1996/wc 二.PSP2.1表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 ...

  3. 2019年春季学期第二周作业(文件指针)

    2019年春季学期第二周作业(基础作业) 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始).并将最大值和对应的最小下标数值写入文件. 输入: 请建立以自 ...

  4. 软件工程 第二周作业

    ##软件工程第二周作业 提出问题 1. 一般来说,想要自己的程序跑得又快又好,就要减少函数的反复调用,但有所得则必有所失,效能提高就有可能伴随着程序的稳定性的降低,这两者应该如何权衡呢? 2. 关于5 ...

  5. 2017-2018-1 20179215《Linux内核原理与分析》第二周作业

    20179215<Linux内核原理与分析>第二周作业 这一周主要了解了计算机是如何工作的,包括现在存储程序计算机的工作模型.X86汇编指令包括几种内存地址的寻址方式和push.pop.c ...

  6. 学习linux第二周作业

    第二周作业: 本周作业内容: 1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. touch,rm,mv,cp,file,ls,chmod,chown,ln,rename, ...

  7. 序列模型第二周作业1:Operations on word vectors

    来自吴恩达深度学习系列视频:序列模型第二周作业1:Operations on word vectors.如果英文对你来说有困难,可以参照:[中文][吴恩达课后编程作业]Course 5 - 序列模型 ...

  8. 20189221 2018-2019-2 《密码与安全新技术专题》第二周作业

    20189221 2018-2019-2 <密码与安全新技术专题>第二周作业 课程:<密码与安全新技术专题> 班级: 201892 姓名: 郭开世 学号:20189221 上课 ...

  9. 「数据结构」普林斯顿算法课第二周作业

    「数据结构」普林斯顿算法课第二周作业 Algorithm I, Princeton 编程作业: Deques and Randomized Queues 思路 Deque.java Randomize ...

最新文章

  1. 数字证书采用公钥体制进行加密和解密。每个用户有一个私钥,用它进行 (46)。。。
  2. YunYang1994/tensorflow-yolov3 IndexError: list index out of range 解决办法
  3. 简明 ASP.NET Core 手册
  4. PyTorch的学习笔记
  5. 解决 jQuery 和其他库的冲突
  6. python和Java实现斐波那契Fibonacci数列
  7. CF1062F Upgrading Cities
  8. Vue 仿网易云音乐 WebApp
  9. jsp java语法_JSP 语法 | 菜鸟教程
  10. request.getParameter、request.getParameterValues、request.getParameterMap用法详解
  11. python文本文件加密_Python 文本加密解密 中文TXT数据
  12. 定义一个交通工具(Vehicle)的类
  13. Nyko推出平板手柄 为运行在Tegra3上的游戏特别打造
  14. 声学感知刻度(mel scale、Bark scale、ERB)与声学特征提取(MFCC、BFCC、GFCC)
  15. 海大叔侃币:作为一个炒币者,分享三点经验
  16. 我的世界服务器修改npc指令,我的世界自定义npc指令 | 手游网游页游攻略大全
  17. idea中找不到maven projects的集中解决办法
  18. [IAR] 编译报错:Variable expansion failed for Pre-Build command line
  19. win10声音设置没有麦克风降噪的解决办法
  20. DTU配置工具-F2x16工具

热门文章

  1. 今日写代码遇到的https请求的时候,提示ssl证书错误
  2. java递归查询无限极分类_sqlserver实现树形结构递归查询(无限极分类)的方法
  3. 误差反向传播法(二)【神经网络以层的方式实现】
  4. linux 在命令行中复制的快捷键_在 Linux 中加速工作的键盘快捷键 | Linux 中国
  5. 刷题关键点总结-单调栈、单调队列
  6. [转]Bootstrap table后端分页(ssm版)
  7. .net 读取xml文件
  8. [原] KVM 虚拟化原理探究(6)— 块设备IO虚拟化
  9. Windows 7 / Windows 10 安装 IPX/SPX
  10. Windows Server 2008 R2托管服务账户(MSA)的功能