当你学习了海明码(汉明码)的时候,有没有想过自己来写一个程序去实现这个奇怪的东西呢?可能很多人还不是很清楚这个海明码到底是什么东西,那就让我们来用我们的智慧来打败那个怪叔叔吧!Fighting。。。
1. 海明码的生成:
CODE:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 5 #define MAX 100 // The maximum length of binary string 6 7 /* cal int from char */ 8 int CalCharToInt(char c) 9 { 10 return (c - '0'); 11 } 12 13 /* cal r */ 14 int Cal_R(int k) 15 { 16 int r; 17 for (r = 0; pow(2.0, r) < k + r + 1; ++r) 18 { } // do nothing 19 20 return r; 21 } 22 23 int main() 24 { 25 int k, r; 26 char rein[MAX]; 27 while (1) 28 { 29 printf("k = "); 30 scanf("%d", &k); 31 getchar(); 32 printf("Information Code: "); 33 fgets(rein, MAX, stdin); 34 r = Cal_R(k); 35 //printf("k = %d, r = %d, s = %s\n", k, r, rein); 36 37 int inform_H[MAX], r_H[MAX]; 38 memset(inform_H, 0, sizeof(inform_H)); 39 memset(r_H, 0, sizeof(r_H)); 40 int i, t, m; 41 42 for (i = 1, t = 0, m = 1; i < k + r; ++i) // save rein[] to a int array rein_H[] 43 { 44 if (i == (int)pow(2.0, (m-1))) // location r_H[i+1] data is 0 45 { 46 inform_H[i] = 0; 47 ++m; 48 // printf("insert %d\n", i); 49 } 50 else 51 { 52 inform_H[i] = CalCharToInt(rein[t]); 53 ++t; 54 } 55 } 56 57 int sum; // save sum from r_H[i] 58 int temp; 59 for (i = 1; i <= r; ++i) // cal the real value of r_H[i] 60 { 61 temp = (int)pow(2.0, i-1); 62 for (t = 1, sum = 0; t <= k + r; ++t) 63 { 64 if ((t/temp)%2) // if this location is right 65 { 66 sum += inform_H[t]; 67 } 68 } 69 r_H[i] = sum % 2; // a even number check-up 70 // printf("%d\n", r_H[i]); 71 } 72 73 for (i = 1, t = 1; i <= k + r; ++i) 74 { 75 if (i == (int)pow(2.0, t-1)) 76 { 77 inform_H[i] = r_H[t]; // change to right at the right location 78 ++t; 79 } 80 } 81 82 printf("Hamming Code: "); 83 for (i = 1; i <= k + r; ++i) 84 { 85 printf("%d", inform_H[i]); 86 } 87 printf("\n\n"); 88 } 89 return 0; 90 }
2. 海明码的接收:
CODE:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 5 #define MAX 100 // The maximum length of binary string 6 7 /* cal int from char */ 8 int CalCharToInt(char c) 9 { 10 return (c - '0'); 11 } 12 13 /* cal r */ 14 int Cal_R(int k) 15 { 16 int r; 17 for (r = 0; pow(2.0, r) < k + r + 1; ++r) 18 { } // do nothing 19 20 return r; 21 } 22 23 int main() 24 { 25 int k, r; 26 char rein[MAX]; 27 while (1) 28 { 29 printf("k = "); 30 scanf("%d", &k); 31 getchar(); 32 printf("Receive Code: "); 33 fgets(rein, MAX, stdin); 34 r = Cal_R(k); 35 //printf("k = %d, r = %d, s = %s\n", k, r, rein); 36 37 int receive_H[MAX], r_H[MAX]; 38 memset(receive_H, 0, sizeof(receive_H)); 39 memset(r_H, 0, sizeof(r_H)); 40 int i, t, m; 41 int err; 42 43 for (i = 1; i <= k + r; ++i) // save rein[] to a int array rein_H[] 44 { 45 receive_H[i] = CalCharToInt(rein[i-1]); 46 } 47 48 int sum; // save sum from r_H[i] 49 int temp; 50 for (i = 1; i <= r; ++i) // cal the real value of r_H[i] 51 { 52 temp = (int)pow(2.0, i-1); 53 for (t = 1, sum = 0; t <= k + r; ++t) 54 { 55 if ((t/temp)%2) // if this location is right 56 { 57 sum += receive_H[t]; 58 } 59 } 60 r_H[i] = sum % 2; // a even number check-up 61 // printf("%d\n", r_H[i]); 62 } 63 64 //printf("%d,%d,%d,%d\n", r_H[1], r_H[2], r_H[3], r_H[4]); 65 66 for (i = 1, err = 0; i <= r; ++i) // cal err 67 { 68 err += ( (r_H[i]) * (int)pow(2.0, (i-1)) ); 69 } 70 printf("err = %d\n", err); 71 72 receive_H[err] = (receive_H[err] + 1) % 2; 73 74 printf("Information Code: "); 75 for (i = 1, t = 0; i <= k + r; ++i) 76 { 77 temp = (int)pow(2.0, (t)); 78 if (temp != i) // remove redundancy code 79 { 80 printf("%d", receive_H[i]); 81 //printf("\n%d", temp); 82 } 83 else ++t; 84 } 85 printf("\n\n"); 86 } 87 return 0; 88 }