软工作业1—java实现wc.exe
github项目地址
https://github.com/liyizhu/wc.exe
WC 项目要求
- 基本功能列表:
wc.exe -c file.c //返回文件 file.c 的字符数(实现)
wc.exe -w file.c //返回文件 file.c 的词的数目 (实现)
wc.exe -l file.c //返回文件 file.c 的行数(实现)
- 扩展功能:
-s 递归处理目录下符合条件的文件。(实现)
-a 返回更复杂的数据(代码行 / 空行 / 注释行)。(实现)
[file_name]: 文件或目录名,可以处理一般通配符。(实现)
空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
代码行:本行包括多于一个字符的代码。
注释行:本行不是代码行,并且本行包括注释。
- 高级功能:
-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,
程序就会显示文件的字符数、行数等全部统计信息。(未实现)
PSP表格
|
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
|
Planning |
计划 |
20 |
20 |
|
· Estimate |
· 估计这个任务需要多少时间 |
10 |
10 |
|
Development |
开发 |
500 |
420 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
150 |
120 |
|
· Design Spec |
· 生成设计文档 |
30 |
40 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
20 |
30 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
10 |
5 |
|
· Design |
· 具体设计 |
30 |
10 |
|
· Coding |
· 具体编码 |
200 |
300 |
|
· Code Review |
· 代码复审 |
30 |
30 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
60 |
90 |
|
Reporting |
报告 |
120 |
120 |
|
· Test Report |
· 测试报告 |
40 |
80 |
|
· Size Measurement |
· 计算工作量 |
10 |
10 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
30 |
30 |
|
合计 |
1250 |
1315 |
解题思路描述
功能:wc.exe -c file.c //返回文件 file.c 的字符数
思路:逐行读取文件,去掉每行空字符后,统计个数,累加,直到文件读取完毕
功能:wc.exe -w file.c //返回文件 file.c 的词的数目
思路:逐行读取文件,将非字母替换为空格,然后按空格切割字符串为字符串数组,累加字符串数组长度,直到文件读取完毕
功能:wc.exe -l file.c //返回文件 file.c 的行数
思路:逐行读取文件,没读取一行,行数加一,知道文件读取完毕
功能:-a 返回更复杂的数据(代码行 / 空行 / 注释行)。
思路:逐行读取文件,判断是否存在//,/*,*/若存在,判定为注释行行数加一,判断是否只存在一个字符,或者为空,若是,判定为空行,若判断行中存在/*,则将标志域设置为true,直到读取到*/,将标志域设置为false,当标志域为true,判定为注释行,若非以上情况,判定为代码行
功能:[file_name]: 文件或目录名,可以处理一般通配符。
思路:将通配符*替换为\\.+表示多个任意字符,将通配符?替换为\\.表示一个任意字符,将文件名与该正则表达式匹配,若匹配,执行相应操作
功能:-s 递归处理目录下符合条件的文件。
思路:列举目录下的所有文件,若为目录,则递归调用函数,若为文件且符合文件格式,则执行相应操作,否则不作任何操作
设计实现过程

说明:
一共有两个类:WcUtil是工具类,WcMain类中只有main方法,用于处理控制台输入的参数,解析,判断是否合法,
并调用WcUtil类中相应的方法
代码说明
main() 功能:循环处理命令行参数,解析,判断参数的合法性,调用相应的处理方法
public class WcMain {
/*
* 主方法
*/
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//当前命令
String currentCommand = null;
//当前命令切分后的数组
String[] currentCommandStrings = null;
while(true) {
try {
//读取命令
if((currentCommand=br.readLine())!=null) {
currentCommandStrings = currentCommand.split(" ");
//命令字符串数组长度小于二或者长度大于四,为错误命令
if(currentCommandStrings.length<2||currentCommandStrings.length>4) {
//空命令直接跳过
if(currentCommandStrings.length==1&&"".equals(currentCommandStrings[0])) {
}else {
System.out.println("错误命令");
System.out.println("输入\"wc.exe help\"或\"wc.exe ?\"查看帮助文档");
}
}else {
//命令第一个字段必须为wc.exe
if("wc.exe".equals(currentCommandStrings[0])){
//查看具体命令
if(currentCommandStrings.length==3) {
switch (currentCommandStrings[1]) {
case "-c":
WcUtil.charNumber(currentCommandStrings[2]);
break;
case "-w":
WcUtil.wordNumber(currentCommandStrings[2]);
break;
case "-l":
WcUtil.lineNumber(currentCommandStrings[2]);
break;
case "-a":
WcUtil.complexData(currentCommandStrings[2]);
break;
default:
System.out.println("错误命令");
System.out.println("输入\"wc.exe help\"或\"wc.exe ?\"查看帮助文档");
break;
}
}else if(currentCommandStrings.length==2) {
switch (currentCommandStrings[1]) {
case "help":
case "?":
WcUtil.showHelpDoc();
break;
default:
System.out.println("错误命令");
System.out.println("输入\"wc.exe help\"或\"wc.exe ?\"查看帮助文档");
break;
}
}else if(currentCommandStrings.length==4&&"-s".equals(currentCommandStrings[1])) {
String currentDirectory = currentCommandStrings[2];
//路径是否存在
if(new File(currentDirectory).isDirectory()) {
String wildcardChar = currentCommandStrings[3].replaceAll("\\*","\\.+").replaceAll("\\?","\\.");
//System.out.println(wildcardChar);
WcUtil.recurseHandleAllFiles(currentDirectory,wildcardChar);
System.out.println("遍历结束");
}else {
System.out.println("错误:找不到指定路径");
}
}else {
System.out.println("错误命令");
System.out.println("输入\"wc.exe help\"或\"wc.exe ?\"查看帮助文档");
}
}else {
System.out.println("错误命令");
System.out.println("输入\"wc.exe help\"或\"wc.exe ?\"查看帮助文档");
}
}
}else {
System.out.println("错误命令");
System.out.println("输入\"wc.exe help\"或\"wc.exe ?\"查看帮助文档");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
lineNumber() 功能:计算文件行数
public static void lineNumber(String fileName) {
//判断文件是否存在且是否为c文件
if(new File(fileName).isFile()&&!fileName.endsWith(".c")) {
System.out.println("不支持该文件类型");
return;
}
int lineNumber = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
while((br.readLine())!=null) {
lineNumber++;
}
br.close();
System.out.println("文件:"+fileName+" 行数:"+lineNumber);
}catch (FileNotFoundException e1) {
System.out.println("错误:文件不存在");
return;
//e1.printStackTrace();
}catch(IOException e2) {
System.out.println("错误:文件读取出错");
//e2.printStackTrace();
}
}
charNumber() 功能:计算文件字符数
public static void charNumber(String fileName) {
if(new File(fileName).isFile()&&!fileName.endsWith(".c")) {
System.out.println("不支持该文件类型");
return;
}
int charNumber = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String currentLine = null;
String[] currentLineStrings = null;
while((currentLine=br.readLine())!=null) {
currentLineStrings = currentLine.trim().split("\\s+");
for(int i=0;i<currentLineStrings.length;i++) {
charNumber = charNumber+currentLineStrings[i].length();
//System.out.println(currentLineStrings[i]+currentLineStrings[i].length());
}
}
br.close();
System.out.println("文件:"+fileName+" 字符数数:"+charNumber);
} catch (FileNotFoundException e1) {
System.out.println("错误:文件不存在");
return;
//e1.printStackTrace();
}catch(IOException e2) {
System.out.println("错误:文件读取出错");
//e2.printStackTrace();
}
}
wordNumber() 功能:计算文件单词数
public static void wordNumber(String fileName) {
if(new File(fileName).isFile()&&!fileName.endsWith(".c")) {
System.out.println("不支持该文件类型");
return;
}
//单词数
int wordNumber = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
//当前行字符串
String currentLine = null;
//当前行切分后的数组
String[] currentLineStrings = null;
Pattern p = Pattern.compile("[_a-zA-Z0-9]+");
Matcher m = null;
while((currentLine=br.readLine())!=null) {
//将当前字符串中非字母下划线的字符全部替换为空格
currentLine = currentLine.replaceAll("[^_a-zA-Z]"," ");
currentLineStrings = currentLine.trim().split("\\s+");
for(int i=0;i<currentLineStrings.length;i++) {
m = p.matcher(currentLineStrings[i]);
if(m.matches()) {
wordNumber++;
}
//System.out.println(currentLineStrings[i]);
}
}
br.close();
System.out.println("文件:"+fileName+" 单词数:"+wordNumber);
} catch (FileNotFoundException e1) {
System.out.println("错误:文件不存在");
return;
//e1.printStackTrace();
}catch(IOException e2) {
System.out.println("错误:文件读取出错");
//e2.printStackTrace();
}
}
complexData() 功能:计算文件更复杂的数据(代码行,空行,注释行)
public static void complexData(String fileName) {
if(new File(fileName).isFile()&&!fileName.endsWith(".c")) {
System.out.println("不支持该文件类型");
return;
}
//代码行
int codeLineNumber = 0;
//空行
int blankLineNumber = 0;
//注释行
int annotationLine = 0;
//记录多行注释是否结束
boolean annotationBegin = false;
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String currentLine = null;
while((currentLine=br.readLine())!=null) {
if(annotationBegin) {
annotationLine++;
//多行注释结束
if(currentLine.contains("*/")) {
annotationBegin = false;
}
//多行注释开始
}else if(currentLine.contains("/*")){
annotationLine++;
annotationBegin = true;
}else if(currentLine.contains("//")) {
annotationLine++;
}else if(currentLine.trim().length()>1) {
codeLineNumber++;
}else {
blankLineNumber++;
}
}
br.close();
System.out.print("文件:"+fileName+" ");
System.out.println("代码行:"+codeLineNumber+" 空行:"+blankLineNumber+" 注释行:"+annotationLine);
}catch (FileNotFoundException e1) {
System.out.println("错误:文件不存在");
return;
//e1.printStackTrace();
}catch(IOException e2) {
System.out.println("错误:文件读取出错");
//e2.printStackTrace();
}
}
recurseHandleAllFiles() 功能:递归处理目录下的所有文件
public static void recurseHandleAllFiles(String directory,String wildcardChar) {
Pattern p = Pattern.compile(wildcardChar);
Matcher m = null;
File file = new File(directory);
if(file.isDirectory()) {
if(file.listFiles()!=null) {
for(File item:file.listFiles()) {
if(item.isDirectory()) {
recurseHandleAllFiles(directory+"\\"+item.getName(),wildcardChar);
}else {
m = p.matcher(item.getName());
if(m.matches()&&item.getName().endsWith(".c")) {
complexData(directory+"\\"+item.getName());
}
}
}
}
}
}
showHelpDoc() 功能:显示帮助文档
public static void showHelpDoc() {
System.out.println("本程序仅支持c程序文件,即.c文件");
System.out.println("wc.exe -c file.c 返回文件 file.c 的字符数");
System.out.println("wc.exe -w file.c 返回文件 file.c 的单词数");
System.out.println("wc.exe -l file.c 返回文件 file.c 的行数");
System.out.println("wc.exe -a file.c 返回文件 file.c 的代码行数、空行数、注释行数");
System.out.println("wc.exe -s directory *.c (支持通配符,*代表多个任意字符,?代表一个任意字符) 返回directory路径下所有符合条件的文件的代码行数、
空行数、注释行数");
System.out.println("wc.exe -x 显示图形界面,选取单个文件,显示文件的字符数、行数等全部统计信息");
}
测试运行
运行测试


单元测试

代码覆盖率

项目小结
1、首次使用软件工程的方法,虽然前期花费的时间稍多,但是后面编码思路更加清晰,改善过程不需要大幅度修改代码
2、首次使用JUnit进行单元测试,回归测试,单元测试覆盖率检测,认识到单元测试的重要性
3、重新学习了git和github的使用,更加熟悉git的各种操作
4、学习了exe4j的使用
软工作业1—java实现wc.exe的更多相关文章
- 软工作业(JAVA)
github传送门:https://github.com/hhg52516/WC.git 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序 ...
- 小白のjava实现wc.exe功能
GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c //返回文件 file.c 的字符数 wc.exe -w file.c //返回文件 file. ...
- 软工作业-----Alpha版本第一周小结
软工作业-----Alpha版本第一周小结 Part1.第一周周计划记录 姓名 学号 周前计划安排 每周工作记录 自我打分 yrz(队长) 1417 1.进行任务分析 2.任务分配 ...
- [软工作业]-软件案例分析-CSDN
[软工作业]-软件案例分析-CSDN(app) 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 个人博客作业-软件案例分析 我在这个课程的目标是 ...
- 软工作业No.1。Java实现WC.exe
网址:https://github.com/a249970271/WC WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有w ...
- 软工作业1:wc.exe项目开发(java)
Github地址:https://github.com/Zzhaomin/learngit 项目相关要求 : wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...
- 软工作业-wc(Python实现)
GitHub地址:GitHub PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 40 · Esti ...
- 软工作业-Wc
Wc.exe wc.exe是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数.单词数和行 ...
- java实现wc.exe
Github地址:https://github.com/ztz1998/wc/tree/master 项目相关要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功 ...
随机推荐
- nginx mac
==> nginx Docroot is: /usr/local/var/www The default port has been set in /usr/local/etc/nginx/ng ...
- 70.纯 CSS 创作一只徘徊的果冻怪兽
原文地址:https://segmentfault.com/a/1190000015484852 感想:monster中边框角.上下动画.旋转动画.左右动画,眼睛中transform:scaleY(n ...
- python 匿名函数&内置函数
匿名函数:为了解决那些功能很简单的需求而设计的一句话函数怎么定义匿名函数: cal = lambda x : x*x # cal是函数名,lambda是定义匿名函数的关键字 冒号前面的额x是参数即函数 ...
- 前端生成excel
一:安装依赖 1:在package.json 中加入四个依赖包 (1): "json2csv": "^3.11.0", (2):"file-saver ...
- Ajax需要带头信息跨域问题的解决
$.ajax({ type:"get", url:"http://localhost:8082/index/getMsg", dataType:'json', ...
- REATE A STATIC WEBSITE
REATE A STATIC WEBSITE USING JEKYLL Review Great work! Let's review what you accomplished in this un ...
- Servlet学习记录4
带进度条的文件上传 UploadServlet只实现了普通的文件上传,并附带普通文本域的提交.如果需要显示上传进度条,实时显示上传速度等,需要配合使用Ajax技术.这里仍然使用Apache的commo ...
- 浏览器端时间循环与nodejs端时间循环的不同之处(宏任务与微任务)
浏览器端与node端都有宏任务与微任务的概念.字面意思上看宏任务就是耗时间比较长的任务,而微任务是耗时短的任务. 在浏览器端,宏任务包括setTimeout,setInterval,微任务则包括Pro ...
- python中线程的知识点
什么是线程? 程序的执行线路.每个进程默认有一条线程.线程包含了程序的具体步骤. 多线程就是一个进程中有除主线程(默认线程)外还有多个线程. 线程与进程的关系(进程包含线程,而线程依赖进程存在) 1. ...
- CSS3奇特的渐变示例
渐变 4个维度去理解渐变 线性渐变 径向渐变 新写法 老写法 最后的老写法镜像渐变可能不太准确.其余都完全正确 <!DOCTYPE html> <html> <head& ...