github项目地址:

https://github.com/changrui520/homework

作业要求:

可执行程序命名为:wc.exe。

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

存储统计结果的文件默认为result.txt,放在与wc.exe相同的目录下。

需求分析:

输入:wc.exe -c file.c

读取file.c,统计字符数,输出字符数并写入到result.txt中。

输入:wc.exe -w file.c

读取file.c,统计单词数,输出单词数并写入到result.txt中。

输入:wc.exe -l file.c

读取file.c,统计行数,输出行数并写入到result.txt中。

输入:wc.exe -a file.c

读取file.c,统计空行、注释行,输出行数并写入到result.txt中

PSP表格:

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

思路:

使用IO流中的字符流实现。

Main类:读取用户录入的内容,根据内容调用utils类中的相应方法。

Utils类:负责具体处理。charNum方法:统计字符数,wordNum方法:统计单词数,lineNum方法:统计行数,markLineNum方法:统计空行、注释行。

开发环境及工具:

win10+idea+jdk9.0.1+git+mavem

关键代码:

Main:

public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String currentCommand = null;
String[] commands = null;
while (true) {
try {
//判断命令是否为空
if ((currentCommand = br.readLine()) != null) {
commands = currentCommand.split(" ");
//判断是否为3个词
if (commands.length == 3) {
//判断第一个词是否为wc.exe
if ("wc.exe".equals(commands[0])) {
//判断第三个词是否以.c结尾
if (!commands[2].endsWith(".c")) {
System.out.println("必须以.c结尾");
continue;
}
//判断第二个词
switch (commands[1]) {
case "-c":
System.out.println(charNum(commands[2]));
break;
case "-w":
System.out.println(wordNum(commands[2]));
break;
case "-l":
System.out.println(lineNum(commands[2]));
break;
case "-a":
System.out.println(markLineNum(commands[2]));
break;
default:
System.out.println("错误,请重新输入");
break;
}
} else {
System.out.println("错误,请重新输入");
continue;
}
} else {
System.out.println("错误,请重新输入");
continue;
}
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("读取文件失败");
}
}
}
}

  

  Utils:

public class Utils {
public static int charNum(String fileName) throws IOException {
Integer num = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw=new BufferedWriter(new FileWriter("result.txt"));
int c;
//统计字符数
while((c=br.read())!=-1){
if((char)c!='\r'&&(char)c!='\n'){
num++;
}
}
//写出结果
bw.write(fileName+","+"字符数"+":");
bw.write(num.toString());
//关流
br.close();
bw.close();
return num;
} public static int wordNum(String fileName)throws IOException{
Integer num=0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw=new BufferedWriter(new FileWriter("result.txt"));
String currentLine=null;
//储存分割后的数组
String[] words=null; while((currentLine=br.readLine())!=null){
currentLine=currentLine.replaceAll("[^_a-zA-Z]"," ");
words=currentLine.split(" ");
for(String word:words){
//去掉空格,和空字符
if(!word.equals(" ")&&!word.equals("")){
num++;
}
}
}
bw.write(fileName+","+"单词数"+":");
bw.write(num.toString());
br.close();
bw.close();
return num;
} public static int lineNum(String fileName)throws IOException{
Integer num=0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw=new BufferedWriter(new FileWriter("result.txt")); while((br.readLine())!=null){
num++;
}
bw.write(fileName+","+"行数"+":");
bw.write(num.toString());
br.close();
bw.close();
return num;
} @Test
public static int markLineNum(String fileName) throws IOException {
Integer num = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
String line = null;
while ((line = br.readLine()) != null) {
line = line.replaceAll("\r\n", "");
if (isMark(line)) {
num++;
}
}
bw.write(fileName + "," + "空行、代码行、注释行数量" + ":");
bw.write(num.toString());
br.close();
bw.close();
return num;
} private static boolean isMark(String line) {
//判断是否为空行
if (line == null || line .equals("")) {
return true;
}
//判断是否以”*/“开头
if (line.endsWith("*/")) {
return true;
}
//判断是否以”//“开头
if (line.startsWith("//")) {
return true;
}
//判断是否以”/*“结尾
if (line.startsWith("/*")) {
return true;
}
return false;
}
}

  

  

测试设计过程:

共计11个测试用例:

1.wc.exe -c file.c

结果:

2.wc.exe -w file.c

结果:

3.wc.exe -l file.c

结果:

4.a.exe -c file.c

结果:

5.wc.exe -x file.c

结果:

6.wc.exe

结果:

7.wc.exe -c file.c file.c

结果:

8.wc.exe -c a.c

结果:

9.空命令

结果:

10..wc.exe -c file.java

结果:

11.wc.exe -a file.c

结果:

全部达到预期要求

作业心得:

通过这次wc作业的开发,我对软件工程这门专业有了更深入的了解和感悟,软件的开发不仅仅是简单地编写代码,还要注意设计的规范化,合理化,代码的安全性,程序的健壮性。软件上线之前必须经过单元测试。想要让用户觉得自己的程序好用,必须要考虑周到。

第一次作业:使用java实现word count的更多相关文章

  1. Java --本地提交MapReduce作业至集群☞实现 Word Count

    还是那句话,看别人写的的总是觉得心累,代码一贴,一打包,扔到Hadoop上跑一遍就完事了????写个测试样例程序(MapReduce中的Hello World)还要这么麻烦!!!?,还本地打Jar包, ...

  2. Word Count作业

    Word Count作业 一.个人Gitee地址:https://gitee.com/Changyu-Guo 二.项目简介 该项目主要是模拟Linux上面的wc命令,基本要求如下: 命令格式: wc. ...

  3. java第一次作业0

    lsl321 java第一次作业 #1. 本章学习总结 你对于本章知识的学习总结 本章我们学习了各种java相关文件的使用,以及码云,博客,pat等程序辅助软件,这些对于我们专业的学习有非常大的帮助, ...

  4. Java第一次作业——Java语言基础

    <Java技术>第一次作业 学习总结 1.Scanner类实现基本数据输入方法 Scanner input=new Scanner(System.in); int num = input. ...

  5. 个人项目作业-Word Count

    个人项目作业 1.Github地址 https://github.com/CLSgGhost/SE_work 2.项目相关需求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数. ...

  6. Spark: 单词计数(Word Count)的MapReduce实现(Java/Python)

    1 导引 我们在博客<Hadoop: 单词计数(Word Count)的MapReduce实现 >中学习了如何用Hadoop-MapReduce实现单词计数,现在我们来看如何用Spark来 ...

  7. 【2016.3.22】作业 Word count 小程序

    今天更下word count程序的设计思路及实现方法. 我的程序贴在coding里,这里就先不贴出来了, 我的coding地址:https://coding.net/u/holy_angel/p/wo ...

  8. Mac下hadoop运行word count的坑

    Mac下hadoop运行word count的坑 Word count体现了Map Reduce的经典思想,是分布式计算中中的hello world.然而博主很幸运地遇到了Mac下特有的问题Mkdir ...

  9. 第一次作业——WorkCount

    项目地址:https://gitee.com/yangfj/wordcount_project 1.软件需求分析: 撰写PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) ...

随机推荐

  1. A - Alice and the List of Presents (排列组合+快速幂取模)

    https://codeforces.com/contest/1236/problem/B Alice got many presents these days. So she decided to ...

  2. idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost connection toMySQL server at

    我是使用navicat的windows端 连接centos下mysql服务器 第一次常规连接mysql正常,idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost co ...

  3. 微信公众号开发调用摄像头、拍摄或选择图片、OCR识别

     一 .准备工作       <1> 域名认证准备工作 在需要调用摄像头的接口页面引入微信的js,具体地址为:(支持https):http://res.wx.qq.com/open/js/ ...

  4. 对接memcache经验分享

    接口访问日志  数据结构 分享 apiname 接口名称 apiname[cnt]接口访问次数每访问一次增加一次 这里要处理并发问题 我还没有解决: apiname[cnt][n][spent_tim ...

  5. gbdt在回归方面的基本原理以及实例并且可以做分类

    对书法的热爱,和编译器打数学公式很艰难,就这样的正例自己学过的东西,明天更新gbdt在分类方面的应用. 结论,如果要用一个常量来预测y,用log(sum(y)/sum(1-y))是一个最佳的选择. 本 ...

  6. ubuntu19.10安装cuda-10.1

    ubuntu19.10安装cuda-10.1 1.安装N卡驱动: 打开ubuntu的软件和更新,设置N卡驱动 2.查看ubuntu显卡驱动 nvidia-smi 显示: Sun Feb 23 06:4 ...

  7. 神奇的Python代码

    一 def f(arg=i): print(arg) i = 6 f() i = 7 f(i) 输出结果是: 7 7

  8. 富文本编辑器Tinymce的示例和配置

    Demo链接: https://download.csdn.net/download/silverbutter/10557703 有时候需要验证tinyMCE编辑器中的内容是否符合规范(不为空),就需 ...

  9. epoll——IO多路复用选择器

    上上篇博客讲的套接字,由于其阻塞性而导致一个服务端同一时间只能与一个客户端连接.基于这个缺点,在上篇博客我们将其设置为非阻塞实现了一个服务端同一时间可以与多个客户端相连,即实现了并发,但其同样留下了一 ...

  10. IntelliJ IDEA2018.2.7安装和破解教程

    一.安装 IntelliJ IDEA2018.2.7 IDEA官网下载地址链接:https://www.jetbrains.com/idea/download/previous.html 1.进入网站 ...