回复 5 楼 azzbcc
#include<stdio.h>
#include<math.h>
#define n_x_num
#define n_state_num
#define n 6
void cal_fre(unsigned char state[n_state_num][n_x_num],unsigned char *out);
void state_out(unsigned int init,unsigned char x[][n_x_num],unsigned char *temp);
void cal_sort(struct temp_state_data *a,int m);
void cal_choutou_i(struct temp_state_data *data,int n_choutou,unsigned int temp_num[64][2],unsigned char state[][31],unsigned
char *out);
struct temp_state_data {
unsigned char state[6];
float state_data[2];};
int main()
{
unsigned int init=0x55abcdef;
unsigned char out[15]={0x3a,0x39,0xcf,0xc8,0x78,0xb0,0xba,0x1e,0x6b,0x81,0xc1,0xdb,0x10,0x11,0xcf},
x[n_state_num][n_x_num],
temp[n_state_num];
state_out(init,x,temp);
cal_fre(x,temp);
return 0;
}
void state_out(unsigned int init,unsigned char x[][n_x_num],unsigned char *temp)
{
unsigned int a,b;
int i=0,j;
for(;i<n_state_num;i++)
{
for(j=0;j<n_x_num;j++)
x[i][j]=((init<<j)&1);
a=(init>>2)&1 ;
b=(init>>30)&1 ;
a=a^b^1;
temp[i]=a;
init=init<<1;
init=a&init;
}
}
void cal_fre(unsigned char state[n_state_num][n_x_num],unsigned char *out)
{
unsigned int temp_num[64][2]={0},
struct temp_state_data temp_data[470];
int i,j,k,l,m=0,num=2;
float temp,temp_1,temp_2,temp_3,data_x2=0,data_chahe=0;
for(i=0;i<n_x_num;i++)
for(j=i+1;j<n_x_num;j++)
{
for(k=0;k<n_state_num;k++)
{
unsigned char a=(state[k][i]);
a=a<<1;
a=a^(state[k][j]);
temp_num[a][out[k]]++;
}
for(l=0;l<4;l++)//4=2^2
{
temp_1=(float)((temp_num[l][0]-temp_num[l][1])*(temp_num[l][0]-temp_num[l][1]));
data_x2=data_x2+temp_1;
temp_2=(float)(temp_num[l][0]-temp_num[l][1]);
temp_3=(float)(temp_num[l][0]+temp_num[l][1]);
if(temp_3)
data_chahe=data_chahe+(float)(temp_2/temp_3);
temp_num[l][0]=0;
temp_num[l][1]=0;
}
temp_data[m].state[0]=i;
temp_data[m].state[1]=j;
temp_data[m].state_data[0]=data_x2;
temp_data[m].state_data[1]=data_chahe;
m++;
cal_sort(temp_data,num);
}
num++;
for(i=3;i<7;i++)
{
cal_choutou_i(temp_data,i,temp_num,state,out);
cal_sort(temp_data,i);
}
}
void cal_choutou_i(struct temp_state_data *data,int n_choutou,unsigned int temp_num[64][2],unsigned char state[][31],unsigned
char *out)
{
struct temp_state_data temp[5];
int i,j,k,l,m,temp_1,temp_2=0;
for(i=0;i<5;i++)
temp[i]=data[i];
for(temp_1=0,i=0;i<5;i++)
{
for(j=0;temp_1<n_x_num-n_choutou;j++,temp_1++)
{
if(j<temp[i].state[0])
{
m=0;
for(k=n_choutou-1;k>0;k--)
data[temp_2+j].state[k]=data[temp_2+j].state[k-1];
data[i].state[m]=j;
}
else if(j>temp[i].state[n_choutou-2])
{
m=n_choutou-1;
data[temp_2+j].state[m]=j;
}
else
for(k=0;k<n_choutou;k++)
if(j>temp[i].state[k])
{
if(j<temp[i].state[k+1])
{
l=k;
k=n_choutou-1;
for(;k>l;k--)
data[temp_2+j].state[k]=data[temp_2+j].state[k-1];
data[temp_2+j].state[l]=j;
break;
}
}
else if(j==temp[i].state[k])
{
j--;
continue;
}
}
temp_2=temp_2+n_x_num+1-n_choutou;
}
float data_x2=0,data_chahe=0,temp_3,temp_4;
l=0;
for(i=0;i<temp_2;i++)
{
for(j=0;j<n_state_num;j++)
{
unsigned int a=state[j][data[i].state[0]];
for(k=0;k<n_choutou;k++)
{
a=a<<1;
a=a^state[j][data[i].state[k]];
}
temp_num[a][out[k]]++;
}
for(l=0;l<(int)pow(2.0,n_choutou);l++)//2^n_choutou
{
temp_3=(float)((temp_num[l][0]-temp_num[l][1])*(temp_num[l][0]-temp_num[l][1]));
data_x2=data_x2+temp_1;
temp_3=(float)(temp_num[l][0]-temp_num[l][1]);
temp_4=(float)(temp_num[l][0]+temp_num[l][1]);
if(temp_4)
data_chahe=data_chahe+(float)(temp_3/temp_4);
temp_num[l][0]=0;
temp_num[l][1]=0;
}
data[i].state_data[0]=data_x2;
data[i].state_data[1]=data_chahe;
}
}
void cal_sort(struct temp_state_data *a,int m)
{
struct temp_state_data temp;
int i,j,k;
if(m==2)
k=465;
else
k=5*(32-m);
for(i=0;i<5;i++)
{
for(j=i+1;j<k;j++)
{
while(a[i].state_data[0]==a[j].state_data[0]&&a[i].state==a[j].state)
{
a[j]=a[k-1];
k--;
}
if(a[i].state_data[0]<a[j].state_data[0])
{
temp=a[i];
a[i]=a[j];
a[j]=a[i];
}
}
}
}