#include <stdio.h>
#include <stdlib.h>
bool dayin=true; //是否打印
int main(){
int i,k,g=0;
int n1=0,n2=0;
int good=0,bad=0;
double rate;
(dayin)? printf("打印具体数据\n"):printf("不打印具体数据\n\n");
FILE *fpRead=fopen("D:\\Alice.dat","r");
if(fpRead==NULL)
{
return 111;
}
fseek(fpRead, 0, SEEK_END);
n1 = ftell(fpRead);
unsigned char *left = (unsigned char*)malloc(n1);
fseek(fpRead, 0, SEEK_SET);
fread(left, n1, 1, fpRead);
/*printf("left数组数据为:\n");
for(i=0;i<n1;i++){
printf("%02X ", left[i]);
}*/
printf("\n共%d个\n\n",n1);
fclose(fpRead);
FILE *fpRead2=fopen("D:\\Bob.dat","r");
if(fpRead2==NULL)
{
free(left);
return 222;
}
fseek(fpRead2, 0, SEEK_END);
n2 = ftell(fpRead2);
unsigned char *right = (unsigned char*)malloc(n2);
fseek(fpRead2, 0, SEEK_SET);
fread(right, n2, 1, fpRead2);
/*printf("right数组数据为:\n");
for(i=0;i<n2;i++){
printf("%02X ", right[i]);
}*/
printf("\n共%d个\n\n",n2);
fclose(fpRead2);
printf("检查数据是否正确,按回车键进行下一步。\n");
getchar();
if(n1!=n2){
printf("!两边数据个数不等!\n");
free(left);
free(right);
return 333;
}
if(n1%2!=0){
printf("!二进制数据个数必须为偶数!\n");
free(left);
free(right);
return 444;
}
printf("开始比对\n");
for(i=0;i<=n1-2;i++){ //有n1个数,对应编号left[n-1],为二进制中的第二个数,故只要循环至n1-2即可
if(i%2==1) continue; //每两个01为一组
if(left[i]!=right[i]) { //若第一个数不同则跳过
g++;
continue;
}
if(left[i+1]==right[i+1]) good++;
else {
bad++;
if(dayin==true)printf("第%d组数不同,分别为%d%d和%d%d\n",i/2+1,left[i],left[i+1],right[i],right[i+1]);
}
}
if((g+good+bad)!=(n1/2)){
printf("比对过程中数据遗漏?");
free(left);
free(right);
return 555;
}
rate=(double)bad/(double)(good+bad)*100;
printf("结果:\n基不同的共%d组。相同的二进制共%d组,不同的有%d组,误码率为%g%%\n",g,good,bad,rate);
printf("比对结束\n");
free(left);
free(right);
system("pause");
return 666;
}