111

#include <iostream>
#include <vector>

using namespace std;

// 定义监督矩阵
vector<vector<int>> parityMatrix = {
    {1, 1, 0, 1},
    {1, 0, 1, 1},
    {1, 0, 0, 0},
    {0, 1, 1, 1},
    {0, 1, 0, 0},
    {0, 0, 1, 0},
    {0, 0, 0, 1}
};

// 定义生成矩阵
vector<vector<int>> generatorMatrix = {
    {1, 0, 0, 0},
    {0, 1, 0, 0},
    {0, 0, 1, 0},
    {0, 0, 0, 1},
    {1, 1, 0, 1},
    {1, 0, 1, 1},
    {0, 1, 1, 1}
};

// 获取矩阵的转置
vector<vector<int>> getTranspose(const vector<vector<int>>& matrix) {
    vector<vector<int>> transpose(matrix[0].size(), vector<int>(matrix.size()));
    for (int i = 0; i < matrix.size(); i++) {
        for (int j = 0; j < matrix[i].size(); j++) {
            transpose[j][i] = matrix[i][j];
        }
    }
    return transpose;
}

// 汉明码的检错纠错
void hammingCodeCorrection(const string& code) {
    vector<int> receivedCode;
    for (char c : code) {
        receivedCode.push_back(c - '0');
    }
    
    vector<int> syndrome(3);
    vector<int> errorPosition(3);
    for (int i = 0; i < syndrome.size(); i++) {
        for (int j = 0; j < receivedCode.size(); j++) {
            syndrome[i] += receivedCode[j] * parityMatrix[j][i];
        }
        syndrome[i] %= 2;
        if (syndrome[i] == 1) {
            errorPosition[i] = 1;
        }
        else {
            errorPosition[i] = 0;
        }
    }
    
    int errorIndex = 0;
    for (int i = 2; i >= 0; i--) {
        errorIndex = errorIndex * 2 + errorPosition[i];
    }
    
    if (errorIndex != 0) {
        cout << "检测到错误位置: " << errorIndex << endl;
        receivedCode[errorIndex - 1] = 1 - receivedCode[errorIndex - 1];
        cout << "纠错后的数据: ";
        for (int i : receivedCode) {
            cout << i;
        }
        cout << endl;
    }
    else {
        cout << "未检测到错误" << endl;
        cout << "数据: " << code << endl;
    }
}

// 汉明码的增余码的检错纠错
void hammingCodeChecksumCorrection(const string& code) {
    vector<int> receivedCode;
    for (char c : code) {
        receivedCode.push_back(c - '0');
    }
    
    vector<int> syndrome(3);
    for (int i = 0; i < syndrome.size(); i++) {
        for (int j = 0; j < receivedCode.size(); j++) {
            syndrome[i] += receivedCode[j] * parityMatrix[j][i + 4];
        }
        syndrome[i] %= 2;
    }
    
    int errorIndex = 0;
    for (int i = 2; i >= 0; i--) {
        errorIndex = errorIndex * 2 + syndrome[i];
    }
    
    if (errorIndex != 0) {
        cout << "检测到错误位置: " << errorIndex + 4 << endl;
        receivedCode[errorIndex + 3] = 1 - receivedCode[errorIndex + 3];
        cout << "纠错后的数据: ";
        for (int i : receivedCode) {
            cout << i;
        }
        cout << endl;
    }
    else {
        cout << "未检测到错误" << endl;
        cout << "数据: " << code << endl;
    }
}

// 连续编码和连续译码
void continuousCodingDecoding() {
    char choice;
    do {
        cout << "请输入4位二进制原始数据: ";
        string data;
        cin >> data;
        
        vector<int> originalData;
        for (char c : data) {
            originalData.push_back(c - '0');
        }
        
        vector<int> encodedData(7);
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 4; j++) {
                encodedData[i] += originalData[j] * generatorMatrix[i][j];
            }
            encodedData[i] %= 2;
        }
        
        cout << "编码后的数据: ";
        for (int i : encodedData) {
            cout << i;
        }
        cout << endl;
        
        cout << "是否继续编码和译码?(Y/N)";
        cin >> choice;
    } while (choice == 'Y' || choice == 'y');
}

int main() {
    int option;
    do {
        cout << "请选择实验内容:" << endl;
        cout << "1. (7, 4)汉明码的检错纠错" << endl;
        cout << "2. (7, 4)汉明码的增余码的检错纠错" << endl;
        cout << "3. (7, 4)汉明码的连续编码和连续译码" << endl;
        cout << "4. 退出" << endl;
        cout << "请选择操作: ";
        cin >> option;

        switch (option) {
            case 1: {
                string code;
                cout << "请输入7位二进制码: ";
                cin >> code;
                hammingCodeCorrection(code);
                break;
            }
            case 2: {
                string code;
                cout << "请输入7位二进制码: ";
                cin >> code;
                hammingCodeChecksumCorrection(code);
                break;
            }
            case 3: {
                continuousCodingDecoding();
                break;
            }
            case 4:
                break;
            default:
                cout << "无效操作,请重新选择" << endl;
                break;
        }
    } while (option != 4);

    return 0;
}