Github项目地址:https://github.com/Sabot1203/WordCount

一. 题目描述

  1. 实现一个简单而完整的软件工具(源程序特征统计程序)。
  2. 进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。
  3. 进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。

二.项目要求

  1. wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

  2. 实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。

  3. 具体功能要求:

    程序处理用户需求的模式为:wc.exe [parameter] [file_name]

三.解题思路

  1. 题目涉及文件的读取解析,选择java程序设计语言主要使用io流部分知识解决较为简单。
  2. 关于java程序转化为exe文件,及命令参数存储在args中相关知识参考网上知识即可解决

四.设计实现及代码说明

将各功能模块封装进WordCount类,再由主函数调用实现相应功能

选用BufferedInputStream和BufferedOutputStream,利用缓冲可以加快效率和速度

实现流程为:

  • 读取文件信息
  • 处理文件信息
  • 将结果写入文件
public class WordCount {

    //统计字符数
public void CountChars() throws IOException
{
...
} //统计总行数
public void CountLines() throws IOException
{
...
} //返统计总词数
public void CountWords() throws IOException
{
...
}
//统计 代码行/空行/注释行 的行数
public void CountLinesByKind()throws IOException
{
...
} //使用停用词文件 统计单词个数
public void CountWordsWithLimite() throws IOException
{
...
}
}

关键代码:

public class Main {
· · ·
public static void analyseCommand(String[] args) throws IOException
{
· · ·
if(args.length==0)//需要用户输出参数
{
System.out.println("Command codes are needed !");
}
else
{
for(int i=0;i<args.length;i++)
{
commands.add(args[i]);
if(!args[i].matches("^-.*"))//不是命令符号
{
if(args[i].contains("."))//是文件名或目录
{
if(!new File(args[i]).exists())//文件不存在
{
System.out.println("The file named "+args[i]+" does not exist");
System.exit(0);
}
else
{
commandsList.add(commands);
commands=new ArrayList<>();
}
}
else//指令有错
{
System.out.println("The "+(i+1)+"th code("+args[i]+") must begin with '-'");
System.exit(0);
}
}
}
}
commandAction(commandsList);
} public static void commandAction(List<List<String>> commandList) throws IOException
{
· · · for(List<String> commands:commandList)
{
if(commands.contains("-o"))
{
output=commands.get(commands.size()-1);
}
else if(commands.contains("-e"))
{
stop=commands.get(commands.size()-1);
}
else
{
input=commands.get(commands.size()-1);
}
} WordCount wc=new WordCount(input,stop,output); for(List<String> commands:commandList)
{
for(int i=0;i<commands.size()-1;i++)
{
switch(commands.get(i))
{
case "-c":wc.CountChars();
break;
case "-w":wc.CountWords();
break;
case "-l":wc.CountLines();
break;
case "-s":wc.CountLinesByKind();
break;
case "-e":wc.CountWordsWithLimite();
break;
case "-o":break;
default:System.out.println("No such command code");
}
}
}
}
}
    //代码行/空行/注释行 的行数
public void CountLinesByKind()throws IOException
{
. . .
while((strLine=input.readLine())!=null)
{
all_lines++;
strLine.replaceAll("\r", "");//去除换行符和空格 便于后面操作
strLine.replaceAll("\n", "");
strLine=strLine.trim();
strLine.replaceAll(" ", "");
if(InNoteLines==true)
{
note_lines++;
if(strLine.endsWith("*/")||strLine.endsWith("*/}"))
{
InNoteLines=false;
}
}
else if(strLine.startsWith("/*")||strLine.startsWith("{/*")) //进入注释行
{
note_lines++;
if(!strLine.endsWith("*/")&&!strLine.endsWith("*/}"))//本行未注释结束
{
InNoteLines=true;
}
}
else if(strLine.startsWith("//")||strLine.startsWith("{//"))
{
note_lines++;
}
else if(strLine.equals("")||strLine.equals("{")||strLine.equals("}"))
{
blank_lines++;
}
}
code_lines=all_lines-blank_lines-note_lines;
... }

五.测试运行

我们建立起一系列测试文件。如下:

  1. 空文件
  2. 只有一个字符的文件
  3. 只有一个词的文件
  4. 只有一行的文件
  5. 一个典型的源文件

测试结果如下:

六.项目总结

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

计划计划时间比实际计划时间短,动手开发时间比计划开发时间长

最新文章

  1. 【初码干货】使用阿里云对Web开发中的资源文件进行CDN加速的深入研究和实践
  2. SQL Server 2008 R2——使用FULL OUTER JOIN实现多表信息汇总
  3. MD5 32位 小写加密和大写加密
  4. 关于 iOS 批量打包的总结
  5. 转:TimeSpan的用法
  6. C++ 11 新特性
  7. javascript函数中的实例对象、类对象、局部变量(局部函数)
  8. C#Stimulator项目&gt;&gt;&gt;C/C++ DLL的生成和调用,Windows下的多线程
  9. File类的使用
  10. Linux server关闭自己主动
  11. url重写步骤
  12. ECMAScript中的两种属性
  13. Vim 简明教程【转载】
  14. 全网最详细的实用的搜索工具Listary和Everything对比的区别【堪称比Everything要好】(图文详解)
  15. Django配置富文本编辑器kindeditor
  16. FSM Code Generator
  17. 【Java并发编程】之六:Runnable和Thread实现多线程的区别
  18. python selenium webdriver方法封装(find_element_by)
  19. js 捕捉回车键触发登录,并验证输入内容
  20. 一些baidu面经

热门文章

  1. 《闲扯Redis八》Redis字典的哈希表执行Rehash过程分析
  2. MacOS下如何设置hosts?
  3. Numpy修改数组中的元素值
  4. Python List remove()方法
  5. PHP min() 函数
  6. PHP tan() 函数
  7. PHP children() 函数
  8. 代码扫描Sonar使用教程
  9. LVS-NAT:搭建HTTP及HTTPS负载均衡集群
  10. Hadoop生态系统入门进阶之一