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

📄 fig.8.cpp

📁 用于交通流研究的Wolfram模型的程序
💻 CPP
字号:
//FIG.8.
#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,c[200],q;
	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<<"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;
			cout<<"*";
		}
		else
		{
			a[i]=0;
			cout<<" ";
		}		
	}
	cout<<endl;
	*/
		for(i=0;i<n;i++)
	{
		c[i]=0;		
	}
	for(i=0;i<int(n*p);i++)
	{
		q=int(rand()*(n-1)/RAND_MAX);
		if(c[q]==0)
			c[q]=1;
		else
			i=i-1;
	}
	for(i=0;i<n;i++)
	{
		if(c[i]==1)
		{
			a[i]=1;
			cout<<"*";
		}
		else
		{
			a[i]=0;
			cout<<" ";
		}
	}	
	cout<<endl;

	//初始时刻中间格点值为1,其它格点值为0;

	for(j=1;j<=time_steps;j++)
	{
		cout.width(4);
		cout<<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]);
			}
		}
		cout<<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];
	if(a[i]==1)
		cout<<"*";
	else
		cout<<" ";
	return a[i];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -