202103226-1 编程作业

202103226-1 编程作业

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/computer-science-class4-2018
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class4-2018/homework/11880
这个作业的目标 完成编程需求、学习PSP模型、阅读《构建之法》
其他参考文献 《构建之法》《现代软件工程》 《腾讯C++代码规范》

一、PSP表格时间统计

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 1h 2h
• Estimate • 估计这个任务需要多少时间 2h 2h
Development 开发 8h 12h
• Analysis • 需求分析 (包括学习新技术) 3h 4h
• Design Spec • 生成设计文档 1h 0.5h
• Design Review • 设计复审 0.5h 0.5h
• Coding Standard • 代码规范 (为目前的开发制定合适的规范) 1h 1.5h
• Design • 具体设计 2h 3h
• Coding • 具体编码 2h 2h
• Code Review • 代码复审 1h 1h
• Test • 测试(自我测试,修改代码,提交修改) 3h 2h
Reporting 报告 2h 2h
• Test Repor • 测试报告 1h 0.5h
• Size Measurement • 计算工作量 1h 2h
• Postmortem & Process Improvement Plan • 事后总结, 并提出过程改进计划 1h 2h

二、码云地址

https://gitee.com/djh000/project-java/tree/master/20188522

三、基本流程图

四、各大板块核心内容编写

1.读取文件内容

String textStr = "";
		try {
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				
				StringBuffer sb = new StringBuffer();
                String lineTxt = null;

2.统计文件字符数

//统计文件的字符数
	public static int charCount(String content){
		return content.length();
	}

3.统计文件单词总数

// (至少以4个英文字母开头)
	public static int wordCount(String content) {
		int count = 0;
		String[] array = { ".", " ", "?", "!" };
		for (int i = 0; i < array.length; i++) {
			content = content.replace(array[i], ",");
		}

		String[] contentArray = content.split(",");
		for (int i = 0; i < contentArray.length; i++) {
			if (contentArray[i].length() >= 4) {
				count++;
			}
		}
		return count;
	}

4.判断有效行数

int count = 0;
		try {
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				
				StringBuffer sb = new StringBuffer();
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null)
                    sb.append(lineTxt).append("
");

5.统计个单词出现频率次数

String result = "";
		// 找出所有的单词
		String[] array = { ".", " ", "?", "!" };
		for (int i = 0; i < array.length; i++) {
			content = content.replace(array[i], ",");
		}
		String[] contentArray = content.split(",");
		// 遍历 记录
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (int i = 0; i < contentArray.length; i++) {
			String key = contentArray[i];
			// 转为小写
			String key_l = key.toLowerCase();
			if (!"".equals(key_l) && key_l.length()>=4) {
				Integer num = map.get(key_l);
				if (num == null || num == 0) {
					map.put(key_l, 1);
				} else if (num > 0) {
					map.put(key_l, num + 1);
				}
			}

6.最多输出10行

int length = 10;
		if(info.size() <= 10){
			length = info.size();
		}
		for (int j = 0; j < length; j++) {
            result = result + info.get(j).getKey() + ":" + info.get(j).getValue() + "
";
        }
		return result;

7.写入文件

public static void writeText(String filePath, String content, boolean flag) {
		try {
			File file = new File(filePath);
			if (file.exists()) {
				FileWriter fw = new FileWriter(file, flag); // flag=true 为追加模式
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write(content);
				bw.flush();
				bw.close();
				fw.close();
			}else{
				System.out.println("writeText方法提示: 找不到指定的文件.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

五、测试

六、心得

这次作业刚开始看起来很多很多,但后来发现把他分成很多个板块,也就简单多了,其实我认为这此作业更是考验我们得动手能力。

原文地址:https://www.cnblogs.com/djhxxx/p/14608687.html