📄 dianti.txt
字号:
问题描述:
模拟某校九层教学楼的电梯系统。该楼有一个自动电梯,能在每层停留。九个楼层由下至上依次称为地下一层、第一层、第二层、……第八层,其中第一层是大楼的进出层,即是电梯的“本垒层”,电梯“空闲”时,将来到该层候命。
乘客可随时地进出于任何层。对于每个人来说,他有一个能容忍的最长等待时间,一旦等候电梯时间过长,他将放弃。
模拟时钟从0开始,时间单位为0.1秒。人和电梯的各种动作均要消耗一定的时间单位(简记为t),比如:有人进出时,电梯每隔40t检测一次,若无人进出,则关门;关门和开门各需要20t;每个人进出电梯均需要25t;如果电梯在某层精致时间超过300t,则驶回1层候命。
基本要求:
按时序显示系统状态的变化过程,即发生的全部人和电梯的动作序列。
需求分析:
(1)用系统计时,每当有新的动作要输入时要暂停计时,等输入完成后即可继续计时。
(2)输入各个动作,最后,输入完毕后,可以保存所有动作序列,并有序输出。
设计:
设计思想
(1)存储结构
根据问题描述,可采用单链表结构。
结点描述:
a[1000]
mytime
*next
事件内容
时间发生的时间
下一个事件指针
定义如下:
typedef struct Node
{
char a[1000];//信息
int mytime;//时间
struct Node *next;
}SLNode;
(2)基本思想
a.用 time.h 里面的clock()来计时,当有输入时,记录下开始输入的时间和结束输入的时间,之后就可以算出动作的时间了。
b.将各个动作发生的时间进行处理,算出每个事件的发生顺序和时间。
c.将各个数据插入链表,即可实现题目要求。
设计表示法
(1)过程或函数调用的关系图
main
yunxing
print
ListInsert
LinListSort
各个动作函数(n个)
ge)
ListInitiate
Destroy
(2)基于数据结构的操作组
主要在于yunxing()函数,它将各个时间动作处理成时间事件,并将事件插入到链表中。
(3)过程与接口规格说明
无
实现注释
未实现。
详细设计
无
调试分析
用户手册
测试结果
没有成功
源程序清单
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct //规定最大乘坐人数为13
{
int lc;//电梯所在楼层
int rs;//电梯中的人数
int men;//门的状态 0关1开(假设关了门时除了在第一层等人时之外,都在移动)
}dianti;
typedef struct
{
int rrsj;//一个人的容忍时间
int sz;//所在楼层
int md;//目的楼层
}man;
typedef struct Node
{
char a[1000];//信息
int mytime;//时间
struct Node *next;
}SLNode;
void ListInitiate(SLNode * * head)
{
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->mytime = 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->mytime<=p->mytime)
{
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;
}
///////////////////////////////////////////////////////////////////////////////
dianti 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 yunxing(SLNode *head,int n)//电梯运行函数
{
int k=0,i;
for(i=0;i<n;i++)
{
if(dt.men==0&&dt.lc==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->mytime);
puts(p->next->a);
}
}
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].sz,&ren[k].md,&ren[k].rrsj);
shuru[k][1]=clock();
k++;
}
}
for(i=0;i<k;i++)
{
shijian[k]=shuru[k][0]-ccc;
ccc=shuru[k][1]-shuru[k][0];
}
yunxing(head,k);
LinListSort(head);
printf("是否打印人和电梯的动作?(1是 2否)\n");
scanf("%d",&a);
if(a==1)
print(head);
Destroy(&head);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -