demo3/demo/main.c

64 lines
1.4 KiB
C
Raw Normal View History

2024-11-30 02:15:30 +00:00
#include "hamming.h"
#define DATA_SIZE 10231
int mainn() {
unsigned char data[DATA_SIZE], newData[DATA_SIZE];
unsigned char parity[3] = {0};
// 初始化测试数据
for(int i = 0; i < DATA_SIZE; i++) {
data[i] = i & 0xff;
newData[i] = data[i];
}
// 计算校验位
calculateParity(data, DATA_SIZE, parity);
size_t k = 0;
for (size_t i = 0; i < DATA_SIZE; i++)
{
// 注入错误
newData[i] ^= (1 << 7);
// 检测并纠正错误
int result = detectAndCorrect(newData, DATA_SIZE, parity);
// 输出结果
// printf("错误检测结果: ");
switch(result) {
case 0:
printf("无错误\n");
break;
case 1:
// printf("错误已纠正\n");
k++;
break;
case -1:
// printf("校验位错误\n");
k=-1;
break;
case -2:
printf("检测到多位错误\n");
k=-1;
break;
}
if (k == -1)
{
printf("数据恢复失败\n");
break;
}
if (memcmp(newData, data, DATA_SIZE) == 0) {
// printf("恢复后数据与原始数据一致\n");
} else {
printf("恢复后数据与原始数据不一致\n");
}
if ( i == DATA_SIZE - 1)
{
printf("数据恢复完毕,k=%d\n",k);
}
}
return 0;
}