add: data-structure-lab & compiler-lab

This commit is contained in:
2024-10-30 17:23:52 +08:00
commit eb8c4fa451
35 changed files with 4266 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
//
// Created by ricardo on 22-12-11.
//
#ifndef ZIP_UNZIP_SEARCH_CONST_H
#define ZIP_UNZIP_SEARCH_CONST_H
// ASCII码的长度
#define ASCII_LENGTH 128
#endif //ZIP_UNZIP_SEARCH_CONST_H

View File

@@ -0,0 +1,82 @@
//
// Created by ricardo on 22-12-11.
//
#ifndef ZIP_UNZIP_SEARCH_FILE_IO_H
#define ZIP_UNZIP_SEARCH_FILE_IO_H
#include "string"
/**
* 文件元信息
*/
struct MetaData
{
/**
* 哈夫曼树节点数组长度
*/
int HuffmanNodeLength;
/**
* 哈夫曼树根节点长度
*/
int HuffmanRoot;
/**
* 文件中最后一个缓冲区被使用的位数
*/
int LastBufferUsedLength;
};
class FileIO
{
public:
/**
* 统计文件的字符的出现频率
* @param fileName 文件名称
* @return 字符频率数组 需要delete
*/
static int* ReadCharFrequency(const std::string& fileName);
/**
* 写入压缩文件
* @param inputFile 被压缩文件名称
* @param outputFile 输出压缩文件名称
*/
static void WriteZipFile(const std::string& inputFile, const std::string& outputFile);
/**
* 写入解压缩文件
* @param inputFile 压缩文件名称
* @param outputFile 解压文件名称
*/
static void WriteUnzipFile(const std::string& inputFile, const std::string& outputFile);
/**
* 计算文件的压缩率
* @param inputFileName
* @param outputFileName
* @return
*/
static double CalculateZipRate(const std::string& inputFileName, const std::string& outputFileName);
};
class BinaryBuffer
{
public:
explicit BinaryBuffer(std::string& inputFileName);
~BinaryBuffer();
char read();
int position = 0;
private:
FILE* file = nullptr;
int buffer;
int bufferPos;
bool readFinishedFlag;
};
#endif //ZIP_UNZIP_SEARCH_FILE_IO_H

View File

@@ -0,0 +1,103 @@
//
// Created by ricardo on 22-12-11.
//
#ifndef ZIP_UNZIP_SEARCH_HUFFMAN_H
#define ZIP_UNZIP_SEARCH_HUFFMAN_H
#include "vector"
#include "array"
#include "const.h"
/**
* 哈夫曼树中的节点结构体
*/
struct HuffmanNode
{
/**
* 节点的编号
*/
int id;
/**
* 节点表示的字符
* 如果不是叶子节点
* 值为-1
*/
char data;
/**
* 字符在文件中出现的频率
* 也就是哈夫曼树中节点的权值
*/
int frequency;
/**
* 左子结点在节点数组中的索引
*/
int lIndex;
/**
* 右子结点在节点中的索引
*/
int rIndex;
};
class HuffmanCode
{
public:
/**
* 树中节点列表
*/
std::vector<HuffmanNode>* nodes = new std::vector<HuffmanNode>();
/**
* 哈夫曼树根节点索引
*/
int root = -1;
/**
* 从字符的频率数组出发创建
* @param frequencyArray
*/
explicit HuffmanCode(const int * frequencyArray);
/**
* 从哈夫曼数组节点出发创建
* @param nodeArray 哈夫曼节点数组
* @param length 节点数组的长度
*/
HuffmanCode(HuffmanNode *nodeArray, int length);
~HuffmanCode();
/**
* 创建哈夫曼树
*/
void createHuffmanTree();
/**
* 打印哈夫曼树
*/
void printHuffmanTree();
/**
* 得到哈夫曼编码
* @return 哈夫曼编码字典
*/
std::array<std::vector<char>, ASCII_LENGTH> * getHuffmanCode();
/**
* 打印哈夫曼编码字典
* @param dictionary 字典
*/
static void printHuffmanCode(const std::array<std::vector<char>, ASCII_LENGTH>& dictionary);
private:
/**
* 对森林列表按权值排序
* @param forests
*/
static void sortForests(std::vector<HuffmanNode>& forests);
void printHuffmanTreeR(int nodeId);
void getHuffmanCodeR(std::array<std::vector<char>, ASCII_LENGTH> &dictionary, int nodeId, std::vector<char> &code);
};
#endif //ZIP_UNZIP_SEARCH_HUFFMAN_H

View File

@@ -0,0 +1,31 @@
//
// Created by ricardo on 22-12-11.
//
#ifndef ZIP_UNZIP_SEARCH_LOGGING_H
#define ZIP_UNZIP_SEARCH_LOGGING_H
#include "string"
class Logging
{
public:
/**
* 输出信息
* @param info
*/
static void LoggingInfo(const std::string& info);
/**
* 输出警告
* @param warning
*/
static void LoggingWarning(const std::string& warning);
/**
* 输出错误
* @param error
*/
static void LoggingError(const std::string& error);
};
#endif //ZIP_UNZIP_SEARCH_LOGGING_H

View File

@@ -0,0 +1,55 @@
//
// Created by ricardo on 22-12-16.
//
#ifndef ZIP_UNZIP_SEARCH_SEARCH_H
#define ZIP_UNZIP_SEARCH_SEARCH_H
#include "vector"
#include "array"
#include "string"
/**
* BM算法搜索实现类
*/
class BMSearch
{
public:
explicit BMSearch(std::vector<char>& sample);
~BMSearch();
/**
* 匹配二进制文件
* @param fileName 指向需要进行匹配的二进制文件 需要读取元信息和哈夫曼数组
*/
void matchFile(std::string &fileName);
private:
// 坏字符规则数组
// 字符串为01串
int* badCharArray;
// 好后缀规则数组
int* goodSuffixArray;
std::vector<char>* sample;
/**
* 生成坏字符数组
* @param s 模板字符串
*/
void generateBrokenCharArray(std::vector<char>& s);
/**
* 生成好后缀数组
* @param s 模板字符串
*/
void generateGoodSuffixArray(std::vector<char>& s);
static int max(int a, int b);
};
void SearchInFile(char* fileName, char* sample);
#endif //ZIP_UNZIP_SEARCH_SEARCH_H