直接贴代码了,功能比较全吧!
/*
* 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;
}