项目地址:https://github.com/yogurt1998/WordCount

要求

  • 基本要求

    • -c 统计文件字符数(实现)
    • -w 统计文件单词数(实现)
    • -l 统计文件行数(实现)
  • 扩展功能
    • 递归处理目录下符合条件的文件。(实现)
    • 返回更复杂的数据(代码行 / 空行 / 注释行)(实现)
  • 高级功能
    • 实现图形化界面(未实现)

PSP

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

设计

main.java

主函数:一个循环获取输入命令并分隔出指令和文件路径。

统计函数:通过IO流获取文件内容并统计字符数、行数、单词数等。

文件判断函数:判断是否为文件夹或者文件以实现递归处理功能

AFile.java

代表着字符数等六个属性。

代码

main()函数

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub do {
String filename = null;
String bString = null;
System.out.println("请输入命令(格式:[parameter] [file_name])"); while (true) {
Scanner scanner = new Scanner(System.in); // 获取键盘输入 if (scanner.hasNext()) {
bString = scanner.next();
} if (bString.equals("-c") || bString.equals("-w")
|| bString.equals("-l") || bString.equals("-s")
|| bString.equals("-a")) {
if (scanner.hasNextLine()) {
filename = scanner.next();
}
break;
} else {
System.out.println("错误!请重新输入!");
}
} String filepath = "D:\\test\\" + filename; // 在绝对路径中打开 File file = new File(filepath);
if (!file.exists()) {
System.out.println("文件不存在");
continue;
} AFile aFile = new AFile();
aFile = findFiles(filepath); if (bString.equals("-c"))
System.out.println("字符数为" + aFile.charNumb);
else if (bString.equals("-w"))
System.out.println("单词数为" + aFile.wordNumb);
else if (bString.equals("-l"))
System.out.println("行数为" + aFile.lineNumb);
else if (bString.equals("-a"))
System.out.println("空行数为:" + aFile.empleLine
+ " 代码行数为:" + aFile.codeLine + " 注释行数为:" + aFile.nodeLine); } while(true); }

findFiles()函数:通过递归处理文件

private static AFile findFiles(String file_path) {

		File file = new File(file_path);
AFile aFile = new AFile(); if (file.isDirectory()) { // 判断为文件夹
File[] files = file.listFiles(); // 获取文件列表
if (files == null) {
System.err.println("找不到");
} else if (files.length == 0) {
System.out.println("目录为空");
} else {
for (File f : files) { // 循环处理文件
if (f.isDirectory()) // 判断为文件夹
findFiles(f.getPath());
else if (f.isFile()) { // 判断为文件
System.out.println("文件名为:" + f.getName() + "\n字符数为" + getCount(f.getPath()).charNumb +
"\n单词数为" + getCount(f.getPath()).wordNumb +
"\n行数为" + getCount(f.getPath()).lineNumb +
"\n空行数为:" + getCount(f.getPath()).empleLine +
"\n代码行数为:" + getCount(f.getPath()).codeLine +
"\n注释行数为:" + getCount(f.getPath()).nodeLine);
}
}
}
} else if (file.isFile() && file.exists()) { // 判断为文件
aFile = getCount(file.getPath());
} return aFile;
}

getCount()函数:通过BufferedReader获取文件内容并判断

private static AFile getCount(String filepath) {
AFile aFile = new AFile();
try {
BufferedReader brin = new BufferedReader(new FileReader(filepath));
String s;
int state = 0; // 判断是否在单词内 // 使用正则表达式判断注释行
String regxNodeBegin = "(\\S?)\\s*/\\*.*";
String regxNodeEnd = "(.*\\*/\\s*)\\S?";
String regxNode = "(\\s*)(\\S?)(//+).*"; while ((s = brin.readLine()) != null) {
++ aFile.lineNumb;
int countLetter = 0; // 判断非空格字符数量 for (int i = 0; i < s.length(); i++) {
++aFile.charNumb;
Character c = s.charAt(i);
++countLetter;
if (c == ' ' || c == '\n' || c == '\t') {
state = 0;
--countLetter;
}
else if (state == 0) {
state = 1;
++aFile.wordNumb;
}
} if (s.matches(regxNodeBegin) || s.matches(regxNodeEnd)
|| s.matches(regxNode))
++aFile.nodeLine;
else if (countLetter > 1) // 如果非空格字符数多于1
++aFile.codeLine;
else
++aFile.empleLine;
}
} catch (IOException e) {
// TODO: handle exception
System.out.println(e.getMessage());
} return aFile;
}

测试运行

测试文件:

测试结果:

总结

  1. 根据在软件工程课堂上得到的知识进行分析,但没有细心分析导致递归功能一开始写错。
  2. 程序的思路一开始并不是很清晰导致后面很多修改以及代码繁杂。
  3. 学习了Git和GitHub的使用以及exe4j的使用。
  4. 对程序设计很不熟悉需要更多的练习。

软件工程作业 - 实现WC功能(java)的更多相关文章

  1. 软件工程—WC功能实现 (JAVA)

    软件工程-WC功能实现(JAVA) Github项目地址:https://github.com/Ousyoung/wc 项目要求 ​ wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和 ...

  2. 软件工程作业 - word count

    (编程和软件工程作业系列) 实践最简单的项目:WC 实践是理论的基础和验证标准,希望读者贯彻“做中学”的思想,动手实现下面的项目,并和别人的成绩相比较,分析产生差距的原因. 1. 实现一个简单而完整的 ...

  3. Implementation of WC in JAVA

    Implementation of WC in JAVA github地址 相关要求 基本功能 -c [文件名] 返回文件的字符数 (实现) -w [文件名] 返回文件的词的数目 (实现) -l [文 ...

  4. 实现wc部分功能 java

    GitHub地址:https://github.com/carlylewen/ruangong 相关要求 基本功能 wc.exe -c file.c     //返回文件 file.c 的字符数(实现 ...

  5. 个人小项目——Java实现WC功能

    这个小项目用了两种方法解决了该功能的实现. 1.两种方法的功能和具体实现 代码可以成功运行,但是有一些情况考虑不完整,一种方法用了FileOutputStream输出流,为了解决空格无法统计问题,对文 ...

  6. java实现wc功能

    github项目地址:https://github.com/3216004717/ruanjiangongcheng.git 项目相关要求 基本要求 wc.exe -c file.c //返回文件 f ...

  7. 算法笔记_202:第三届蓝桥杯软件类决赛真题(Java高职)

    目录 1 填算式 2 提取子串 3 机器人行走 4 地址格式转换 5 排日程   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 填算式 [结果填空] (满分11分) 看这个算式: ☆☆☆ + ☆☆ ...

  8. 软件设计模式之代理模式(JAVA)

    貌似停笔了近半个月了,实在不该啊,新的一年,时刻让自己归零. Back To Zero,就从这篇文章拉开今年的序幕吧. 这篇文章准备介绍下有关代理模式的基本概念和静态代理.动态代理的优缺点及使用方法( ...

  9. 软件工程作业——Word Counter

    github地址 https://github.com/Pryriat/Word_Counter 项目说明 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命 ...

随机推荐

  1. 微信小程序及开发工具介绍

    http://mp.weixin.qq.com/wiki  这里下载开发者工具

  2. getchar()、putchar()、gets()、puts()、cin.get()、cin.getline()、getline()

    1.getchar: 原型为int getchar(void). 它从stdin里读取一个字符.返回值为用户输入的ASCⅡ码,出错返回-1. eg:c=getchar(). 2.putchar: 原型 ...

  3. 140. Word Break II (String; DP,DFS)

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  4. discrete

    discrete - 必应词典 美[dɪ'skrit]英[dɪ'skriːt] adj.离散的:分离的:各别的 网络不连续的:分立的:离散型

  5. [leetcode]403. Frog Jump青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  6. Spring整合JMS——事务管理

    Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.JmsTransactio ...

  7. 设置 svn 与 web线上同步

    默认你已经配置好了svn服务 1.假设我们的线上网站目录为:/data/www/xxx 2.假设svn的仓库目录为:/data/svn/repo 一.checkout一份svn到线上网站目录 svn ...

  8. ajax访问当前页面后的 [WebMethod]描述的方法

    脚本: function show() { $.ajax({ type: "post", async: false, contentType: "application/ ...

  9. Bioconductor简介

    Bioconductor简介 2012-10-09 ~ ADMIN 源:Bioconductor: open software development for computational biolog ...

  10. json.dumps错误:'utf8' codec can't decode byte解决方案-乾颐堂

    一次在使用json.dumps()过程中,出现错误提示: ERROR:"UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in ...