此问题建立在其他一些帖子的基础上。我理解这是否与互联网上的大多数人无关,但在这一点上,就像其他人一样,我陷入困境,无法在逻辑中找到错误。此问题需要检查指定的汉明码以获得单比特错误并报告/更正错误。以下是执行此操作的程序:
#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 :(得分:2)
我无法完全遵循您的代码应该如何工作。所以这是一个简单的实现,它计算代码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 );
}