循环冗余码c语言程序实现,计算机网络中CRC冗余码的c语言实现

直接贴代码了,功能比较全吧!

/*

* crc.cpp

*

* Created on: 2010-3-30

* Author: wzw

*/

#include "stdio.h"

#include "conio.h"

void getMagAppendZeroBit(int Frame[],int Length_Frame,int Length_Gx,int Out[]);

void getRemainder(int Frame[],int Length_Frame,int Gx[],int Length_Gx,int Remainder[]);

void NandCal(int X[],int Gx[],int Length,int Out[]);

void AppendOneToY(int X,int Y[],int Length_Y,int Out[]);

void PrintRemainder(int Remainder[],int Length_Remainder);

void PrintFrameAndGx(int FrameMatrix[],int Length_Frame,int GxMatrix[],int Length_Gx);

void PrintTransmitterFrame(int Frame[],int Length_Frame,int Remainder[],int Length_Remainder);

int main(){

int LengthOfFrame;

int LengthOfGx;

printf("Input The Length Of Frame:/n");

scanf("%d",&LengthOfFrame);

int FrameMatrix[LengthOfFrame];

for (int i=0;i

printf("Input The %dth Bit/n",i+1);

int temp;

temp=(int)getche()-48;

if((temp==0)||(temp==1)){ //确保输入数据时0或1

FrameMatrix[i]=temp;

printf("/n");

}

else{

printf("/nInput Must Be 0 Or 1/n");

i--;

}

}

printf("Press C/c For Custom Or Other Keys for Standard Generator?/n");

char ChooseMode;

ChooseMode=getche();

printf("/n");

if(ChooseMode=='C'||ChooseMode=='c'){//自定义Gx

printf("Input The Length Of G(x):/n");

scanf("%d",&LengthOfGx);

int GxMatrix[LengthOfGx];

for (int i=0;i

printf("Input The %dth Bit/n",i+1);

int temp;

temp=(int)getche()-48;

printf("/n");

if((temp==0)||(temp==1)){ //确保输入数据时0或1

if((i==0)&&(temp==0)){

printf("The First Bit Must Be 1/n");

i--;

}

else if((i==(LengthOfGx-1))&&(temp==0)){

printf("The Last Bit Must Be 1/n");

i--;

}

else{

GxMatrix[i]=temp;

}

}

else{

printf("Input Must Be 0 Or 1/n");

i--;

}

}

PrintFrameAndGx(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx);

int RemainderMatrix[LengthOfGx-1];

getRemainder(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx,RemainderMatrix);

PrintRemainder(RemainderMatrix,LengthOfGx-1);

PrintTransmitterFrame(FrameMatrix,LengthOfFrame,RemainderMatrix,LengthOfGx-1);

}

else{//通用标准Gx

printf("/nPress '1' for CRC8 or '2' for CRC12 or '3' for CRC16 or '4' for CRC_CCITT or Other keys for CRC32?/n");

char ChooseStandardCRC;

ChooseStandardCRC=getche();

if(ChooseStandardCRC=='1'){//crc8

LengthOfGx=9;

int GxMatrix[]={1,0,0,0,0,0,1,1,1};

PrintFrameAndGx(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx);

int RemainderMatrix[LengthOfGx-1];

getRemainder(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx,RemainderMatrix);

PrintRemainder(RemainderMatrix,LengthOfGx-1);

PrintTransmitterFrame(FrameMatrix,LengthOfFrame,RemainderMatrix,LengthOfGx-1);

}

else if(ChooseStandardCRC=='2'){//crc12

LengthOfGx=13;

int GxMatrix[]={1,1,0,0,0,0,0,0,0,1,1,1,1};

PrintFrameAndGx(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx);

int RemainderMatrix[LengthOfGx-1];

getRemainder(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx,RemainderMatrix);

PrintRemainder(RemainderMatrix,LengthOfGx-1);

PrintTransmitterFrame(FrameMatrix,LengthOfFrame,RemainderMatrix,LengthOfGx-1);

}

else if(ChooseStandardCRC=='3'){//crc16

LengthOfGx=17;

int GxMatrix[]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1};

PrintFrameAndGx(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx);

int RemainderMatrix[LengthOfGx-1];

getRemainder(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx,RemainderMatrix);

PrintRemainder(RemainderMatrix,LengthOfGx-1);

PrintTransmitterFrame(FrameMatrix,LengthOfFrame,RemainderMatrix,LengthOfGx-1);

}

else if(ChooseStandardCRC=='4'){//crc ccitt

LengthOfGx=17;

int GxMatrix[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};

PrintFrameAndGx(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx);

int RemainderMatrix[LengthOfGx-1];

getRemainder(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx,RemainderMatrix);

PrintRemainder(RemainderMatrix,LengthOfGx-1);

PrintTransmitterFrame(FrameMatrix,LengthOfFrame,RemainderMatrix,LengthOfGx-1);

}

else{//crc32

LengthOfGx=33;

int GxMatrix[]={1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1};

PrintFrameAndGx(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx);

int RemainderMatrix[LengthOfGx-1];

getRemainder(FrameMatrix,LengthOfFrame,GxMatrix,LengthOfGx,RemainderMatrix);

PrintRemainder(RemainderMatrix,LengthOfGx-1);

PrintTransmitterFrame(FrameMatrix,LengthOfFrame,RemainderMatrix,LengthOfGx-1);

}

}

printf("/nPress Any Key To Exit");

getch();

}

void getRemainder(int Frame[],int Length_Frame,int Gx[],int Length_Gx,int Remainder[]){

int Length_AppendZeroBit=Length_Frame+Length_Gx-1;

int AppendZeroBit[Length_AppendZeroBit];

getMagAppendZeroBit(Frame,Length_Frame,Length_Gx,AppendZeroBit);

int MagEveryCal[Length_Gx];

for(int i=0;i

MagEveryCal[i]=Frame[i];

}

NandCal(MagEveryCal,Gx,Length_Gx,Remainder);

for(int i=Length_Gx;i

AppendOneToY(AppendZeroBit[i],Remainder,Length_Gx-1,MagEveryCal);

NandCal(MagEveryCal,Gx,Length_Gx,Remainder);

}

}

void PrintRemainder(int Remainder[],int Length_Remainder){

printf("/nRemainder Code Is:/n");

for(int i=0;i

printf("%d",Remainder[i]);

}

printf("/n");

}

void PrintFrameAndGx(int FrameMatrix[],int Length_Frame,int GxMatrix[],int Length_Gx){

printf("/nFrame To Be Sent Is:/n");

for(int i=0;i

printf("%d",FrameMatrix[i]);

}

printf("/nChoosed G(x) Is:/n");

for(int i=0;i

printf("%d",GxMatrix[i]);

}

printf("/n");

}

void PrintTransmitterFrame(int Frame[],int Length_Frame,int Remainder[],int Length_Remainder){

printf("/nTransmitter Frame Is :/n");

for(int i=0;i

printf("%d",Frame[i]);

}

for(int i=0;i

printf("%d",Remainder[i]);

}

printf("/n");

}

void NandCal(int X[],int Gx[],int Length,int Out[]){

for(int i=1;i

if(X[0]==1){

Out[i-1]=(X[i]==Gx[i])?0:1;

}

else{

Out[i-1]=X[i];

}

}

}

void getMagAppendZeroBit(int Frame[],int Length_Frame,int Length_Gx,int Out[]){ //将帧加上0序列后输出

for(int i=0;i

Out[i]=Frame[i];

}

for(int i =0;i

Out[Length_Frame+i]=0;

}

}

void AppendOneToY(int X,int Y[],int Length_Y,int Out[]){

for(int i=0;i

Out[i]=Y[i];

}

Out[Length_Y]=X;

}

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值