WordCount
1. github地址:https://github.com/codermover/WordCount
2.PSP表格:
| PSP2.1 | PSP阶段 | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 50 | 60 |
| · Estimate | · 估计这个任务需要多少时间 | 20 | 20 |
| Development | 开发 | 300 | 400 |
| · Analysis | · 需求分析 (包括学习新技术) | 100 | 150 |
| · Design Spec | · 生成设计文档 | 30 | 30 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 30 | 30 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 10 | 10 |
| · Design | · 具体设计 | 50 | 100 |
| · Coding | · 具体编码 | 300 | 600 |
| · Code Review | · 代码复审 | 120 | 240 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 100 | 120 |
| Reporting | 报告 | 120 | 120 |
| · Test Report | · 测试报告 | 120 | 120 |
| · Size Measurement | · 计算工作量 | 40 | 10 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 0 |
| 合计 | 1825 | 2030 |
3.解题思路
题目较为困难,网上查阅,利用正则,先学习Java在学习打包.
4.程序设计实现过程
设计了一个包含多个函数的类,包括基本功能,记录行数,词数和字符数等
public int countWStopList()
public void setStopListFile()
public int countCommentLine()
public void read()
...
5.代码设计
有如下几个关键代码
public OutputStream setfile(String filename) {
File root = new File("");
//System.out.println(root.getAbsolutePath());
File file = new File(root.getAbsolutePath() + '/' + filename);
OutputStream outputStream;
try {
return new FileOutputStream(file);
} catch (IOException e) {
System.out.println(e);
}
return null;
}
public void setStopListFile(String filename)
{
File root = new File("");
File file = new File(root.getAbsolutePath() + "\\" + filename);
try {
InputStream inputStream = new FileInputStream(file);
int size = inputStream.available();
for(int i = 0; i < size; i++)
{
sl += (char)inputStream.read();
}
} catch (IOException e) {
System.out.println(e);
}
}
public int countChar()
{
return buffer.length();
}
public int countChar()
{
return buffer.length();
}
public int countLine()
{
if(buffer.length() == 0)
return 0;
String[] lines = buffer.split("\n", -1);
return lines.length;
}
public int countWord()
{
if(buffer.length() == 0)
return 0;
String[] words = buffer.split("[, \t\n]+");
return words.length;
}
public int countCodeLine()
{
if(buffer.length() == 0) return 0;
String[] lines = buffer.split("\n", -1);
return lines.length - countCommentLine() - countEmptyLine();
}
public int countWStopList()
{
if(buffer.length() == 0) return 0;
String[] stopWords = sl.split("[ ]");
List<String> stopWordList = Arrays.asList(stopWords);
String[] words = buffer.split("[, \t\n]+");
int result = words.length;
for(String word : words)
{
if(stopWordList.contains(word))
{
result--;
}
}
return result;
}
....详细代码转阅github
6 测试用例
| 用例 | 说明 |
|---|---|
| wc.exe -w file.c | 统计file.c中的单词数 |
| wc.exe -l file.c | 统计file.c中的行数 |
| wc.exe -c | 统计file.c中的字符数、单词数和行数 |
| wc.exe -a file.c | 同时统计file.c中的代码行/空行/注释行 |
| wc.exe -a file.c -o result.txt | 统计file.c中的代码行/空行/注释行,并存储到result.txt |
#######[注]
- 部分代码请教于杨晨

浙公网安备 33010602011771号