2015-11-06 129 views
0

这个问题建立在几个其他职位。我明白,如果这与互联网上的大多数人无关,但在这一点上,就像其他人一样,我陷入困境,无法找到逻辑上的错误。这个问题需要检查指定的汉明代码是否有单位错误,并报告/纠正错误。下面是该程序可以这样做:汉明码 - 错误检测和更正

#include <string.h> 
#include <math.h> 
#include <stdlib.h> 
#include <stdio.h> 

    /** Initializing the global variables */ 
    int MaxLength; 
    int length; 
    int parity; 
    // Initialize the hamming string with a random or NULL memory address 
    char *HammingString=NULL; 

    /** Function to enter the values */ 
    void EnterParameters(int *length, int *parity) 
    { 
     printf("Enter the maximum length: "); 
     /** %d reads an integer to be stored in an int. This integer can be signed */ 
     scanf("%d", length); 
     printf("Enter the parity (0=even, 1=odd): "); 
     /** %d reads an integer to be stored in an int. This integer can be signed */ 
     scanf("%d", parity); 
    } 

    void CheckHamming(char *HammingString, int parity) 
    { 
     // Initializing the local variables i, j, k, start, length, ParityNumber 
     int i, j, k, start, length, ParityNumber; 
     printf("Enter the Hamming code: "); 
     scanf("%s", HammingString); 

     int ErrorBit = 0;      // Initialize the error bit 
     length = strlen(HammingString);   // The strlen computes the length of a string up to, but not including the terminating null character 
     length--; 
     if (length > MaxLength) 
     { 
      printf("\n** Invalid Entry - Exceeds Maximum Code Length of %d\n\n", MaxLength); 
      return; 
     } 
     ParityNumber = ceil(log(length)/log(2)); // The ceil function returns the smallest integer that is greater than or equal to 'x'. 

     for(i = 0; i < ParityNumber; i++) 
     { 
      // pow returns x raised to the power y. In this case, 2 raised to the power i. 
      start = pow(2, i); 
      int ParityCheck = parity; 

      for(j = start; j < length; j=j+(2*start)) 
      { 
       for(k = j; (k < ((2*j) - 1)) && (k < length); k++) 
       { 
        ParityCheck ^= (HammingString[length - k] - '0'); 
       } // End the k for-loop 
      } // End the j for-loop 

       ErrorBit = ErrorBit + (ParityCheck * start); 
      } // End the i for-loop 

     if(ErrorBit == 0) 
     { 
      printf("No error \n"); 
     } 
     else 
     { 
      printf("There is an error in bit: %d\n", ErrorBit); 
      if(HammingString[length - ErrorBit] == '0') 
      { 
       HammingString[length - ErrorBit] = '1'; 
      } 
      else 
      { 
       HammingString[length - ErrorBit] = '0'; 
      } 

      printf("The corrected Hamming code is: %s \n", HammingString); 
     } 
    } // End CheckHamming 

    int main() 
    { 

     int parity; 
     int choice = 0; 
      printf("Error detection/correction: \n"); 
      printf("----------------------------\n"); 
      printf("1) Enter parameters \n"); 
      printf("2) Check Hamming code \n"); 
      printf("3) Exit \n"); 
      printf("\nEnter selection: "); 
      scanf("%d", &choice); 

      while (choice != 3) 
      { 
       if (choice == 1) 
       { 
        EnterParameters(&MaxLength, &parity); 
        HammingString = (char*) malloc (MaxLength * sizeof(char)); 
        main(); 
       } 
       else if (choice == 2) 
       { 
        CheckHamming(HammingString, parity); 
        main(); 
       } 
       else 
       { 
        printf("Valid options are 1, 2, or 3. Quitting program. \n"); 
        exit(0); 
       }  
      }//end while 
      exit(0); 
    }//end main 

如果海明码:输入1000110,手工计算错误就出来一个错误,但6与该修正的代码是1100110.这个代码显示了一个错误位3与正确的代码是1000010.任何帮助将不胜感激。

+0

我只是尝试另一种手写海明码:1000110,无误码,而上面的程序告诉我,有一点错误在纠正码为1000011的位2中。 –

+0

嗯,问题'1000110'中的代码和您评论'1000110'中的代码完全相同。因此,第1步似乎是要找到一些代码,在其中您确实知道正确的答案。 – user3386109

+0

这是我的不好,也许我没有正确写出问题。我刚刚运行了原始汉明码。要检查的输入是1000110.正确的答案是1100110,第6位有错误。我得到1000010,第3位出现错误。这是你所指的? –

回答

0

我不能完全遵循你的代码应该如何工作。所以这是一个简单的实现,它计算代码1000110的综合征。从程序的输出为6,即误差在6位

#include <stdio.h> 

int main(void) 
{    // 7654321 
    char input[] = "1000110"; 
    int parity = 0; 
    for (int mask = 4; mask; mask >>= 1) 
    { 
     for (int bit = 1; bit <= 7; bit++) 
      if (bit & mask) 
       if (input[7-bit] == '1') 
        parity ^= mask; 
    } 
    printf("%d\n", parity); 
} 
+0

感谢您的示例。我的代码应该读取任意长度的字符串并计算它上面的汉明码。如果没有错误,则应该报告,如果有错误,它应该报告位值并将其更正。我试图找出如何附上一个工作示例,因为我有一个作为参考点。 –

+0

像这样:输入最大长度:12 输入奇偶校验(0 =偶数,1 =奇数):0输入的选择:2 输入汉明码:100011001010010 \t ***无效条目 - 超出最大码的12 长度输入的选择:2 输入汉明码:1000110 \t ***有位错误:6 ***校正汉明码是:1100110 –