编程作业
| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879 |
|---|---|
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879 |
| 这个作业的目标 | 使用gitee并且编程统计一些词频统计的程序 |
| 其他参考文献 | 构建之法 |
| gitee地址 https://gitee.com/xutaoa/project-java |
PSP
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | ||
| • Estimate | • 估计这个任务需要多少时间 | 900 | 1565 |
| Development | 开发 | ||
| • Analysis | • 需求分析 (包括学习新技术) | 30 | 70 |
| • Design Spec | • 生成设计文档 | 10 | 25 |
| • Design Review | • 设计复审 | 10 | 30 |
| • Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 10 | 10 |
| • Design | • 具体设计 | 30 | 40 |
| • Coding | • 具体编码 | 650 | 1200 |
| • Code Review | • 代码复审 | 30 | 30 |
| • Test | • 测试(自我测试,修改代码,提交修改) | 30 | 60 |
| Reporting | 报告 | 30 | 30 |
| • Test Repor | • 测试报告 | 30 | 30 |
| • Size Measurement | • 计算工作量 | 20 | 20 |
| • Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 20 | 20 |
| 合计 | 900 | 1565 |
代码规范地址https://gitee.com/xzyll/project-java/blob/master/20188484徐子溢/src/ codestyle.md
解题思路描述
1.读取文件信息
2.检查文件中字符数
3.检查单词总数
4.统计有效行数
5.输出统计内容到指定的输出文件中
设计与实现过程
1.检查文件中字符数
使用BufferedReader类对象读取文件,然后创建一个char来判断获取的字符是否是ASCLL码,如符合规则,记录字符的变量countchar便加一。
while((ch=(char)br.read())!=(char)-1)
{
if(ch<=127)//保证读取的字符为ASCLL码
{
countChar++;
}
}
2.检查单词总数
String[] strs=words.split("[^a-zA-Z0-9]");
String regexs = "^[a-zA-Z]{4,}.*";
for(int i=0;i<strs.length;i++)
{
if(strs[i].matches(regexs))
{
countWord++;
}
}
3.统计有效行数
for (int i=0;i<c.length;i++)
{
if (c[i]!='\n' && c[i]!='\r' && c[i]!='\t')
{
countLine++;
break;
}
}
4.统计出现频率最高的10个单词的出现次数
for (int i = 0; i < strs.length; i++) {
if (strs[i].matches(regexs)) {
if (!map.containsKey(strs[i].toLowerCase())) {
map.put(strs[i].toLowerCase(), 1);
} else {
int num = map.get(strs[i].toLowerCase());
map.put(strs[i].toLowerCase(), num + 1);
}
}
}
5.输出统计内容到指定的输出文件中
StringBuilder str = new StringBuilder();
str.append("characters: "+countChar+"\n" + "words: "+countWord+"\n" +"lines: "+countLine+"\n"s);
for(int i = 0;i<(list.size()<10 ? list.size():10);i++){
str.append(list.get(i).getKey()+": "+list.get(i).getValue()+"\n");
}
测试代码
1.字符数
void getCountChar() throws IOException {
String str = "D://input.txt";
Lib lib = new Lib(str,null);
int c=lib.getCountChar();
Assert.assertEquals(21,c);
}
2.列数
void getLineCount() throws IOException {
String str = "D://input.txt";
Lib lib = new Lib(str,null);
int c=lib.getLineCount();
Assert.assertEquals(3,c);
}
3.单词数
void getWordNum() throws IOException {
String str = "D://input.txt";
Lib lib = new Lib(str,null);
int c=lib.getWordNum();
Assert.assertEquals(2,c);
}
input

output

心路历程与收获
作业刚发布时,那字老多了看得我直头晕,拖拖拉拉才搞明白问题。之后使用了老久以前学习的JAVA,巩固了JAVA读取文件等知识。学会了使用gitee上传自己的代码,初步编写了自己的代码规范和使用文档,学习到了以后学习需要运用到的技能。
浙公网安备 33010602011771号