我将程序附上来吧,可能有点冗长,大家帮忙看下:
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
int r[5];
/*************多项式相乘模块****************/
void pmul(int k[],int x,int t[],int y,int n[],int z)
{
int i,j;
for(i=0;i<z;i++)
n[i]=0;
for(i=0;i<x;i++)
for(j=0;j<y;j++)
n[i+j]=n[i+j]+k[i]*t[j];
return;
}
/*************多项式相除模块**************/
void pdiv(int n[],int x,int m[],int y,int r[],int l)
{
int i,k;
for(i=x-1;i>=0;i--)
if(n[i]!=0)
{
k=i;
printf("%d\n",k);
break;
}
while(k>=5)
{
for(i=k;i>=k-5;i--)
{
n[i]=n[i]^m[y-1-k+i];
}
for(i=k;i>=k-5;i--)
if(n[i]!=0)
{
k=i;
// printf("%d\n",k);
break;
}
}
for(i=l-1;i>=0;i--)
r[i]=n[i];
return;
}
void main()
{
int i,j,z;
char flag='Y';
while(flag=='Y')
{
int n[15];//整个码长,用于存放多项式相乘后各项的系数.
int k[10];//信息码系数数组.
int t[6]={0,0,0,0,0,1};//与信息码多项式相乘的多项式的各项的系数,即x^5.
FILE *fp;
fp=fopen("a.txt","w");
srand(time(0));
printf("The Cofficient of the information code is: \n");
for(i=0;i<10;i++)
{
k[i]=rand()%2;
printf("%d",k[i]);
}
printf("\n");
pmul(k,10,t,6,n,15);//多项式相乘模块的调用.
printf("The Cofficient of the total code is: \n");
for(i=0;i<15;i++)
{
printf("%d",n[i]);
fprintf(fp,"%d",n[i]);
}
fprintf(fp,"\n");
printf("\n");
fclose(fp);
int m[6]={1,0,1,0,1,1};
// static int r[5];
pdiv(n,15,m,6,r,5);//多项式相除模块的调用.
printf("The Cofficient of the array r is: \n");
for(i=0;i<=4;i++)
printf("%d",r[i]);
printf("\n");
/**********将文件中的数据读出来**********/
fp=fopen("a.txt","rb");
printf("The Cofficient that we read from the file is: \n");
// fscanf(fp,"%s",n);//读出文件中的二进制代码.
for(i=0;i<15;i++)
{
if(fscanf(fp,"%c",&n[i])==EOF)//改为每次读出一个字符.
break;
printf("%c",n[i]);//同样以字符格式输出.
}
printf("\n");
for(i=0;i<15;i++)
{
n[i]=n[i]-48;//将字符型转换成整形.
printf("%d",n[i]);
}
printf("\n");
fclose(fp);
/**********将余多项式中的各项系数赋值给信息多项式中的系数************/
for(i=0;i<=4;i++)
n[i]=r[i];
printf("The Cofficient that we need lastly is: \n");
for(i=0;i<15;i++)
printf("%d",n[i]);
printf("\n\n");
/*************对发送的码组加入误码,即定义为接收到的码组s[15]**************/
int s[15]; //接收到的码组;
printf("The Code Group that we recived is: \n");
for(i=0;i<=4;i++)
s[i]=n[i];
for(i=5;i<=14;i++)
s[i]=rand()%2; //加入误码过程.
for(i=0;i<=14;i++)
printf("%d",s[i]);
printf("\n");
/************根据接收到的码组来解得校验子q[5]*************/
int q[5];
pdiv(s,15,m,6,q,5); //调用多项式相除模块.
printf("Calibration sub- is: \n"); //输出校验子.
for(i=0;i<=4;i++)
printf("%d",q[i]);
printf("\n");
/************根据求得的校验子来判别接收是否有误******************/
for(i=0;i<=4;i++)
if(q[i]!=0)
{
printf("The Code Group that we recived isn't equal to the Code Group that we sent.\n\n");
break;
}
/************求生成矩阵G的多项式G(x)*******************/
static int a[10]; //对x^i进行数组定义;
int G[15];
FILE *fp1;
fp1=fopen("b.txt","w"); //将每一个生成的G(x)的系数数组输出到b.txt中;
for(i=9;i>=0;i--)
{
a[i]=1;
pmul(a,10,m,6,G,15);
a[i]=0; //每一次循环之后都要对a[10]重置0;
for(j=14;j>=0;j--)
fprintf(fp1,"%d ",G[j]); //输出到文件中;
fprintf(fp1,"\n");
}
fclose(fp1);
fp1=fopen("b.txt","r");
printf("The Matrix that we read from the file is: \n");
int b[10][15]; //将生成矩阵G从文件读出来并存放在定义的矩阵b中;
for(i=0;i<=9;i++)
{
for(j=0;j<=14;j++)
{
fscanf(fp1,"%d",&b[i][j]);
printf("%d ",b[i][j]);
}
printf("\n");
}
fclose(fp1);
/***********将生成矩阵G转换成典型生成矩阵b**************/
for(i=9;i>=0;i--)
for(j=i-1;j>=0;j--)
for(z=0;z<=14;z++)
{
if(b[j][i]==b[i][i]) //主要是消去对应位置上的1使其变为0;
b[j][z]=b[j][z]^b[i][z];
}
printf("The Matrix b that we convert from Matrix G is: \n");
for(i=0;i<=9;i++)
{
for(j=0;j<=14;j++)
printf("%d ",b[i][j]);
printf("\n");
}
/***********将典型生成矩阵转换成监督矩阵H**************/
int c[10][5],d[5][10];
printf("The Matrix Q is: \n");
for(i=0;i<=9;i++)
{
for(j=10;j<=14;j++)
{
c[i][j-10]=b[i][j];
printf("%d ",c[i][j-10]);
}
printf("\n");
}
printf("The Matrix P is: \n");
for(i=0;i<=4;i++)
{
for(j=0;j<=9;j++)
{
d[i][j]=c[j][i];
printf("%d ",d[i][j]);
}
printf("\n");
}
printf("The Monitoring Matrix H is: \n");
int H[5][15];
for(i=0;i<=4;i++)
{
for(j=0;j<=9;j++)
H[i][j]=d[i][j];
for(j=10;j<=14;j++)
{
if((i+10)==j)
H[i][j]=1;
else
H[i][j]=0;
}
}
for(i=0;i<=4;i++)
{
for(j=0;j<=14;j++)
printf("%d ",H[i][j]);
printf("\n");
}
/*************求解错误图样E:其中q[]=E[]*D(未完成)***************/
printf("The Transpose of Matrix H is: \n");
int D[15][5],E[15];
for(i=0;i<=14;i++)
{
for(j=0;j<=4;j++)
{
D[i][j]=H[j][i];
printf("%d ",D[i][j]);
}
printf("\n");
}
printf("Do you want to continue?If yes,press Y,if not,press N.\n");
scanf("%c",&flag);
getchar();
if(flag=='Y')
continue;
else break;
}
}