WordCount

WordCount

代码链接:https://github.com/sliyoxn/wc
在线浏览:https://sliyoxn.github.io/wc/

一、题目描述

实现一个简单而完整的软件工具(源程序特征统计程序)。
进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。
进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。

二、WC 项目要求

wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。
实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:程序处理用户需求的模式为:wc.exe [parameter] [file_name]

三、代码分析

关键代码

  • 计算代码各个指标的入口函数
_calResult(text) {
	if (text.length === 0) {
		return {
			charCount : 0,
			lineCount : 0,
			wordCount : 0,
			emptyLineCount : 0,
			commentCount : 0,
			codeLineCount : 0
		};
	}
	// 处理某些文件的换行是\r\n
	text = text.replace(/\r\n/gm, "\n");
	let lines = text.split("\n");
	let words = text.match(/[a-zA-Z$_][a-zA-Z0-9$_]*/gm);
	// 字符数
	let charCount = text.length;
	// 行数
	let lineCount = lines.length;
	// 单词数
	let wordCount = words ? words.length : 0;
	// 空行数
	let emptyLineCount = this._calEmptyLine(text);
	// 注释行数
	let commentCount = this._calCommentCount(text);
	// 代码行数
	let codeLineCount = lineCount - emptyLineCount - commentCount;
	return {
		charCount,
		lineCount,
		wordCount,
		emptyLineCount,
		commentCount,
		codeLineCount
	}
},
		
  • 空行计算
_calEmptyLine(text) {
	let lines = this._sliceMulComment(text).split("\n");
	let emptyLineCount = 0;
	lines.forEach((line, index) => {
         // 匹配有无不能出现的字符
		let hasForbiddenCharacter = /[a-zA-Z0-9$_]+/gm.test(line);
         // 匹配可以出现的字符是否出现次数多于1 
		let hasMoreThanOneNoEmptyCharacter = /[^a-zA-Z0-9$_\s]{2,}/gm.test(line);
		if (!hasForbiddenCharacter && !hasMoreThanOneNoEmptyCharacter) {
			emptyLineCount++;
		}
	});
	return emptyLineCount;
},
  • 注释计算
_calCommentCount(text) {
	// 裁去多行注释
	let filterText = this._sliceMulComment(this._removeStr(text));
	// 多行注释的行数
	let mulCommentLength = this._removeStr(text).split("\n").length - filterText.split("\n").length;
	let singleCommentCount = 0;
	// 裁去字符串
	let lines = filterText.split("\n");
	lines.forEach((line, index) => {
		let flag = /\/\//gm.test(line);
		if (flag) {
			singleCommentCount++;
		}
	});
	return singleCommentCount + mulCommentLength;
},

四、项目测试

  • 单文件

  • 多文件

  • nodeJS测试核心代码

五、PSP

PSP Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 15 20
· Estimate 估计这个任务需要多少时间 10 10
Development 开发 1200 1400
Analysis 需求分析 (包括学习新技术) 20 20
·Design Spec 生成设计文档 10 10
·Design Review 设计复审 (和同事审核设计文档) 10 10
· Coding Standard 代码规范 (为目前的开发制定合适的规范) 10 10
·Design 具体设计 10 10
·Coding 具体编码 910 1120
·Code Review 代码复审 10 10
·Test 测试(自我测试,修改代码,提交修改) 220 210
Reporting 报告 30 40
·Test Report -测试报告 10 20
·Size Measurement 计算工作量 10 10
·Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 10 10
合计 1245 1460

六、项目小结

这次项目带我实践了一个软工流程开发流程,收获良多,但是有些bug没有修复,有些遗憾。

posted @ 2020-03-18 13:48  sorena  阅读(325)  评论(0编辑  收藏  举报