我试图找到基本字符串之间的汉明距离。基本字符串中的节点S_ {i}的状态是二进制数据的形式。下面的代码在时刻t0-t14生成14个数据快照。我如何为C中的200个基本字符串生成数据,这样我就可以获得更多的数据点。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
FILE *bp = NULL;
bp = fopen("hamming.txt","w");
if(bp == NULL)
printf("Error!\n");
FILE *pr = NULL;
pr = fopen("pairs.txt","w");
if(pr == NULL)
printf("Error in opening file pr!\n");
else
srand(unsigned(time(NULL)));
int M = RAND_MAX / 2;
int N = 8; // No.of nodes in the network : // test run for 14 strings:
int n = 10; // snapshots of data:
int s[N], ss[N-1];
double t = 0.0;
double dt = 1;
double delta = 0.42857;
fprintf(bp,"s[i]\t s[i+1] \t s[i+2]\t s[i+3]\t s[i+4]\t s[i+5]
\tpairs\t time \t\n");
fprintf(bp,"----------------------------------------------------\n");
for(t=0;t < n-1; )
{
for(int i=0; i<N; i++)
{
if(rand() > M)
s[i] = 1;
else
s[i] = 0;
}
for(int i=0, j=0; i<N-1; i++)
{
if(!s[i])
{
if(s[i+1])
{
if(s[i+2])
{
if(s[i+3])
{
if(s[i+4])
{
if(s[i+5])
{
ss[j] = 1;
}
else
{
ss[j] = 0;}
j++;
}
}
}
}
}
if(s[i])
{
if(!s[i+1])
{
if(!s[i+2])
{
if(!s[i+3])
{
if(!s[i+4])
{
if(!s[i+5])
{
ss[j] = 0;
}
else
{
ss[j] = -1;
}
j++;
}
}
}
}
}
if(s[i])
{
if(s[i+1])
{
if(s[i+2])
{
if(s[i+3])
{
if(s[i+4])
{
if(s[i+5])
{
ss[j] = -1;
}
else
{
ss[j] = 0;
}
j++;
}
}
}
}
}
//choose only the pairs "00" and "01":
if(s[i] == 0 && s[i+1] == 0)
{
//if(s[i] == 0 && s[i+2] == 1){
fprintf(pr,"(%d%d)\t\t%0.1lf\n",s[i+1],s[i],t);
}
fprintf(bp,"%d\t%d\t%d\t%d\t%d\t%d\t%d%d\t
%0.1lf\t\n",s[i],s[i+1],s[i+2],s[i+3],s[i+4],s[i+5],s[i],s[i+1],t);
t = t+dt;
j++;
}
}
return 0;
fclose(bp);
fclose(pr);
}