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

📄 roundabout_showview.cpp

📁 09年数学建模美赛A题程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// roundabout_showView.cpp : implementation of the CRoundabout_showView class
//

#include "stdafx.h"

#include "roundabout_show.h"

#include "roundabout_showDoc.h"
#include "roundabout_showView.h"
#include "Set.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRoundabout_showView
struct car{
	int index;
	int v;
	int des;
	int status;
	int entertime;
};
struct cell{
	bool iscar;
	car mtcar;
};
bool isshow=true;
int conterminal=4,L=100,C=140,runtime=3600*5,vmax=7,cmax=5,vinit=3,gap=1,carnum=0,p1=60,p2=95,p3=95,p4=100,p5=100,p6=100;
int p7=100,p8=100;
int diji=-1,sec=5,stime=5;
int redtime=0,greentime=50,signal[10];
cell cel[10000];
cell oldcel[10000];
int outroad[10];
int inroad[10];
int total,totalstay=0;
int num_of_create=0;
int num_of_leave=0;
int num_of_enter=0;
CStdioFile out1,out2;
int last1=0,last2=0,last3=0;
IMPLEMENT_DYNCREATE(CRoundabout_showView, CView)

BEGIN_MESSAGE_MAP(CRoundabout_showView, CView)
	//{{AFX_MSG_MAP(CRoundabout_showView)
	ON_COMMAND(ID_BEGIN, OnBegin)
	ON_COMMAND(IDSET, OnSet)
	ON_COMMAND(IDSTAT, OnStat)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRoundabout_showView construction/destruction

CRoundabout_showView::CRoundabout_showView()
{
	// TODO: add construction code here
	isfirst=1;
	out1.Open("data1.txt",CFile::modeCreate | CFile::modeWrite);
	out2.Open("data2.txt",CFile::modeCreate | CFile::modeWrite);
	total=conterminal*4*L+C*2;
	outroad[1]=C-1;inroad[1]=1;
	int dis=(C-4*conterminal)/conterminal;
	for(int i=2;i<=conterminal;i++){
		outroad[i]=inroad[i-1]+dis+2;
		inroad[i]=outroad[i]+2;
	}
	for(i=1;i<=total;i++)
		cel[i].iscar=false;

}

CRoundabout_showView::~CRoundabout_showView()
{
	out1.Close();
	out2.Close();
}

BOOL CRoundabout_showView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CRoundabout_showView drawing

void CRoundabout_showView::OnDraw(CDC* pDC)
{
	CRoundabout_showDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	if(isfirst!=0)return;
	int i,j,x,y;
	x=10;y=10;
	for(i=1;i<=C;i++){
		pDC->Rectangle(x-5,y-5,x+5,y+5);
		x+=9;
	}
	x=10;
	y+=9;
	for(i=1;i<=C;i++){
		pDC->Rectangle(x-5,y-5,x+5,y+5);
		x+=9;
	}
	for(i=1;i<=conterminal;i++){
		x=10+(inroad[i]-1)*9;
		for(j=y+9;j<=y+L*9;j+=9)
			pDC->Rectangle(x-5,j-5,x+5,j+5);
		x+=9;
		for(j=y+9;j<=y+L*9;j+=9)
			pDC->Rectangle(x-5,j-5,x+5,j+5);
		x=10+(outroad[i]-1)*9;
		for(j=y+9;j<=y+L*9;j+=9)
			pDC->Rectangle(x-5,j-5,x+5,j+5);
		x+=9;
		for(j=y+9;j<=y+L*9;j+=9)
			pDC->Rectangle(x-5,j-5,x+5,j+5);
		
		
	}
	

}

/////////////////////////////////////////////////////////////////////////////
// CRoundabout_showView printing

BOOL CRoundabout_showView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CRoundabout_showView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CRoundabout_showView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CRoundabout_showView diagnostics

#ifdef _DEBUG
void CRoundabout_showView::AssertValid() const
{
	CView::AssertValid();
}

void CRoundabout_showView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CRoundabout_showDoc* CRoundabout_showView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRoundabout_showDoc)));
	return (CRoundabout_showDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CRoundabout_showView message handlers

void CRoundabout_showView::OnBegin() 
{
	// TODO: Add your command handler code here
	
	isfirst=2;
	Invalidate();
	int i,j,index,rt;
	for(i=1;i<=total;i++)
		cel[i].iscar=false;
	last1=0,last2=0,last3=0;
	totalstay=0;
	num_of_create=0;
	num_of_leave=0;
	num_of_enter=0;
	if(redtime!=0){
		for(i=1;i<=conterminal;i++){
			if(rand()%100 > 50)
				signal[i]=rand()%redtime+1;
			else
				signal[i]=-(rand()%greentime+1);
		}
	}
	for(rt=1;rt<=runtime;rt++){
		//copy old values into temp
		for(index=1;index<=total;index++)
			oldcel[index]=cel[index];
		
		//update the lights
		for(i=1;i<=conterminal;i++){
			if(redtime==0)
				signal[i]=1;
			else if(signal[i]>0){
				signal[i]++;
				if(signal[i]>greentime)
					signal[i]=-1;
			}
			else{
				signal[i]--;
				if(signal[i]<-redtime)
					signal[i]=1;
			}
		
		}
		

		//create car
		for(j=1;j<=conterminal;j++)
			for(int temp=1+(j-1)*4*L;temp<=L+1+(j-1)*4*L;temp+=L)
				if(rand()%100>p1 && oldcel[temp].iscar==false){
					cel[temp].iscar=true;
					cel[temp].mtcar.index=carnum;
					cel[temp].mtcar.v=vinit;
					cel[temp].mtcar.status=1;
					cel[temp].mtcar.des=(j+rand()%(conterminal-1)+1)%conterminal;
					if(cel[temp].mtcar.des==0)cel[temp].mtcar.des=conterminal;
					carnum++;
					num_of_create++;
				}
		
		//update the incoming cars
		for(j=1;j<=conterminal;j++)
			for(int temp=1+(j-1)*4*L;temp<=L+1+(j-1)*4*L;temp+=L)
				for(int deal=temp;deal<=temp+L-2;deal++)
					if(oldcel[deal].iscar){
						int curgap=0;
						int p=deal+1;
						while(p<=temp+L-1){
							if(oldcel[p].iscar==false)
								curgap++;
							else
								break;
							p++;
						}
						if(curgap>=gap && oldcel[deal].mtcar.v<curgap&&oldcel[deal].mtcar.v<vmax)
							cel[deal].mtcar.v=min(oldcel[deal].mtcar.v+1,vmax);
						else
							cel[deal].mtcar.v=min(oldcel[deal].mtcar.v,curgap);
						if((rand()%100)>p2)
							cel[deal].mtcar.v=max(cel[deal].mtcar.v-1,0);
						cel[deal].iscar=false;
						cel[deal+cel[deal].mtcar.v].iscar=true;
						cel[deal+cel[deal].mtcar.v].mtcar=cel[deal].mtcar;
					}
		
		//update the car who whill enter the circle
		for(j=1;j<=conterminal;j++){
			int flag,deal=L+(j-1)*4*L;
			flag=0;
			if(oldcel[deal].iscar){
				bool isentry;
				int pt=rand()%100;
				if(pt<p4){
					isentry=f1(inroad[j]);
				}
				else if(pt<p5){
					isentry=f2(inroad[j]);
				}
				else if(pt<p6){
					isentry=f3(inroad[j]);
				}
				else {
					isentry=f4(inroad[j]);
				}
		//		isentry=f1(inroad[j]);
				if(redtime!=0)
					isentry=f1(inroad[j]);
				if(isentry && signal[j]>0)
				{
					num_of_enter++;
					cel[deal].iscar=false;
					cel[conterminal*4*L+inroad[j]+C].iscar=true;
					cel[conterminal*4*L+inroad[j]+C].mtcar=oldcel[deal].mtcar;
					cel[conterminal*4*L+inroad[j]+C].mtcar.status=2;
					cel[conterminal*4*L+inroad[j]+C].mtcar.v=1;
					cel[conterminal*4*L+inroad[j]+C].mtcar.entertime=rt;
					flag=1;
				}
				if(flag==0)
					cel[deal].mtcar.v=0;
			}
			deal+=L;
			flag=0;
			if(oldcel[deal].iscar){
				int pt=rand()%100;
				bool isentry;
					if(pt<p7){
					isentry=f_1(inroad[j]+1);
				}
				else
					isentry=f0(inroad[j]+1);
				if(redtime!=0)
					isentry=f_1(inroad[j]+1);
				if(isentry && signal[j]>0)
				{
					num_of_enter++;
					cel[deal].iscar=false;
					cel[conterminal*4*L+inroad[j]+1+C].iscar=true;
					cel[conterminal*4*L+inroad[j]+1+C].mtcar=oldcel[deal].mtcar;
					cel[conterminal*4*L+inroad[j]+1+C].mtcar.status=4;
					cel[conterminal*4*L+inroad[j]+1+C].mtcar.v=1;
					cel[conterminal*4*L+inroad[j]+1+C].mtcar.entertime=rt;
					flag=1;
				}
				if(flag==0)
					cel[deal].mtcar.v=0; 		
			}
			
		}

		//update the leaving cars

		for(j=1;j<=conterminal;j++)
			for(int temp=3*L+(j-1)*4*L;temp<=4*L+(j-1)*4*L;temp+=L)
				for(int deal=temp;deal>=temp-L+1;deal--)
					if(oldcel[deal].iscar){
						if(deal==temp-L+1){
							cel[deal].iscar=false;
						}
						else{
							CString tem;
							tem.Format("%d,%d,%d",j,temp,deal);
							int curgap=0;
							int p=deal-1;
							while(p>=temp-L+1){
								if(oldcel[p].iscar==false)
									curgap++;
								else
									break;
								p--;
							}
							if(curgap>=gap && oldcel[deal].mtcar.v<curgap&&oldcel[deal].mtcar.v<vmax)
								cel[deal].mtcar.v=min(oldcel[deal].mtcar.v+1,vmax);
							else
								cel[deal].mtcar.v=min(oldcel[deal].mtcar.v,curgap);
							if((rand()%100)>p2)
								cel[deal].mtcar.v=max(oldcel[deal].mtcar.v-1,0);
							cel[deal].iscar=false;
							if(deal-cel[deal].mtcar.v>=temp-L+1){
							cel[deal-cel[deal].mtcar.v].iscar=true;
							cel[deal-cel[deal].mtcar.v].mtcar=cel[deal].mtcar;
							}
						}
					}
	




		//update the cars int the circle
		for(j=conterminal*4*L+1;j<=conterminal*4*L+1+C;j+=C)
			for(int deal=j;deal<=j+C-1;deal++){
				if(oldcel[deal].iscar){
					if(oldcel[deal].mtcar.status==0 || oldcel[deal].mtcar.status==3 || oldcel[deal].mtcar.status==4){
						int tindex,cindex;
						tindex=cindex=deal-conterminal*4*L;
						int p,curgap=0;
						if(tindex<=C){
							tindex=(tindex+1)%C;
							if(tindex==0)
								tindex=C;
						}
						else{
							tindex-=C;
							tindex=(tindex+1)%C;
							if(tindex==0)
								tindex=C;
							tindex+=C;
						}
						p=tindex+conterminal*4*L;
						while(curgap<=C-1){
							if(oldcel[p].iscar==false)
								curgap++;
							else
								break;
							if(tindex<=C){
								tindex=(tindex+1)%C;
								if(tindex==0)
									tindex=C;
							}
							else{
								tindex-=C;
								tindex=(tindex+1)%C;
								if(tindex==0)
									tindex=C;
								tindex+=C;
							}
							p=tindex+conterminal*4*L;
						}
						car tcar;
						if(curgap>=gap && oldcel[deal].mtcar.v<curgap&&oldcel[deal].mtcar.v<cmax)
							tcar.v=min(oldcel[deal].mtcar.v+1,cmax);
						else
							tcar.v=min(oldcel[deal].mtcar.v,curgap);
						if((rand()%100)>p3)
							tcar.v=max(cel[deal].mtcar.v-1,0);
						tindex=cindex;
						if(tindex<=C){
								tindex=(tindex+tcar.v)%C;
								if(tindex==0)
									tindex=C;
							}
							else{
								tindex-=C;
								tindex=(tindex+tcar.v)%C;
								if(tindex==0)
									tindex=C;
								tindex+=C;
							}
						int des=tindex+conterminal*4*L,turn,flag=0;
						if(tcar.v!=0){
							if(cindex<=C){
								turn=conterminal*4*L+outroad[(oldcel[deal].mtcar.des)]+1;
								if((deal<=des && turn>=deal && turn<=des) || (deal>=des && turn>=deal && turn>=des) || (deal>=des && turn<=deal && turn<=des)){
									cel[deal].iscar=false;
									cel[turn].iscar=true;
									cel[turn].mtcar=oldcel[deal].mtcar;
									cel[turn].mtcar.v=1;
									cel[turn].mtcar.status=5;	
									flag=1;
								}
							}
							else{
								turn=conterminal*4*L+outroad[(oldcel[deal].mtcar.des)]+C;
								if((deal<=des && turn>=deal && turn<=des) || (deal>=des && turn>=deal && turn>=des) || (deal>=des && turn<=deal && turn<=des)){

⌨️ 快捷键说明

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