软件工程作业:个人项目-WC项目
项目相关要求
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 返回更复杂的数据(代码行 / 空行 / 注释行)。
空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
代码行:本行包括多于一个字符的代码。
注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:
} //注释
在这种情况下,这一行属于注释行。
高级功能:
-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。
需求举例: wc.exe -s -a *.c
返回当前目录及子目录中所有*.c 文件的代码行数、空行数、注释行数。
1.项目Github地址
我的github的项目:
https://github.com/JoyGin/wc
2.遇到的困难及解决方法
困难一:
判读命令中参数输入的合法性
创建了一个Set,将合法的参数初始化进去,若从命令中截取的参数不在集合内,则判为输入错误,重新输入。
此方法可以避免反复的循环判断
代码如下:
private static Set<String> set=new HashSet<String>();//存放正确的参数集合
//将命令转换成map,map中包含参数数组和文件名
public static Map<String,String[]> converse(String cmd) {
set.add("-c");set.add("-w");set.add("-l");set.add("-s");set.add("-a");//初始化合法参数集合
Map<String,String[]> paramAndFile=new HashMap<String,String[]>();
String[] paramWithPath=cmd.split(" ");
String[] param=new String[paramWithPath.length-1];
for(int i=0;i<paramWithPath.length;i++) {
if(i==paramWithPath.length-1) {
paramAndFile.put("fileName",new String[] {paramWithPath[i]});
}else {
if(!set.contains(paramWithPath[i])) return null;
else param[i]=paramWithPath[i];
}
}
paramAndFile.put("parameters",param);
return paramAndFile;
}
图形化界面:
一开始不知道从何下手,后来查找资料发现java的swing
public static void graphic() {
chooser = new JFileChooser();
int value = chooser.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
String filename = file.getAbsolutePath();
Map<String,Integer> map=count(filename);
print(true,null,map);
}
}
3.关键代码or设计说明
统计行数、字符数、单词数、注释行数、空白行数、代码行数,最终以Map返回,若返回为null说明路径错误,外部函数可以通过这个判断:
//计算字符数、行数等
public static Map<String,Integer> count(String fileName) {
Map<String,Integer> map=new HashMap<String,Integer>();
int lineCount = 0;//行数
int wordCount = 0;//单词数
int charCount = 0;//字符数
int nullLineCount = 0;// 空行数
int codeLineCount = 0;// 代码行数
int noteLineCount = 0;// 注释行数
File file = new File(fileName);
if (file.exists()) {
try {
//文件流读取文件
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
lineCount++;
sb.append(line);
charCount += line.length();
//去掉首尾空格
line = line.trim();
// 空白行
if (line == "" || line.length() <= 1) {
nullLineCount++;
continue;
}
// 注释行
int a = line.indexOf("/");
int b = line.substring(a + 1).indexOf("/");
if (b == 0) {
noteLineCount++;
continue;
}
codeLineCount++;// 代码行
}
wordCount = sb.toString().split("\\s+").length;//单词行
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
map.put("lineCount", lineCount);
map.put("wordCount", wordCount);
map.put("charCount", charCount);
map.put("nullLineCount", nullLineCount);
map.put("codeLineCount", codeLineCount);
map.put("noteLineCount", noteLineCount);
return map;
} else {
System.out.println("path error");
return null;
}
}
图形化界面效果:
进行单元测试:
4.PSP
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 15 |
· Estimate | · 估计这个任务需要多少时间 | 30 | 15 |
Development | 开发 | 595 | 725 |
· Analysis | · 需求分析 (包括学习新技术) | 120 | 150 |
· Design Spec | · 生成设计文档 | 30 | 15 |
· Design Review | · 设计复审 (和同事审核设计文档) | 15 | 13 |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 10 | 7 |
· Design | · 具体设计 | 60 | 10 |
· Coding | · 具体编码 | 180 | 300 |
· Code Review | · 代码复审 | 60 | 30 |
· Test | · 测试(自我测试,修改代码,提交修改) | 120 | 180 |
Reporting | 报告 | 55 | 50 |
· Test Report | · 测试报告 | 30 | 25 |
· Size Measurement | · 计算工作量 | 10 | 5 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 15 | 20 |
合计 | 680 | 770 |
学习进度条
第N周 | 新增代码(行) | 累计代码(行) | 本周学习耗时(小时) | 累计学习耗时(小时) | 重要成长 |
---|---|---|---|---|---|
1 | 266 | 266 | 680 | 680 | 熟悉java语言面向对象的特性,初步掌握软件测试能力,初始熟悉软件开发的基本流程和时间规划,对PSP表格有了解的基础上再加深了理解 |