**github地址:https://github.com/kongweixu/WordCount
一.PSP表格
PSP2.1 | PSP阶段 | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 20分钟 | 30分钟 |
· Estimate | · 估计这个任务需要多少时间 | 2天 | 3天 |
Development | 开发 | 2天 | 2天 |
· Analysis | · 需求分析 (包括学习新技术) | 2天 | 1天 |
· Design Spec | · 生成设计文档 | - | - |
· Design Review | · 设计复审 (和同事审核设计文档) | - | - |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 20分钟 | 20分钟 |
· Design | · 具体设计 | 2小时 | 3小时 |
· Coding | · 具体编码 | 4天 | 3天 |
· Code Review | · 代码复审 | 0.5天 | 0.5天 |
· Test | · 测试(自我测试,修改代码,提交修改) | 0.5天 | 0.5天 |
Reporting | 报告 | 30分钟 | 30分钟 |
· Test Report | · 测试报告 | 30分钟 | 30分钟 |
· Size Measurement | · 计算工作量 | 2小时 | 1小时 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 1小时 | 0.5小时 |
合计 | 10天 | 10天 |
二、解题思路
真·多年不碰JAVA,完全忘了java咋写了。选择了eclipse写,主要通过百度搜索了解每一个函数的使用方法,咋依葫芦画瓢去写。具体方法主要是分为读取函数方法和计算方法,读取文件之后调用计算方法,根据读取到的参数来决定输出的内容
三、代码说明
-
统计单词数的代码
总体大致相同,便贴出统计单词数部分的代码!
public int charCounts(File f) throws Exception{
String str;
int charCount = 0;
BufferedReader br=new BufferedReader(new FileReader(f));
while((str=br.readLine())!=null){
charCount += str.length();
}
File fs=new File("result.txt");
FileWriter fw = new FileWriter(fs, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(f.getName()+",字符数: "+charCount);
pw.flush();
fw.flush();
pw.close();
fw.close();
return charCount;
-
停用词表
public void stopWords(File f) throws Exception{
String str;
int wordCount=0;
int stopWord=0;
BufferedReader br=new BufferedReader(new FileReader(f));
while((str=br.readLine())!=null){
String []line= str.split(",| ");
wordCount+=line.length;
for(int i=0;i<line.length;i++){
if(line[i].equals("while"))stopWord++;
if(line[i].equals("if"))stopWord++;
if(line[i].equals("switch"))stopWord++;
}
}
int count =wordCount-stopWord;
File fs=new File("result.txt");
FileWriter fw = new FileWriter(fs, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(f.getName()+",单词数: "+count);
pw.flush();//flush实际上就是将所有的写入的流,一次性输出到文件中,之后进行关闭即可。如果没关闭流,也没进行flush,此时的内容并未写入到文件的。
fw.flush();
pw.close();
fw.close();
}
-
递归处理目录下符合条件的文件
public ArrayList<File> recursion(String filedir) throws Exception{
//ArrayList<File> listFiles=new ArrayList<File>();
File file=new File(filedir);
File []files=file.listFiles();
if(files==null)return null;
for(File f:files){
if(f.isFile()){
if(f.getName().endsWith(".c")){
fileList.add(f);
}
}else if(f.isDirectory()){
recursion(f.getAbsolutePath());
}
}
return fileList;
} -
参数解析
public void complex(File f) throws Exception{
String str;
boolean nodeflag = false;
int codeLine = 0;
int spaceLine = 0;
int nodeLine = 0;
BufferedReader br =new BufferedReader(new FileReader(f));
while((str=br.readLine())!=null){
if(str.matches("\\s*/\\*.*")&&str.matches(".*\\*/\\s*")){
nodeLine++;
continue;
}
else if(str.matches("\\s*|}\\s*|\\{\\s*")){
spaceLine++;
}
else if(str.matches("//.*")){
nodeLine++;
}else if(str.matches("\\s*/\\*.*")){
nodeLine++;
nodeflag = true;
}else if(str.matches(".*\\*/\\s*")){
nodeLine++;
nodeflag = false;
}else if(nodeflag)nodeLine++;
else codeLine++;
}
File fs=new File("result.txt");
FileWriter fw = new FileWriter(fs, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(f.getName()+",代码行/空行/注释行: "+codeLine+"/"+spaceLine+"/"+nodeLine);
pw.flush();//flush实际上就是将所有的写入的流,一次性输出到文件中,之后进行关闭即可。如果没关闭流,也没进行flush,此时的内容并未写入到文件的。
fw.flush();
pw.close();
fw.close();
}
四、测试
五、参考链接