一、码云地址
https://gitee.com/WoShiLiangChaoWeiDeShiYou/PersonalProject-Java/tree/master/wordcount
二、个人psp表格
| PSP2.1 | 个人开发流程 | 预估耗费时间(分钟) | 实际耗费时间(分钟) | 
|---|---|---|---|
| Planning | 计划 | 30 | 50 | 
| · Estimate | 明确需求和其他相关因素,估计每个阶段的时间成本 | 30 | 60 | 
| Development | 开发 | 100 | 300 | 
| · Analysis | 需求分析 (包括学习新技术) | 50 | 100 | 
| · Design Spec | 生成设计文档 | 60 | 80 | 
| · Design Review | 设计复审 | 40 | 50 | 
| · Coding Standard | 代码规范 | 40 | 60 | 
| · Design | 具体设计 | 60 | 150 | 
| · Coding | 具体编码 | 20 | 60 | 
| · Code Review | 代码复审 | 30 | 0 | 
| · Test | 测试(自我测试,修改代码,提交修改) | 0 | 0 | 
| Reporting | 报告 | 20 | 0 | 
| · | 测试报告 | 15 | 0 | 
| · | 计算工作量 | 30 | 0 | 
| · | 并提出过程改进计划 | 0 | 0 | 
三、解题思路
1、本题我选用的编程语言是Java
2、要统计txt文件中单词数目,就要对txt文件的内容进行读取,使用流,就写了一个;需要对读取的数据内容进行判别分析,单词、空格、字符、数字等; 对于重复出现的单词进行统计次数;不管大小写还是单词等都要进行统计,最后对每种字符进行分类和统计输出;这就用到了Java语言对字符串判断的知识点和分类统计的知识点
3、对于编码的设计,我先对类进行了划分,然后才开始编码,个人觉得这样比较有效率
4、还有一点就是遇到问题时怎么解决,遇到的问题大部分都是上百度查了然后自己理解之后进行尝试,尝试了很多很多遍最终还是成功了,说明坚持就是胜利!
四、设计实现过程
1、划分的类:
- Word类,用来对字符进行统计
- File_rw类,用来对文本文件进行读取和写入
- Main类,对所有的类进行调用,然后实现代码的功能
 2、代码说明
- Word类是核心类,里用getWordnumber方法统计单词数量,getCharnumber方法统计字符数量,getLine统计行数,getWordcount方法统计单词出现次数。
- 虽然对单词次数进行了统计,但没有对它次数进行输出,目前来讲没有用武之地
import java.util.Map;
public class Word{
    
	private int wordnumber=0;
	private int charnumber=0;
	private int line=0;
	private Map<String,Integer> wordcount;
	String text;
	public Word(String text) {
		this.text = text;
	}
	public int getWordnumber() {
		String str=text;
		String[] words=str.split("\\s*[^0-9a-zA-Z]+");
		for(String s:words) {
			if(s.matches("[a-zA-Z]{4,}[a-zA-Z0-9]*")) {
				wordnumber++;
			}
		}
		return wordnumber;
	}
	public int getCharnumber() {
		for(int i=0;i<text.length();i++) {
			char c=text.charAt(i);
			if(c > 31 && c < 127 ||c == 10) {
				charnumber++;
			}
		}
		return charnumber;
	}
	public int getLine() {
		String[] lines=text.split("\r\n");
		for(int i=0;i<lines.length;i++) {
			if(lines[i].trim().length()!=0) {
			    line++;
			}
		}
		return line;
	}
	public Map getWordcount() {//统计每个单词出现次数
		String []wordnum=text.split("\\s");
		for(int i=0;i<wordnum.length;i++) {
			if(wordnum[i].length()>=4) {
				char c;
				for (int j = 0; j < 4; j++) {
					c = wordnum[i].charAt(j);
					if (!(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {
						if(wordcount.get(wordnum[i]) != null) {
							wordcount.put(wordnum[i], wordcount.get(wordnum[i])+1);
							
						}
						else {
							wordcount.put(wordnum[i], 1);
						}
					}
			}
		}
	}
		return wordcount;
	
}        
	}
- File_rw类中用readToString方法对文件进行读取。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class File_rw {
   public String readToString(String filepath) throws IOException {
	   File file =new File(filepath);
	   FileInputStream files=new FileInputStream(file);
		Long filelength =file.length();
		byte[] by =new byte[filelength.intValue()];
		StringBuffer buf = new StringBuffer();
	   if(file.isFile()&&file.exists()) {
		   
		
			if(files.read(by)!=-1) {
				buf.append(new String(by));
			}
			   
	   }files.close();
	  return buf.toString();
   }
}
五、心路历程与收获
个人项目的难度确实有些大,自己要做一个完美的项目更是难上加难,思考了很久才开始着手写代码,并且实现的功能还十分简陋。比如对于字符单词处理如果使用正则表达式。由于自己之前没有好好学习,现在才觉得书到用时方恨少的感觉。很多代码包括一些基础简单的都要上网查资料或者咨询同学。自己之后会更认真更努力去完善自己的不足,争取进步
 
                    
                 

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号