软工个人项目

发布时间 2023-09-17 16:28:31作者: 曾中港
软件工程 21计科四班
作业要求 https://edu.cnblogs.com/campus/gdgy/CSGrade21-34/homework/13023
仓库地址 https://github.com/2077435277/introduction_myself

PSP表格

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

实验准备环境

  • 编程语言和环境:jdk1.8
  • 编程工具IDE:Intellij IDEA
  • 第三方依赖:

主要代码实现

文件io类,TextIoUtil:

public class TextIoUtil {
/**
* 读出txt文件
* 传入文件绝对路径,将文件内容转化为 String字符串输出
*
* @param txtPath 文件路径
* @return 文件内容
*/
public static String readTxt(String txtPath) {
StringBuilder str = new StringBuilder();
String strLine;
// 将 txt文件按行读入 str中
File file = new File(txtPath);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 字符串拼接
while ((strLine = bufferedReader.readLine()) != null) {
str.append(strLine);
}
// 关闭资源
inputStreamReader.close();
bufferedReader.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}

/**
 * 写入txt文件
 * 传入内容、文件全路径名,将内容写入文件并换行
 *
 * @param txtElem 传入的内容
 * @param txtPath 写入的文件路径
 */
public static void writeTxt(double txtElem, String txtPath) {
    String str = Double.toString(txtElem);
    File file = new File(txtPath);
    FileWriter fileWriter = null;
    try {
        fileWriter = new FileWriter(file, true);
        fileWriter.write(str, 0, (str.length() > 3 ? 4 : str.length()));
        fileWriter.write("\r\n");
        // 关闭资源
        fileWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
海明距离计算方法:

public static int getHammingDistance(String simHash1, String simHash2) {
int distance = 0;
if (simHash1.length() != simHash2.length()) {
// 出错,返回-1
distance = -1;
} else {
for (int i = 0; i < simHash1.length(); i++) {
// 每一位进行比较
if (simHash1.charAt(i) != simHash2.charAt(i)) {
distance++;
}
}
}
return distance;
}
相似度计算:

public static double getSimilarity(String simHash1, String simHash2) {
// 通过 simHash1 和 simHash2 获得它们的海明距离
int distance = getHammingDistance(simHash1, simHash2);
// 通过海明距离计算出相似度,并返回
return 0.01 * (100 - distance * 100 / 128);
}

结果测试

public class Main {
public static void main(String[] args) throws NoSuchAlgorithmException {
// 从命令行输入的路径名读取对应的文件,将文件的内容转化为对应的字符串
String path1 = "C:\Users\26913\Desktop\demo\src\main\resources\str0.txt";
String path2 = "C:\Users\26913\Desktop\demo\src\main\resources\str1.txt";
String str0 = TextIoUtil.readTxt(path1);
String str1 = TextIoUtil.readTxt(path2);
String pathRes = "C:\Users\26913\Desktop\demo\src\main\resources\res.txt";
// 由字符串得出对应的 simHash值
String simHash0 = SimHashUtil.getSimHash(str0);
String simHash1 = SimHashUtil.getSimHash(str1);
// 由 simHash值求出相似度
double similarity = SimHashUtil.getSimilarity(simHash0, simHash1);
// 把相似度写入最后的结果文件中
TextIoUtil.writeTxt(similarity, pathRes);
// 退出程序
System.exit(0);
}
}