工程概论第二次作业

GitHub连接

GitHub

作业要求

工程概论 班级链接
作业要求 作业的要求链接
作业的目标 了解查重代码,熟悉github

需求

题目:论文查重

描述如下:

设计一个论文查重算法,给出一个原文文件和一个在这份原文上经过了增删改的抄袭版论文的文件,在答案文件中输出其重复率。

原文示例:今天的天气非常晴朗,阳光明媚,温度适宜。预计今天的最高气温为25摄氏度,风力较轻。大家可以外出活动或者野餐,享受美好的天气。
抄袭版示例:今日天气晴朗,阳光明媚,气温适宜。预计今天最高气温约为26摄氏度,风力较弱。适合户外活动,大家可以出门散步或举办野餐等活动,享受美好的天气。
要求输入输出采用文件输入输出,规范如下:

从命令行参数给出:论文原文的文件的绝对路径。
从命令行参数给出:抄袭版论文的文件的绝对路径。
从命令行参数给出:输出的答案文件的绝对路径。
我们提供一份样例,课堂上下发,上传到班级群,使用方法是:orig.txt是原文,其他orig_add.txt等均为抄袭版论文。

注意:答案文件中输出的答案为浮点型,精确到小数点后两位

PSP表格

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

整体项目框架

异常处理

读到空的时候进行处理,当BufferedReader对象读取到空行时,循环将退出,不再执行循环体中的语句。

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
             String line;
             while ((line = reader.readLine()) != null) {
                 sb.append(line);
                 sb.append(System.lineSeparator());
             }
         } catch (IOException e) {
             e.printStackTrace();
         }

结果

代码

点击查看代码
package 查重;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class 查重系统 {
    public static void main(String[] args) {
    	String filePath1 = "C:\\Users\\72411\\Desktop\\工程概论第二次作业\\orig.txt";
    	String filePath2 = "C:\\Users\\72411\\Desktop\\工程概论第二次作业\\orig_add.txt";

    	 String text1 = readFile(filePath1);
         String text2 = readFile(filePath2);

         double similarity = calculateSimilarity(text1, text2);
         String formattedSimilarity = String.format("%.2f%%", similarity*100);
         System.out.println("文本相似度:" + formattedSimilarity);

     }

     public static String readFile(String filePath) {
         StringBuilder sb = new StringBuilder();
         try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
             String line;
             while ((line = reader.readLine()) != null) {
                 sb.append(line);
                 sb.append(System.lineSeparator());
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
         return sb.toString();
     }

     public static double calculateSimilarity(String text1, String text2) {
         int matchingCount = findMatchingCount(text1, text2);
         int totalLength = Math.max(text1.length(), text2.length());
         return (double) matchingCount / totalLength * 100;
     }

     public static int findMatchingCount(String text1, String text2) {
         int count = 0;
         int text1Length = text1.length();
         int text2Length = text2.length();
         int minLen = Math.min(text1Length, text2Length);

         for (int i = 0; i < minLen; i++) {
             if (text1.charAt(i) == text2.charAt(i)) {
                 count++;
             }
         }

         return count;
     }
 }
posted @ 2023-09-20 14:27  nnnnn-k  阅读(76)  评论(0)    收藏  举报