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

📄 dianti.cpp

📁 设计一个电梯模拟系统,这是一个离散的模拟程序,因为电梯系统是乘客和电梯等”活动体”构成的集合,虽然它们彼此交互作用,但它们的行为是基本独立的.在离散的模拟中,以模拟时钟决定每一个活动体的动作发生的时刻
💻 CPP
字号:
#include<iostream.h>
#include <cmath>
#include <ctime>
//using namespace std;
//使用RAND_MAX
#include   <time.h>   
#include   <stdio.h>  
#include<windows.h>
int stack_size=100;
int Goingup=1;//电梯状态上
int Goingdown=-1;//电梯状态下
int Idle=0;//电梯状态等待
int nfloor=1;
int d1=0;
int d2=0;
int d3=0;
int State=Idle;
int call[5];
int callup[5];
int calldown[5];
int b;
int e;
int Asleep(int n)//休眠n*0.1秒
{
      while(n--)
	  {
		Sleep(100);//休眠0.1秒
		//每0.1秒执行一次		
	  }
	  return 0;
}

typedef struct Lnode{
	char   name;//想上电梯
	int    infloor;  //进入哪层楼
	int    outfloor;  //要去那层楼
    double giveuptime;  //能忍耐多长时间
	double Intertime;  //下一人出现的时间	
}Lnode,*person;  // 定义人的结构体

  //入电梯的队列链式存储结构
typedef struct Queue{
	Lnode man;
    struct Queue *next;
}Queue,*people; 
typedef struct{
	int count;
	people front;  //队头指针
	people rear;  //对尾指针
}linkqueue;
int Initqueue(linkqueue &Q)  //构造一个空队列
{
	Q.front=Q.rear=new Queue;
	Q.front->next=NULL;
	return 1;
}
int Enqueue(linkqueue &Q,Lnode &e)  //插入元素
{
	people p=new Queue;
	p->man=e;
	
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	Q.count++;
	return 1;
}
int Dequeue(linkqueue &Q,Lnode &e) //删除元素
{
	if(Q.front==Q.rear)
		return 0;
		people p=new Queue;
		p=Q.front->next;
        Lnode *k=&e;
		*k=p->man;
        Q.front->next=p->next;
		if(Q.front==p)Q.rear=Q.front;
		delete p;
        Q.count--;
		return 1;
}
//栈的顺序存储结构
typedef struct{
struct	Lnode *base;
struct	Lnode *top;
	int stacksize;
}Sqstack;
int initstack(Sqstack &s)
{ 
	s.base=new Lnode[stack_size];
	s.top=s.base;
	s.stacksize=stack_size;
	return 1;
}
int Push(Sqstack &s,Lnode &e)//进栈
{
	if(s.top-s.base>=s.stacksize){
		s.base =(Lnode *)realloc(s.base,(s.stacksize+stack_size)*sizeof(Lnode));
	s.top=s.base+s.stacksize;
	s.stacksize+=stack_size;
	}
	*s.top=e;
	s.top++;
	return 1;
}
int Pop(Sqstack &s,Lnode &e)//出栈
{
	--s.top;
	e=*s.top;	
	return 1;
}
void E1(Sqstack &s,linkqueue *Q);//电梯在一楼等待
void E3(Sqstack &s,linkqueue *Q);//电梯开门
void E5(Sqstack &s,linkqueue *Q);//电梯让人出入并关门
void E6(Sqstack &s,linkqueue *Q);//准备移动
void E7(Sqstack &s,linkqueue *Q);//上升一层
void E8(Sqstack &s,linkqueue *Q);//下降一层
void controler(Sqstack &s,linkqueue *Q);
void E4(Sqstack &s,linkqueue *Q)
{      
    Sqstack w;
    initstack(w);
	Lnode y;	
	if(s.base!=s.top)//判断电梯里是否有人
	{
		while(s.base!=s.top)
		{
		   Pop(s,y);       
		   if(y.outfloor!=nfloor)//在不在这层下?
		   {
			Push(w,y);
		   }
		   else 
		   {
			cout<<"&& 第 "<<nfloor<<" 层有 ["<<y.name<<"] "<<" 在第 "<<y.outfloor<<" 层下电梯"<<endl;
		   }
		}
	}

	while(w.base!=w.top)
		{
		Pop(w,y);
		Push(s,y);
		} 

    for(int i=Q[nfloor].count;i>0;i--)//下人(并判断是否超过忍耐时间)
	{    	
	     b=clock();   	    
		if((b-e)/1000<20)
		{
		Dequeue(Q[nfloor],y);
		Q[nfloor].count--; 
	  cout<<"## 电梯在第 "<<nfloor<<" 层时:有 ["<<y.name<<"] "<<"进电梯  并按到 "<<y.outfloor<<" 层按钮"<<endl;
        Push(s,y);
		}
		else
		{
            Dequeue(Q[nfloor],y);
		    Q[nfloor].count--;
			if(State==Goingup)			
			{
				callup[nfloor]=0;
			}
				else
				{
                 calldown[nfloor]=0;
				}
cout<<"!!["<<y.name<<"] "<<"在 "<<y.infloor<<" 层"<<" 放弃到"<<y.outfloor<<" 层"<<endl;
		}       
	}
	cout<<" "<<endl;
    if(nfloor==0)
	{
      State=Goingup;
	}
    if(nfloor==4)
	{
       State=Goingdown;
	}
  d1=0;
  int a=0;
	for (i=nfloor;i<5;i++)
	{
		if (callup[i]==1)
		{
                 a=1;
		}
		}

		if(a==1)
		{
        E5(s,Q);
		}
	else
		{
			State=Goingdown;
              E5(s,Q);
		}
}
void controler(Sqstack &s,linkqueue *Q)
{
	if(State!=Idle)
	{	
	}
	if(nfloor==1&&call[1]==1)
	{
		Asleep(10);
		State=Goingup;
		E3(s,Q);//开门
	}
	for(int i=0;i<=4;i++)
	{    
		if(call[i]==1)
		{    			
			if(i>nfloor)
			{
              nfloor=i;
			  
			  nfloor--;
			  State=Goingup;
			  E6(s,Q);
			  break;			  
			}
			else
			{
              nfloor=i;
			  nfloor++;
			  State=Goingdown;
			  E6(s,Q);
			  break;			  
			}
		}
	}	
}
void E1(Sqstack &s,linkqueue *Q)
{   
	if(Idle==0)
		controler(s,Q);	
}
void E3(Sqstack &s,linkqueue *Q)
{
	d1=d2=1;
	Asleep(10);   
	E4(s,Q);
}
void E5(Sqstack &s,linkqueue *Q)//关门
{
	
	while (d1!=0)
	{
		Asleep(10);//是否关门
	}
	Asleep(10);
    E6(s,Q);

}
void E6(Sqstack &s,linkqueue *Q)
{
	if(State==Goingup)
	{ 
		E7(s,Q);
	}
	else
	{	
		E8(s,Q);
    }
}
void E7(Sqstack &s,linkqueue *Q)
{ 
	nfloor++;
	Asleep(10);
	E3(s,Q);  
}
void E8(Sqstack &s,linkqueue *Q)
{ 
	nfloor--;
	Asleep(15);
	E3(s,Q);  
}
void main()
{
   e=clock()/1000;     
   cout<<"******************< 本程序是模拟电梯运行情况 >******************"<<endl;
   int n; 
   srand(time(NULL)); 
   n=rand()%22;//产生按电梯的随机人数
   if(n==0)
	 cout<<"没人按电梯电梯停止"<<endl;
   b=clock(); //产生当前时间 
   Sqstack s;
   initstack(s);//产生电梯栈
   linkqueue Q[5];
   for(int i=0;i<5;i++)//初始化五个进电梯的五个派队队列
	{
     Initqueue(Q[i]);
	 Q[i].count=0;
	}
    Lnode y;
    cout<<"输出按电梯的人的信息:"<<endl;
    for(i=0;i<n;i++)//初始化进电梯的人的信息
	{
		y.name=97+i;
		Asleep(3);
	y.infloor=rand()%5;
    Asleep(1);
	y.outfloor=rand()%5;	
    while(y.infloor==y.outfloor)
	{
		y.outfloor=rand()%5;
	}
    y.giveuptime=rand()%5;
	y.Intertime=e;
	cout<<"["<<y.name<<"] "<<"在 "<<y.infloor<<" 层按电梯铃"<<" 想去"<<y.outfloor<<"层 "<<" 忍耐时间"<<"50"<<endl;
	cout<<" "<<endl;
    Enqueue(Q[y.infloor],y);
    call[y.infloor]=1;
	if(y.infloor<y.outfloor)
	{
		callup[y.outfloor]=1;
	}
	else
	{
         calldown[y.outfloor]=1;
	}
	}
	E1(s,Q);
}

⌨️ 快捷键说明

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