⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig.12.cpp

📁 用于交通流研究的Wolfram模型的程序
💻 CPP
字号:
// Fig.12.cpp 
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int a[200],b[8], i,temp[200],s=0,t;
int match(int l,int m,int n);
float p;
int _tmain(int argc, _TCHAR* argv[])
{
	int j,n,time_steps;
	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;
	cout<<"(";
	while(R>0)
	{
		b[k]=R%2;
		R=R/2;		
		k--;
	}
	for(k=0;k<8;k++)
		cout<<b[k];
	cout<<")"<<endl;
	//	将十进制R转化为二进制;
	cout.width(4);
	cout<<"t="<<"0  ";
	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;
		if(a[i]==1)
			s++;
	}
	cout<<"p="<<(float)(s)/(float)(n)<<endl;
	//初始时刻中间格点值为1,其它格点值为0;

	for(j=1;j<=time_steps;j++)
	{
		t=0;
		cout.width(4);
		cout<<"t="<<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]);
			}
			if(a[i]==1)
				t++;
		}
		p=(float)(t)/(float)(n);
		cout<<"p="<<p<<endl;    
		FILE *fp;
		if((fp=fopen("E:\\file.dat","a+"))==NULL)
		{
			cout<<"Can not open this file."<<endl;
			exit(0);
		}
		fprintf(fp,"%d,%f\n",j,p);
		fclose(fp);
	}

	//每个格点同步地按局域规则进行演化;

	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 + -