第二次作业

发布时间 2023-09-20 17:44:25作者: xiaoxxixix
这个作业属于哪个课程 首页 - 计算21级 - 集美大学 - 班级博客 - 博客园 (cnblogs.com)
这个作业要求在哪里 个人项目 - 作业 - 计算21级 - 班级博客 - 博客园 (cnblogs.com)
这个作业的目标 查重算法

PSP表格

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

链接

xiaocsz/202121394042 at main · xiaocsz/xiaocsz (github.com)

实现思路

使用了最长公共子序列(LCS)算法来计算两个文本的相似度。代码首先读取原文和抄袭版文本,然后计算它们之间的相似度,并将结果写入输出文件中。

代码

使用最长公共子序列(LCS)算法来计算两个字符串 str1str2 的相似度.

// 函数用于计算两个字符串的相似度
double calculateSimilarity(const char *str1, const char *str2) {
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    // 创建一个二维数组来存储子问题的解
    int dp[len1 + 1][len2 + 1];
    // 填充二维数组
    for (int i = 0; i <= len1; i++) {
        for (int j = 0; j <= len2; j++) {
            if (i == 0 || j == 0)
                dp[i][j] = 0;
            else if (str1[i - 1] == str2[j - 1])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else
                dp[i][j] = (dp[i - 1][j] > dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1];
        }
    }
    // 计算相似度
    double similarity = (double)dp[len1][len2] / len1;
    return similarity;
}

异常设置

    if (argc != 4) {
        printf("Usage: %s <original_file> <plagiarized_file> <output_file>\n", argv[0]);
        return 1;
    }
   // 打开原文文件
    FILE *original_file = fopen(original_file_path, "r");
    if (!original_file) {
        perror("Error opening original file");
        return 1;
    }

    // 打开抄袭版文件
    FILE *plagiarized_file = fopen(plagiarized_file_path, "r");
    if (!plagiarized_file) {
        perror("Error opening plagiarized file");
        fclose(original_file);
        return 1;
    }
    if (fgets(original_text, sizeof(original_text), original_file) == NULL) {
        perror("Error reading original file");
        fclose(original_file);
        fclose(plagiarized_file);
        return 1;
    }

    if (fgets(plagiarized_text, sizeof(plagiarized_text), plagiarized_file) == NULL) {
        perror("Error reading plagiarized file");
        fclose(original_file);
        fclose(plagiarized_file);
        return 1;
    }

测试

gcc main.cpp -o main
main.exe "D:\大三上\工程概论\2\orig.txt"  "D:\大三上\工程概论\2\orig_add.txt"  "D:\大三上\工程概论\2\答案.txt"