第二周个人作业WordCount
Github地址:https://github.com/Fish2333333/WordCount
PSP2.1表格:
|
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
|
Planning |
计划 |
30 |
40 |
|
· Estimate |
· 估计这个任务需要多少时间 |
180 |
300 |
|
Development |
开发 |
|
|
|
· Analysis |
· 需求分析 (包括学习新技术) |
25 |
30 |
|
· Design Spec |
· 生成设计文档 |
0 |
0 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
0 |
0 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
5 |
10 |
|
· Design |
· 具体设计 |
20 |
35 |
|
· Coding |
· 具体编码 |
150 |
210 |
|
· Code Review |
· 代码复审 |
10 |
15 |
|
· Test |
· 测试(自我测试,修改代码,修改 |
40 |
80 |
|
Reporting |
报告 |
30 |
35 |
|
· Test Report |
· 测试报告 |
10 |
10 |
|
· Size Measurement |
· 计算工作量 |
5 |
5 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
20 |
25 |
|
|
合计 |
345 |
495 |
解题思路
本人编程能力比较低,拿到题后,分析一下题目,根据需求在网上寻找相关要求的资料,参考并了解部分,根据自己理解结合网上内容编写。
程序设计实现过程
在main(String[] args)完成主要功能
通过countNumber(String str)//统计数字
countLetter(String str)//统计字母
countChinese(String str)//统计汉字
countLine(String line)//统计行数
countSpace(String str)//统计空格
统计文件内内容
通过printToFile(String filepath,String str)
输出结果到指定文件
代码说明
1、递归处理文件目录
public static ArrayList<File> findALLFile(File file,ArrayList<File> fileArrayList) {
if(file.isDirectory())
{
File[] lists = file.listFiles();
if(lists!=null)
{
for(int i=0;i<lists.length;i++)
{
findALLFile(lists[i],fileArrayList);
}
}
}
fileArrayList.add(file);
return fileArrayList;
}
2、统计数字
public static int countNumber(String str) {
int count = 0;
Pattern p = Pattern.compile("\\d");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
3、统计字母
public static int countLetter(String str) {
int count = 0;
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
4、统计汉字
public static int countChinese(String str) {
int count = 0;
Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
5、统计空格
public static int countSpace(String str) {
int count = 0;
Pattern p = Pattern.compile("\\s");
Matcher m = p.matcher(str);
while(m.find()){
count++;
}
return count;
}
6、统计行数
public static long[] countLine(String line) {
long notLine = 0;
// 判断是否为注释行
boolean comment = false;
int whiteLines = 0;
int commentLines = 0;
int normalLines = 0;
if (line.matches("^[//s&&[^//n]]*$") || line.equals("{") || line.equals("}")) {
// 空行
whiteLines++;
}
else if (line.startsWith("/*") && !line.endsWith("*/") || ((line.startsWith("{/*") || line.startsWith("}/*")) && !line.endsWith("*/"))) {
// 判断为"/*"开头
commentLines++;
comment = true;
} else if (comment == true && !line.endsWith("*/") && !line.startsWith("*/")) {
// 为多行注释中的一行(不是开头和结尾)
notLine++;
commentLines++;
} else if (comment == true && (line.endsWith("*/") || line.startsWith("*/"))) {
// 为多行注释的结束行
commentLines++;
comment = false;
} else if (line.startsWith("//") || line.startsWith("}//") || line.startsWith("{//") ||
((line.startsWith("{/*") || line.startsWith("}/*") || line.startsWith("/*")) && line.endsWith("*/"))) {
// 单行注释行
commentLines++;
} else {
// 正常代码行
normalLines++;
}
long[] args={normalLines+notLine,whiteLines,commentLines- notLine};
return args;
}
7、主函数
public static void main(String[] args){
int line=0;
int num=0;
int letter=0;
int space=0;
int word=0;
测试设计过程
test.txt

1


2


3


4


参考链接
https://www.cnblogs.com/azhqiang/p/4596793.html
https://www.cnblogs.com/bekeyuan123/p/6955283.html
http://www.jb51.net/article/115229.htm
https://wenku.baidu.com/view/a16b2dd8b307e87101f696ed.html
http://www.xuebuyuan.com/1324342.html
浙公网安备 33010602011771号