直接贴代码了 //============================================================================ // Name : Hamming.cpp // Author : wzw // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <stdio.h> #include <math.h> #include <conio.h> int isRightParity(int,int[]); void getColumn(int,int,int,int[]); void getParity(int[],int,int,int[],int[]);//得到数据与校验码组合码 void getCodedMessage(int[],int[],int,int[]); int isPowerOfTwo(int); int getParityBit(int); void getNextParityCode(int,int,int[]); int main(){ int data_bit; //数据位长 printf("Input The Bits Of Data:/n"); scanf("%d",&data_bit); int data[data_bit]; for (int i=0;i<data_bit;i++){ //循环输入数据 printf("Input The %dth Bit/n",i+1); int temp; temp=(int)getche()-48; if((temp==0)||(temp==1)){ //确保输入数据时0或1 data[i]=temp; printf("/n"); } else{ printf("/nInput Must Be 0 Or 1/n"); i--; } } printf("The Data To Be Coded Is: ");//显示输入的待传输数据 for(int i=0;i<data_bit;i++){ printf("%d",data[i]); } int parity_bit; //校验码位长matrix column;matrix row is parity+data_bit; parity_bit=getParityBit(data_bit); //根据数据位长选择最短的检验码,返回位长 printf("/nChoose Bits Of Parity Is: %d /n",parity_bit); int parity_code[parity_bit]; //校验码 int coded_message[parity_bit+data_bit]; //数据和校验码融合之后的数据 getParity(data,data_bit,parity_bit,parity_code,coded_message); printf("Parity Is: "); for(int i=0;i<parity_bit;i++){ printf("%d",parity_code[i]); } printf("/nCoded Message Is: "); for(int i=0;i<parity_bit+data_bit;i++){ printf("%d",coded_message[i]); } printf("/n/nPress Any Key Exit..."); getch(); } void getColumn(int index,int data_bit,int parity_bit,int column[]){ for(int i=0;i<(data_bit+parity_bit);i++){ int parity_row[parity_bit]; getNextParityCode(parity_bit,i,parity_row); column[i]=parity_row[index]; } } void getParity(int data[],int data_bit,int parity_bit,int parity_code[],int coded_message[]){ //计算校验码P int flag[parity_bit]; int column[data_bit+parity_bit]; //矩阵一列 for(int m=0;m<(int)pow(2,parity_bit);m++){ getNextParityCode(parity_bit,m,parity_code); getCodedMessage(data,parity_code,(parity_bit+data_bit),coded_message); for(int i=0;i<parity_bit;i++){ flag[i]=0; } for(int i=0;i<parity_bit;i++){ getColumn(i,data_bit,parity_bit,column); int matrix_product=0;//乘积 for(int j=0;j<(data_bit+parity_bit);j++){ matrix_product=matrix_product+coded_message[j]*column[j]; } if(matrix_product%2==0){ flag[i]=1; continue; } else{ flag[i]=0; break; } } if(isRightParity(parity_bit,flag)==1){ break; } else{ continue; } } } int isRightParity(int length,int flag[]){ int temp=1; for(int i=0;i<length;i++){ temp=temp*flag[i]; } if(temp==1){ return 1; } else{ return 0; } } void getCodedMessage(int data[],int parity_code[],int length,int coded_message[]){ int index_data,index_parity; index_data=index_parity=0; for(int i=0;i<length;i++){ if(isPowerOfTwo(i+1)==1){ coded_message[i]=parity_code[index_parity]; index_parity++; } else{ coded_message[i]=data[index_data]; index_data++; } } } int isPowerOfTwo(int num){//num is power幂 of 2 or not int ispoweroftwo; int i=0; while(1){ int pow2i=(int)pow(2,(double)i); if(num<pow2i){ ispoweroftwo=0; break; } else if(num==pow2i){ ispoweroftwo=1; break; } else if(num>pow2i){ i++; } } return ispoweroftwo; } void getNextParityCode(int parity_bit,int index,int parity_code[]){//change index+1 to BCD int temp=index+1; for(int i=0;i<parity_bit;i++){ parity_code[parity_bit-i-1]=temp%2; temp=temp/2; } } int getParityBit(int data_bit){//get least bit of parity int paritybit=0; int power; while(1){ paritybit++; power=(int)pow(2,(double)paritybit)-1; if((paritybit+data_bit)<=power){ break; } } return paritybit; } 转载自: http://50vip.com