📄 fec.cpp
字号:
#include <stdio.h>
#include "iostream.h"
main()
{
// int code_in,j;
// int code_out[15];
// int mid[15];
int input,proc;
int i,a,b,c,d,e,err,temp,ecc=0;
proc=1;
a=b=c=d=e=0;
cout<<"请输入信息(0~0x3ff):";
cin>>input;
//FEC ENCODE*******************************************************
for(i=0;i<10;i++)
{
temp=e^((input&proc)>>i);
e=temp^d;
d=c;
c=temp^b;
b=a;
a=temp;
proc<<=1;
}
cout<<"进行编码后输出为:";
if(a)input|=16384;
if(b)input|=8192;
if(c)input|=4096;
if(d)input|=2048;
if(e)input|=1024;
for(proc=16384,i=0;i<15;i++)
{
if(input&proc)cout<<"1";else cout<<"0";
proc>>=1;
}
//FEC DECODE*******************************************************
/* 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*/
/* int check[5]={000011101100101b, */
/* 000100110101111b, */
/* 001001101011110b, */
/* 010000111011001b, */
/* 100001110110010b } */
/*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=9;i>=0;i--) /*only print the information bits*/
/* printf("%d",code_out[i]);*/
a=b=c=d=e=0;
cout<<endl<<"请输入接收的信息:";
cin>>input;temp=0;proc=1;
for(proc=1,i=0;i<15;i++)
{
//err=(a^1)&(b^1)&(c)&(d)&(e^1);
//if(i<15){temp=(e^err^((input&proc)>>i));proc<<=1;}
//else {temp=e^err^0;}
//if(err)input^=(err<<(i-15));
temp=e^((input&proc)>>i);
e=temp^d;
d=c;
c=temp^b;
b=a;
a=temp;
proc<<=1;
}
if(a)ecc|=16;
if(b)ecc|=8;
if(c)ecc|=4;
if(d)ecc|=2;
if(e)ecc|=1;
proc=1;
if(ecc)
{
if(ecc<15)input^=(proc<<(ecc-1));else cout<<"不可纠正错误";
}
//proc=1;
//ecc=(a<<4)|(b<<3)|(c<<2)|(d<<1)|e;
//if(ecc!=0)input^=(proc<<(ecc-1));
cout<<"译码后输出信息为:";
cout<<' '<<' '<<' '<<' '<<' ';
for(proc=512,i=0;i<10;i++)
{
if(input&proc)cout<<"1";else cout<<"0";
proc>>=1;
}
cout<<endl;
cout<<a<<b<<c<<d<<e;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -