C ++ Program Implementation of Haiming code

Source: Internet
Author: User

//////////////////////////////////////// /////////////////////////
/// The following is a self-written Program for finding any binary code Haiming code (running in the C ++ environment)
//////////////////////////////////////// ////////////////////////
///
# Include <iostream>
# Include <fstream>
# Include <stdio. h>
# Define n 1270
# Define K 1200
# Define g 70
// # Define Haiming
Using namespace STD;
Int Len [N];
Int I _2_r (int A) // evaluate the power of a 2 ^ N power
{
Int COUNT = 0;
If (A = 1)
Return 0;
While (A> 1)
{
A = A/2;
Count ++;
}
Return count;
}
Void in_to_group (int A [], int B, int N) // Add it to each group
{
Int I;
For (I = 1; I <= N; I ++)
If (A [I] =-1)
A [I] = B;
}
Int _ 2_r_gt_k_r (int K, int R) // Judge 2 ^ r> = K + R + 1
{
Int I, S = 1;
For (I = 1; I <= r; I ++)
S = 2 * s;
If (S> = K + R + 1)
Return 1;
Else
Return 0;
}
Int is_ I _2_r (int I) // judge whether I is 2 ^ n digits
{
If (I = 1)
Return 1;
If (I % 2! = 0)
Return 0;
While (I % 2 = 0 & I! = 1)
I = I/2;
If (I = 1)
Return 1;
Return 0;
}
Int int_to_invdec (int A [], int N) // calculates the binary code of an integer in reverse order.
{
Int I = 1; // calculate the Checkpoint number
While (N)
{
A [I ++] = n % 2;
N/= 2;
}
Return I-1; // number of returned digits
}
Int count1 (int A [], int N) // calculate the number of 1
{
Int I, Count = 0;
For (I = 1; I <= N; I ++)
If (A [I] = 1)
Count ++;
If (count % 2 = 0)
Return 1;
Return 0;
}
Void main ()
{
# Ifdef Haiming
Freopen ("Haiming code .txt", "r", stdin );
# Endif
Int infor [N], I, K, R, findr = 1;
Int group [g] [K], G, J; // number of groups used for group Detection 1
Int A [n], a_l;
Printf ("/N input number of valid information bits K (0 = <k <= 1200 ):");
Scanf ("% d", & K );
I = 2;
While (findr) // find the R value of the number of digits to be detected
{
R = I;
If (_ 2_r_gt_k_r (K, R ))
Findr = 0;
I ++;
}
Printf ("/n requires % d-bit detection bit./N", R );
Printf ("/N input message code:/N ");
For (I = 1; I <= K + R; I ++)
{
Infor [I] =-1;
If (! Is_ I _2_r (I ))
Scanf ("% d", & infor [I]);
}
Printf ("/N ");
For (I = 1; I <= r; I ++)
For (j = 1; j <= K; j ++)
Group [I] [J] =-1; // *** initialize each group as-1 to indicate null information.
For (I = 1; I <= K; I ++)
Len [I] = 0; // *** the number of elements in each group is 0.
Printf ("Hamming Code (even verification):/N ");
For (I = 1; I <= K + R; I ++ )//!!! * Grouping starts from the first bit after the detection bit is added (key)
{
A_l = int_to_invdec (A, I); // ** convert the bits into binary numbers in reverse order and store them in array a []. The length of the array is returned.
For (G = 1; G <= r; G ++) // groups R.
For (j = 1; j <= a_l; j ++)
If (A [J] = 1) // ** after the query is converted, the subscript of 1 in a [] is assigned to the 1st group, 2 to the second group, etc)
If (G = J) // if the group number is the same as the subscript,
{
In_to_group (group [g], infor [I], Len [g]); // ** add this information code to the G group
Len [g] ++; // * the length of the G group increases by 1
Break;
}
}
For (I = 1; I <= K + R; I ++) // ****** checks the number of 1 in each group.
{If (is_ I _2_r (I) & count1 (group [I _2_r (I) + 1], Len [I _2_r (I) + 1]) // If the bid is 2 ^ N, it indicates the detection bit.
Infor [I] = 0; // *** and the number of 1 in the group is an even number, add 0 to the detection bit for verification
If (is_ I _2_r (I )&&! Count1 (group [I _2_r (I) + 1], Len [I _2_r (I) + 1]) // If the BSN is 2 ^ N, it indicates the detection bits
Infor [I] = 1; // *** and the number of 1 in the group is an odd number, add 1 to the detection bit for verification
}
For (I = 1; I <= K + R; I ++)
Cout <infor [I] <""; // output the compiled Haiming code
Printf ("/N ");
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.