📄 fig.13.cpp
字号:
// Fig.13.cpp
#include "stdafx.h"
#include<iostream.h>
#include <stdlib.h>
#include <time.h>
int a[200],b[8], i,temp[200];
int match(int l,int m,int n);
float p;
int _tmain(int argc, _TCHAR* argv[])
{
int j,n,time_steps,s[200],r;
double C,e,f,g;
cout<<"Please enter the number of sites n: "<<endl;
cin>>n;
cout<<"Please enter time_steps: "<<endl;
cin>>time_steps;
int R,k=7;
cout<<"Please enter the rule R: "<<endl;
cin>>R;
cout<<"Please enter the probability p: "<<endl;
cin>>p;
while(R>0)
{
b[k]=R%2;
R=R/2;
k--;
}
// 将十进制R转化为二进制;
srand( (unsigned)time( NULL ) );
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
for(i=0;i<n;i++)
{
if(rand()<((RAND_MAX+1)*p-1))
a[i]=1;
else
a[i]=0;
}
//初始状态随机分布;
for(j=1;j<=time_steps;j++)
{
for(i=0;i<n;i++)
temp[i]=a[i];
for(i=0;i<n;i++)
{
if(i==0)
match(temp[n-1],temp[i],temp[i+1]);
else
{
if(i==(n-1))
match(temp[i-1],temp[i],temp[0]);
//周期性边界条件;
else
match(temp[i-1],temp[i],temp[i+1]);
}
}
}
//每个格点同步地按局域规则进行演化;
for(i=0;i<n;i++)
{
if(a[i]==1)
s[i]=1;
else
s[i]=-1;
}
for(r=0;r<=40;r++)
{
cout<<"r="<<r<<" ";
e=0;
f=0;
g=0;
for(i=0;i<n;i++)
{
e=e+s[i];
if(i>=0&&i<(n-r))
{
f=f+s[i+r];
g=g+s[i]*s[i+r];
}
if(i>=(n-r)&&i<n)
{
f=f+s[i+r-n];
g=g+s[i]*s[i+r-n];
}
}
C=g/n-e/n*f/n;
cout<<"C="<<C<<endl;
}
return 0;
}
int match(int l,int m,int n)
{
if(l==1&&m==1&&n)
a[i]=b[0];
if(l==1&&m==1&&n==0)
a[i]=b[1];
if(l==1&&m==0&&n==1)
a[i]=b[2];
if(l==1&&m==0&&n==0)
a[i]=b[3];
if(l==0&&m==1&&n==1)
a[i]=b[4];
if(l==0&&m==1&&n==0)
a[i]=b[5];
if(l==0&&m==0&&n==1)
a[i]=b[6];
if(l==0&&m==0&&n==0)
a[i]=b[7];
return a[i];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -