简单的C语言汉明码纠错程序,已调整

 2024-02-26 03:03:54  阅读 0

汉明码是一种具有纠错功能的校验码,可用C语言实现。 下面是生成和检查汉明码的简单 C 代码示例: 生成汉明码:```# # # int main(){int data[100], h[100], c, i, j, k, n , p, q, r;("输入数据位:");scanf("%d", &n);for (i = 0; i < n; i++){("输入位 %d : ", i + 1);scanf("%d", &data[i]);}for (i = 0, j = 0; i < n; i++){if (pow(2, j) == i + j + 1){h[i] = 0;j++;}else{h[i] = data[k];k++;}}for (i = 0; i < j; i++){c = 0;p = pow( 2、i);for (k = p - 1; k < n; k += p){for (q = 0; q < p; q++){if (h[k + q] == 1 ){c++ ;}}}if (c % 2 == 0){h[p - 1] = 0;}else{h[p - 1] = 1;}}("代码是:");for (i = n + j - 1; i >= 0; i--){if (pow(2, j) != i + j + 1){("%d", h[i]);}else {j- -;}} 0;}```检查汉明码:```# # # int main(){int data[100], h[100], c, i, j, k, n, p , q, r;("输入数据位:");scanf("%d", &n);for (i = 0; i < n; i++){("输入位 %d: ", i + 1); scanf("%d", &data[i]);}j = log2(n) + 1;for (i = 0; i < n + j; i++){if (pow(2, j) == i + j + 1){h[i] = 0;}else{h[i] = 数据[k];k++;}}for (i = 0; 我<j; i++){c = 0;p = pow(2, i);for (k = p - 1;k < n + j;k += p){for (q = 0;q < p;q++){if (h[k + q] == 1){c++;}}}if (c % 2 == 0){r = 0;}else{r = 1;}h[p - 1] = r;}( “代码是:”); for (i = n + j - 1; i >= 0; i--){if (pow(2, j) != i + j + 1){("%d", h[i]);} }c = 0;for (i = 0; i < j; i++){p = pow(2, i);if (h[p - 1] == 1){c += p;}}if (c == 0){("\n没有错误。\n");}else{("\ 位 %d.\n", c);} 0;}```

标签: 示例 生成 纠错

如本站内容信息有侵犯到您的权益请联系我们删除,谢谢!!


Copyright © 2020 All Rights Reserved 京ICP5741267-1号 统计代码