WC.exe(Java实现)
一、GitHub项目地址:https://github.com/nullcjm/mypage
二、项目相关要求:
wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。
实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:
wc.exe [parameter] [file_name]
基本功能列表:
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 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。
需求举例:
wc.exe -s -a *.c
返回当前目录及子目录中所有*.c 文件的代码行数、空行数、注释行数。
三、PSP表格:
|
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
|
Planning |
计划 |
30 |
30 |
|
· Estimate |
· 估计这个任务需要多少时间 |
30 |
30 |
|
Development |
开发 |
660 |
780 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
240 |
300 |
|
· Design Spec |
· 生成设计文档 |
30 |
30 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 |
30 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 |
30 |
|
· Design |
· 具体设计 |
30 |
30 |
|
· Coding |
· 具体编码 |
180 |
240 |
|
· Code Review |
· 代码复审 |
60 |
60 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
60 |
60 |
|
Reporting |
报告 |
120 |
140 |
|
· Test Report |
· 测试报告 |
60 |
80 |
|
· Size Measurement |
· 计算工作量 |
30 |
30 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
30 |
30 |
|
合计 |
810 |
950 |
四、解题思路:看了项目要求后很快就有了大概的解题思路了,在制作文本编辑器时就有相关的关于行数,字数等的统计功能,主要花了较长时间重新学习相关的Java知识(如:正则表达式等),参考了其他人的解题思路,发现大家的思路都是大同小异,花了一定的时间后终于实现了相关功能。
五、功能实现
- WC类主程序
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class WC {
public static void main(String[] args) throws IOException{
while(true) {
System.out.println("指令说明:");
System.out.println("**************************************");
System.out.println("wc.exe -c 返回文件 file.c 的字符数:");
System.out.println("wc.exe -w 返回文件 file.c 的词的数目 ");
System.out.println("wc.exe -l 返回文件 file.c 的行数");
System.out.println("wc.exe -a 返回文件 file.c 的空行,代码行,注释行");
System.out.println("wc.exe -x 启用图形界面");
System.out.println("**************************************");
System.out.println("输入指令:"); Scanner input=new Scanner(System.in); //从键盘上输入指令并执行
String commend=input.nextLine();
switch (commend) {
case "-c":
Charcount ch = new Charcount();
break;
case "-w":
Wordcount wc = new Wordcount();
break;
case "-l":
Linecount line = new Linecount();
break;
case "-a":
Complex cp = new Complex();
break;
case "-x":
WCJFrame wf = new WCJFrame();
break;
default:
System.out.println("指令错误,请重新输入:");
break;
} }
}
} - -c指令实现
public class Charcount {
String REGEX ="\\S";
Charcount() throws IOException {
System.out.println("输入路径:");
Scanner input=new Scanner(System.in);
String path=input.nextLine();
BufferedReader fis =new BufferedReader(new FileReader(path));
int charcount=0;
String w;
Pattern p =Pattern.compile(REGEX);//匹配合适的字符
while((w=fis.readLine()) != null) {
Matcher m =p.matcher(w);
while(m.find()) //找到对应字符时字符数+1 charcount ++;
}
System.out.println("字符数:"+charcount);
fis.close(); }
} - -w指令实现
public class Wordcount {
String REGEX ="[a-zA-Z]+\\b"; //判定为单词的正则表达式条件
Wordcount() throws IOException {
System.out.println("输入路径:");
Scanner input=new Scanner(System.in);
String path=input.nextLine();
BufferedReader fis =new BufferedReader(new FileReader(path));
int wordcount =0;
String w;
Pattern p =Pattern.compile(REGEX);
while((w=fis.readLine()) != null) {
Matcher m =p.matcher(w);
while(m.find()) //当找到符合条件的内容时单词数+1
wordcount ++;
} System.out.println("单词数:"+wordcount);
fis.close();
}
} - -l指令实现
public class Linecount {
Linecount() throws IOException {
System.out.println("输入路径:");
Scanner input=new Scanner(System.in);
String path=input.nextLine();
BufferedReader fis =new BufferedReader(new FileReader(path));
int linecount=0;
while(fis.readLine()!=null) { //当前行不为空时,行数+1
linecount++;
}
System.out.println("行数:"+linecount);
fis.close();
}
} - -a指令实现
public class Complex { Complex() throws IOException{
System.out.println("输入路径:");
Scanner a = new Scanner(System.in);
String path = a.nextLine();
BufferedReader fis = new BufferedReader(new FileReader(path));
int spacecount = 0;
int notecount = 0;
int codecount = 0;
boolean state = false;
String c;
while((c=fis.readLine())!=null) {
if(c.contains("/*")) { //多行注释开始标记
notecount++;
state = true;
}
else if(state) {
notecount++;
if(c.contains("*/")) { //多行注释结束标记
state = false;}
}
else if(c.contains("//")) { //单行注释标记
notecount++;
}
else if(c.trim().length()>1) { //判定为代码行条件
codecount++;
}
else {spacecount++;}
} fis.close();
System.out.println("空白行:"+spacecount);
System.out.println("注释行:"+notecount);
System.out.println("代码行:"+codecount);
}
}
六、功能测试
1.测试文档:
空文件(1.txt)
只有一个字符的文件(2.txt)
只有一个词的文件(3.txt)
只有一行的文件(4.txt)
一个典型的源文件(5.txt)
2.-c指令回归测试

3.-w、-a、-x指令测试

4.-x指令测试


七、心得回顾
第一次详细制定计划去完成一个项目,各方面都觉得收获匪浅。不仅回顾了以前学过的知识,还学会了运用刚学会的知识。但在时间安排上还存在很大的问题,这点有待改进。
WC.exe(Java实现)的更多相关文章
- 个人项目-WC.exe (Java实现)
一.Github项目地址:https://github.com/blanche789/wordCount/tree/master/src/main/java/com/blanche 二.PSP表格 P ...
- 小白のjava实现wc.exe功能
GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c //返回文件 file.c 的字符数 wc.exe -w file.c //返回文件 file. ...
- java实现wc.exe
Github地址:https://github.com/ztz1998/wc/tree/master 项目相关要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功 ...
- 软工作业No.1。Java实现WC.exe
网址:https://github.com/a249970271/WC WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有w ...
- JAVA实现WC.exe功能
项目要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个文件. 具体功能要求: 程序处理用户需求的模式为: wc.exe [paramet ...
- Java 实现 WC.exe
Github:https://github.com/YJOED/Code/tree/master/WC/src 一.题目:实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他 ...
- 软件工程:Java实现WC.exe基本功能
项目相关要求 GitHub地址:https://github.com/3216004716/WC 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处 ...
- 软工作业1:wc.exe项目开发(java)
Github地址:https://github.com/Zzhaomin/learngit 项目相关要求 : wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...
- WC.exe(基于Java实现)
一.github地址 https://github.com/Mazin-hub/MyWC.exe.git 二.PSP表格 PSP2.1 Personal Software Process Stage ...
随机推荐
- shiro 基本知识测试
shiro 基本知识测试 <!--shiro核心包--> <dependency> <groupId>org.apache.shiro</groupId> ...
- maxima已知方程,计算结果
- 利用nodejs识别二维码内容的方法
const decodeImage = require('jimp').read; const qrcodeReader = require('qrcode-reader'); qrDecode(&q ...
- 【day02】PHP
一.数据类型(8个主要数据类型和4个伪类型) 1.8个主要数据类型 (1)标量类型(存储单一值) a.整型(Integer Int) b.浮点型(Float Do ...
- vijos2055 移动金币
题目链接 思路 首先这是一个阶梯博弈. 我们将金币两两组合,如果对方移动前一个,那么我们把后一个移动相同的距离,局面相当于没有变化.如果对方移动后一个,就相当于\(NIM\)游戏中,取走了一些石子. ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 关于ENVI5.0菜单栏不能正常显示(win7 x86系统)
在安装了envi5.0之后,打开显示的并不是之前4.8版本的样式也就罢了,还不能正常的将菜单栏显示出来. 上网搜了下,发现又是一个是版本兼容的问题 解决: 找到Envi5.0 右键---属性---兼容 ...
- 使用Docker构建Jekyll框架网站
使用Docker构建Jekyll框架网站 使用dockerfile构建apache + jekyll 目录 Jekyll基础镜像 构建Jekyll基础镜像 Apache镜像 构建Jekyll Apac ...
- Gin框架 - 项目目录
概述 今天给大家分享,在 API 端使用 Gin 框架时,项目的目录. 目录 ├─ Project Name │ ├─ config //配置文件 │ ├── ... │ ├─ controller ...