📄 elevator.cpp
字号:
#include <stdio.h> //头文件
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct //规定最大乘坐人数为 13
{
int elevator_stair;//电梯所在楼层
int elevator_man_munber;//电梯中的人数
int door_state;//门的状态0关1开(假设关了门时除了在第一层等人时之外,都在移动)
}elevator; //elevator structurer
typedef struct
{
int deadline_time;//一个人的容忍时间
int man_stair;//所在楼层
int aim_stair;//目的楼层
}man; //man structurer
typedef struct Node
{
char a[1000]; //信息
int clock_time; //时间
struct Node *next; //指针,指向下一个。
}SLNode; //linklist
void ListInitiate(SLNode* *head) //INIT_LIST 引用调用
{
if ((*head=(SLNode *)malloc(sizeof(SLNode)))==NULL)
exit(1);
(*head)->next=NULL;
}
int ListInsert(SLNode *head,int i,char a[1000],int t)
{
SLNode *p,*q;
int j;
p=head;
j=-1;
while (p->next!=NULL && j<i-1)
{
p=p->next;
j++;
}
if (j!=i-1)
{
printf("插入位置参数错!");
return 0;
}
if ((q=(SLNode *)malloc(sizeof(SLNode))) == NULL)
exit(1);
q->clock_time=t;
strcpy(q->a,a);
q->next=p->next;
p->next=q;
return 1;
}
void LinListSort(SLNode *head) //用时间排序
{
SLNode *curr,*pre,*p,*q;
p=head->next;
head->next=NULL;
while(p!=NULL)
{
curr=head->next;
pre=head;
while(curr!=NULL && curr->clock_time<=p->clock_time)
{
pre=curr;
curr=curr->next;
}
q=p;
p=p->next;
q->next=pre->next;
pre->next=q;
}
}
void Destroy(SLNode * *head)
{
SLNode *p,*p1;
p=*head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
*head = NULL;
}
///////////////////////////////////////////////////////////////////////////////
elevator dt;//定义一个电梯
man ren[1000];//定义人
clock_t shijian[10000];//事件
int g=0;
///////////////////////////////////////////////////////////////////////////////
/*
电梯动作: 1.开始开门,20t,开门结束;
2.开始关门,20t,关门结束;
3.从n-1层开始移动,30t,到达第n层;
4.开始等待,有人来或者300t,等待结束;
人的动作: 1.开始等待,x<=最大容忍时间,等待结束;
2.开始进门,25t,进门结束;
3.开始出门,25t,出门结束;
*/
void dt_1(SLNode *head,int t)//开门
{
char a[1000]={"开始开门!"},b[1000]={"开门结束!"};
ListInsert(head,g,a,t);
g++;
ListInsert(head,g,b,t+20);
g++;
}
void dt_2(SLNode *head,int t)//关门
{
char a[1000]={"开始关门!"},b[1000]={"关门结束!"};
ListInsert(head,g,a,t);
g++;
ListInsert(head,g,b,t+20);
g++;
}
void r_2(SLNode *head,int t)//关门
{
char a[1000]={"开始进门!"},b[1000]={"进门结束!"};
ListInsert(head,g,a,t);
g++;
ListInsert(head,g,b,t+25);
g++;
}
void r_3(SLNode *head,int t)//关门
{
char a[1000]={"开始出门!"},b[1000]={"出门结束!"};
ListInsert(head,g,a,t);
g++;
ListInsert(head,g,b,t+25);
g++;
}
///////////////////////////////////////////////////////////////////////////////
void run(SLNode *head,int n)//电梯运行函数
{
int k=0,i;
for(i=0;i<n;i++)
{
if(dt.door_state==0&&dt.elevator_stair==1)
{
dt_1(head,shijian[k]);
dt_1(head,shijian[k]+20);
}
}
}
void print(SLNode *head)
{
SLNode *p;
p=head;
while(p->next!=NULL)
{
printf("时间为: %d t 时,发生:",p->next->clock_time);
puts(p->next->a);
}
}
int main()
{
int t,a,k=0,i; //定义几个变量
SLNode *head; //定义头节点
clock_t ccc;
clock_t shuru[10000][2];//为了保证输入数据时计时暂停,定义一个shuru[]
//数组,用来存输入数据时候所用时间
ccc=clock();
t=(int)ccc/100;
printf("开始计时,每单位时间t为0.1秒,当前时刻为 %d t 。\n",t);
ListInitiate(&head);
while (1)
{
printf("正在计时中,如果有人要用电梯,请输入1,如果下班请输入0:\n");
scanf("%d",&a);
if (a==0)
break;
if (a==1)
{
printf("计时中断,请输入此人的所在楼层,目的楼层,最大容忍时间(单位:t):\n");
shuru[k][0]=clock();
scanf("%d,%d,%d",&ren[k].man_stair,&ren[k].aim_stair,&ren[k].deadline_time);
shuru[k][1]=clock();
k++;
}
}
for (i=0;i<k;i++)
{
shijian[k]=shuru[k][0]-ccc;
ccc=shuru[k][1]-shuru[k][0];
}
run(head,k);
LinListSort(head);
printf("是否打印人和电梯的动作?(1是 2否)\n");
scanf("%d",&a);
if (a==1)
print(head);
Destroy(&head);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -