用C語言如何描述電梯的運行機制?
#include#include
using namespacestd;/*乘客的結構體*/
structpassenger
{charname;intstart;intend;intdir;
};/*鏈表結構*/typedefstructLNode
{undefined
passenger P;struct LNode *next;
}LNode,*LinkList;/*將節點LN插入到鏈表C中,插入到頭結點后面*/
void Insert(LNode *C, LNode *LN)
{undefined
LN->next = C->next;
C->next =LN;
}/*刪除LN后的第一個節點*/
void del(LNode *LN)
{undefined
LNode*next2 = LN->next;
LN->next = next2->next;free(next2);
}/*在鏈表C中查找到與LN節點相同的節點,并刪除*/
void finddel(LNode *LN, LinkList C)
{for (LNode *p = C; p->next != NULL; p = p->next)
{if (p->next->P.name == LN->P.name)
{undefined
del(p);break;
}
}
}/*判斷鏈表是否為空*/
boolisempty(LinkList L)
{if (L->next ==NULL)return 1;else
return 0;
}/*當電梯運行方向為dir時,
將C中要進入電梯的節點全部存放至A中,按照進入電梯的順序存放
并將其相應的存放在B中,按照離開電梯的順序存放*/
void Insert2(LinkList A, LinkList B, LinkList C, int dir, intfloor)
{for (LNode *p = C->next; p != NULL; p = p->next)
{if (dir == 1 && p->P.start >= floor && p->P.dir == 1)
{//查找出C中方向相同且起始樓層在電梯上方的所有節點
LNode *r = (LNode*)malloc(sizeof(LNode));
LNode*s = (LNode*)malloc(sizeof(LNode));
r->P = p->P;
s->P = p->P;for (LNode *q = A; ; q = q->next)
{//將該節點插入A中,確保其起始樓層為升序的方式
if (q->next == NULL || q->next->P.start >= r->P.start)
{undefined
Insert(q, r);break;
}
}for (LNode *q = B; ; q = q->next)
{//將該節點插入B中,確保其目標樓層為升序的方式
if (q->next == NULL || q->next->P.end >= s->P.end)
{undefined
Insert(q, s);break;
}
}
}else if (dir == 0 && p->P.start <= floor && p->P.dir == 0)
{undefined
LNode*r = (LNode*)malloc(sizeof(LNode));
LNode*s = (LNode*)malloc(sizeof(LNode));
r->P = p->P;
s->P = p->P;for (LNode *q = A; ; q = q->next)
{if (q->next == NULL || q->next->P.start <= r->P.start)
{undefined
Insert(q, r);break;
}
}for (LNode *q = B; ; q = q->next)
{if (q->next == NULL || q->next->P.end <= s->P.end)
{undefined
Insert(q, s);break;
}
}
}
}
}int finds(int m, LinkList C, intdir)
{//確定出電梯行駛方向最后停留的樓層
if (dir == 1)
{for (LNode *p = C->next; p != NULL; p = p->next)
{if (mP.start)
m= p->P.start;
}
}else{for (LNode *p = C->next; p != NULL; p = p->next)
{if (m>p->P.start)
m= p->P.start;
}
}returnm;
}intmain()
{undefined
LinkList C= (LNode*)malloc(sizeof(LNode));
C->next = NULL;//C為存儲所有的乘客信息的鏈表
LinkList A = (LNode*)malloc(sizeof(LNode));
A->next = NULL;//A為存儲某個行駛方向上,上電梯的所有乘客信息
LinkList B = (LNode*)malloc(sizeof(LNode));
B->next = NULL;//B為A中乘客的按下電梯順序排列
int floor;//記錄電梯選擇運行方向時所在的樓層
int dir;//記錄電梯運行的方向
cout << "請輸入電梯所在樓層:";while (cin >>floor)
{/*輸入乘客信息并創建鏈表,電梯運行的初始方向根據只要上方有乘客則向上*/cout<< "請依次輸入乘客代號、起始樓層、目標樓層、行駛方向:" <
dir= 0;while (cin >>a)
{undefined
LinkList x= (LNode*)malloc(sizeof(LNode));
x->P.name =a;
cin>> x->P.start >> x->P.end >> x->P.dir;
Insert(C, x);if (x->P.start >=floor)
dir= 1;
}/*while(cin>>str)語句在結束輸入時使用了Ctrl+Z,告訴cin用戶已經結束了輸入,
為了讓程序正常運行,調用cin.clear()讓cin的所有條件狀態位復位*/cin.clear();/*模擬電梯行駛過程*/
while (!isempty(C))
{//有乘客未乘坐電梯
Insert2(A, B, C, dir, floor);//將C中滿足條件的乘客放入A、B中
/*乘客上下*/
if(isempty(B))
floor=finds(floor, C, dir);while (!isempty(B))
{//模擬電梯往某個特定方向行駛的過程
if (dir == 1)
{//當電梯是向上行駛時
if ((A->next == NULL) || (A->next->P.start > B->next->P.end))
{//出電梯的情況
cout << "當前樓層為" << B->next->P.end << ","
<< B->next->P.name << "出電梯" <
finddel(B->next, C);//刪除C中對應的乘客信息
del(B);//刪除鏈表中該乘客的信息
}else{//進電梯的情況
cout << "當前樓層為" << A->next->P.start << ","
<< A->next->P.name << "進電梯" <
del(A);//刪除鏈表中該乘客的信息
}
}else{//當電梯是向下行駛時
if ((A->next == NULL) || (A->next->P.start < B->next->P.end))
{undefined
cout<< "當前樓層為" << B->next->P.end << ","
<< B->next->P.name << "出電梯" <
finddel(B->next, C);
del(B);
}else{undefined
cout<< "當前樓層為" << A->next->P.start << ","
<< A->next->P.name << "進電梯" <
del(A);
}
}if (B->next != NULL && B->next->next ==NULL)
{undefined
floor= finds(B->next->P.end, C, dir);
}
}
dir= !dir;//改變行駛方向
}/**/cout<< endl <
cout<< "請輸入電梯所在樓層:";
}
system("pause");
}