?? code-decode.cpp
字號:
#include "stdio.h"
void main()
{
int code_in;
int i,j,a;
int code_out[15];
int mid[15];
/* code */
int g[15]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x365,0x1AF,0x35E,0x1D9,0x3B2};
printf("輸入原始信息數據(16進制):");
scanf("%x",&code_in);
for(i=0;i<15;i++)
{
a=code_in&g[i];
for(j=0;j<10;j++)
{
mid[j]=a&1;
a=a>>1;
}
code_out[i]=mid[0];
for(j=1;j<10;j++)
code_out[i]=code_out[i]^mid[j];
}
printf("糾錯編碼后的輸出比特流: ");
for(i=15;i>0;i--)
printf("%d",code_out[i-1]);
/* decode and correct */
for (i=0;i<15;i++)
{
code_out[i]=0;
mid[i]=0;
}
code_in=0;
i=0;
j=0;
a=0;
int check_out[5];
int errorflag,matchbit,matchflag,comparebit;
int check[5]={0x765,0x9AF,0x135E,0x21D9,0x43B2}; /*the check array*/
printf("\n輸入接受端收到的原始數據(16進制):");
scanf("%x",&code_in); /*input the data received at the receiver*/
a=code_in;
for (i=0;i<15;i++) /*change the data received into bits*/
{
code_out[i]=a&1;
a=a>>1;
}
for(i=0;i<5;i++) /*caculate the cheksum*/
{
a=code_in&check[i];
for(j=0;j<15;j++) /*get every bit of a */
{
mid[j]=a&1;
a=a>>1;
}
check_out[i]=mid[0];
for(j=1;j<15;j++)
check_out[i]=check_out[i]^mid[j]; /*add the bits in mode 2*/
}
i=0;
errorflag=0;
do
{
if (check_out[i]==1)
{
errorflag=1;
i=5;
}
else
i++;
}
while (i<5); /* check if the check is all 0 */
if (errorflag==1)
{
i=0;
matchbit=100; /* look for the same line*/
do
{
j=0;
matchflag=1;
do
{
comparebit=(check[j]>>i)&1; /* get the [j][i] bit of the array*/
if (check_out[j]!=comparebit)
{
j=5;
matchflag=0;
} /*not coincident,jump out at once*/
else
j++;
}
while (j<5);
if (matchflag==1)
{
matchbit=i;
i=15;
} /*if found,jump out at once*/
else
i++;
}
while (i<15);
if (matchbit<=14)
{
code_out[matchbit]=code_out[matchbit]^1; /*correct the error bit*/
printf("第%d位數據出錯!\n",matchbit+1);
}
else
printf("錯誤但不可糾!\n");
}
else printf("傳輸正確或錯誤不可檢!\n");
printf("還原的信息比特為:");
for (i=10;i>0;i--) /*only print the information bits*/
printf("%d",code_out[i-1]);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -