这个问题建立在几个其他职位。我明白,如果这与互联网上的大多数人无关,但在这一点上,就像其他人一样,我陷入困境,无法找到逻辑上的错误。这个问题需要检查指定的汉明代码是否有单位错误,并报告/纠正错误。下面是该程序可以这样做:汉明码 - 错误检测和更正
#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.任何帮助将不胜感激。
我只是尝试另一种手写海明码:1000110,无误码,而上面的程序告诉我,有一点错误在纠正码为1000011的位2中。 –
嗯,问题'1000110'中的代码和您评论'1000110'中的代码完全相同。因此,第1步似乎是要找到一些代码,在其中您确实知道正确的答案。 – user3386109
这是我的不好,也许我没有正确写出问题。我刚刚运行了原始汉明码。要检查的输入是1000110.正确的答案是1100110,第6位有错误。我得到1000010,第3位出现错误。这是你所指的? –