?? 電梯管理 程序.cpp
字號:
//電梯管理程序
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#define max_persons 15
bool operation_state=1; //控制電梯是否運行
struct floors //樓層狀態
{bool up; //樓層的“上”按鈕狀態
bool down; //樓層的“下”按鈕狀態
int up_persons; //在該樓層等待上樓的人數
int down_persons; //在該樓層等待下樓的人數
}floor[12];
struct lift_state //電梯狀態
{int stay_in_floor; //電梯所在的樓層
bool up; //電梯的運動狀態
bool down; //電梯的運動狀態
bool stay; //電梯的運動狀態
int all_persons; //電梯內的總人數
int out_persons[12]; //電梯內的人準備在各層出去的人數
bool inbutton_state[12]; //電梯內的按鈕狀態
}lift1={0,0,0,1,0};
lift_state lift2={0,0,0,1,0};
DWORD id1,id2,id3;
HANDLE hbutton,hprint,hprint2;
DWORD WINAPI print(PVOID);
DWORD WINAPI button(PVOID);
DWORD WINAPI print2(PVOID);
void main(void) //主函數
{int i;
//初始化電梯狀態
for (i=0;i<12;i++)
{lift1.inbutton_state[i]=0;
lift1.out_persons[i]=0;
lift2.inbutton_state[i]=0;
lift2.out_persons[i]=0;
}
//初始化樓層狀態
for (i=0;i<12;i++)
{floor[i].up=0;
floor[i].down=0;
floor[i].up_persons=0;
floor[i].down_persons=0;
}
hprint2=CreateThread(NULL,0,print2,0,0,&id3); //創建管理輸出的線程
hprint=CreateThread(NULL,0,print,0,0,&id2); //創建管理輸出的線程
hbutton=CreateThread(NULL,0,button,0,0,&id1); //創建管理各種按鈕的線程
while (operation_state)
;
}
//實現管理各種按鈕的函數
DWORD WINAPI button(PVOID PARAM)
{char ch;
while (operation_state)
{
ch=getchar();
SuspendThread(hprint); //掛起print線程
SuspendThread(hprint2); //掛起print2線程
switch (ch)
{ case '`': floor[0].up=1;cout<<"請輸入在地下2層等待上樓的人數:"<<endl;cin>>floor[0].up_persons;break;
case '1': floor[1].up=1;cout<<"請輸入在地下1層等待上樓的人數:"<<endl;cin>>floor[1].up_persons;break;
case '2':floor[1].down=1;cout<<"請輸入在地下1層等待下樓的人數:"<<endl;cin>>floor[1].down_persons;break;
case '3':floor[2].up=1;cout<<"請輸入在第0層等待上樓的人數:"<<endl;cin>>floor[2].up_persons;break;
case '4':floor[2].down=1;cout<<"請輸入在第0層等待下樓的人數:"<<endl;cin>>floor[2].down_persons;break;
case '5':floor[3].up=1;cout<<"請輸入在第1層等待上樓的人數:"<<endl;cin>>floor[3].up_persons;break;
case '6':floor[3].down=1;cout<<"請輸入在第1層等待下樓的人數:"<<endl;cin>>floor[3].down_persons;break;
case '7':floor[4].up=1;cout<<"請輸入在第2層等待上樓的人數:"<<endl;cin>>floor[4].up_persons;break;
case '8':floor[4].down=1;cout<<"請輸入在第2層等待下樓梯的人數:"<<endl;cin>>floor[4].down_persons;break;
case '9':floor[5].up=1;cout<<"請輸入在第3層等待上樓的人數:"<<endl;cin>>floor[5].up_persons;break;
case '0':floor[5].down=1;cout<<"請輸入在第3層等待下樓的人數:"<<endl;cin>>floor[5].down_persons;break;
case '-':floor[6].up=1;cout<<"請輸入在第4層等待上樓的人數:"<<endl;cin>>floor[6].up_persons;break;
case '=':floor[6].down=1;cout<<"請輸入在第4層等待下樓的人數:"<<endl;cin>>floor[6].down_persons;break;
case '\\':floor[7].up=1;cout<<"請輸入在第5層等待上樓的人數:"<<endl;cin>>floor[7].up_persons;break;
case 'q':floor[7].down=1;cout<<"請輸入在第5層等待下樓的人數:"<<endl;cin>>floor[7].down_persons;break;
case 'w':floor[8].up=1;cout<<"請輸入在第6層等待上樓的人數:"<<endl;cin>>floor[8].up_persons;break;
case 'e':floor[8].down=1;cout<<"請輸入在第6層等待下樓的人數:"<<endl;cin>>floor[8].down_persons;break;
case 'r':floor[9].up=1;cout<<"請輸入在第7層等待上樓的人數:"<<endl;cin>>floor[9].up_persons;break;
case 't':floor[9].down=1;cout<<"請輸入在第7層等待下樓的人數:"<<endl;cin>>floor[9].down_persons;break;
case 'y':floor[10].up=1;cout<<"請輸入在第8層等待上樓的人數:"<<endl;cin>>floor[10].up_persons;break;
case 'u':floor[10].down=1;cout<<"請輸入在第8層等待下樓的人數:"<<endl;cin>>floor[10].down_persons;break;
case 'i':floor[11].down=1;cout<<"請輸入在第9層等待下樓的人數:"<<endl;cin>>floor[11].down_persons;break;
case 'a':lift1.inbutton_state[0]=1;cout<<"請輸入電梯內1的人準備在地下2層出去的人數:"<<endl;cin>>lift1.out_persons[0];break;
case 's':lift1.inbutton_state[1]=1;cout<<"請輸入電梯內1的人準備在地下1層出去的人數:"<<endl;cin>>lift1.out_persons[1];break;
case 'd':lift1.inbutton_state[2]=1;cout<<"請輸入電梯內1的人準備在第0層出去的人數:"<<endl;cin>>lift1.out_persons[2];break;
case 'f':lift1.inbutton_state[3]=1;cout<<"請輸入電梯內1的人準備在第1層出去的人數:"<<endl;cin>>lift1.out_persons[3];break;
case 'g':lift1.inbutton_state[4]=1;cout<<"請輸入電梯內1的人準備在第2層出去的人數:"<<endl;cin>>lift1.out_persons[4];break;
case 'h':lift1.inbutton_state[5]=1;cout<<"請輸入電梯內1的人準備在第3層出去的人數:"<<endl;cin>>lift1.out_persons[5];break;
case 'j':lift1.inbutton_state[6]=1;cout<<"請輸入電梯內1的人準備在第4層出去的人數:"<<endl;cin>>lift1.out_persons[6];break;
case 'k':lift1.inbutton_state[7]=1;cout<<"請輸入電梯內1的人準備在第5層出去的人數:"<<endl;cin>>lift1.out_persons[7];break;
case 'l':lift1.inbutton_state[8]=1;cout<<"請輸入電梯內1的人準備在第6層出去的人數:"<<endl;cin>>lift1.out_persons[8];break;
case ';':lift1.inbutton_state[9]=1;cout<<"請輸入電梯內1的人準備在第7層出去的人數:"<<endl;cin>>lift1.out_persons[9];break;
case '\'':lift1.inbutton_state[10]=1;cout<<"請輸入電梯內1的人準備在第8層出去的人數:"<<endl;cin>>lift1.out_persons[10];break;
case 'z':lift1.inbutton_state[11]=1;cout<<"請輸入電梯內1的人準備在第9層出去的人數:"<<endl;cin>>lift1.out_persons[11];break;
case 'A':lift2.inbutton_state[0]=1;cout<<"請輸入電梯內2的人準備在地下2層出去的人數:"<<endl;cin>>lift2.out_persons[0];break;
case 'S':lift2.inbutton_state[1]=1;cout<<"請輸入電梯內2的人準備在地下1層出去的人數:"<<endl;cin>>lift2.out_persons[1];break;
case 'D':lift2.inbutton_state[2]=1;cout<<"請輸入電梯內2的人準備在第0層出去的人數:"<<endl;cin>>lift2.out_persons[2];break;
case 'F':lift2.inbutton_state[3]=1;cout<<"請輸入電梯內2的人準備在第1層出去的人數:"<<endl;cin>>lift2.out_persons[3];break;
case 'G':lift2.inbutton_state[4]=1;cout<<"請輸入電梯內2的人準備在第2層出去的人數:"<<endl;cin>>lift2.out_persons[4];break;
case 'H':lift2.inbutton_state[5]=1;cout<<"請輸入電梯內2的人準備在第3層出去的人數:"<<endl;cin>>lift2.out_persons[5];break;
case 'J':lift2.inbutton_state[6]=1;cout<<"請輸入電梯內2的人準備在第4層出去的人數:"<<endl;cin>>lift2.out_persons[6];break;
case 'K':lift2.inbutton_state[7]=1;cout<<"請輸入電梯內2的人準備在第5層出去的人數:"<<endl;cin>>lift2.out_persons[7];break;
case 'L':lift2.inbutton_state[8]=1;cout<<"請輸入電梯內2的人準備在第6層出去的人數:"<<endl;cin>>lift2.out_persons[8];break;
case ':':lift2.inbutton_state[9]=1;cout<<"請輸入電梯內2的人準備在第7層出去的人數:"<<endl;cin>>lift2.out_persons[9];break;
case '"':lift2.inbutton_state[10]=1;cout<<"請輸入電梯內2的人準備在第8層出去的人數:"<<endl;cin>>lift2.out_persons[10];break;
case 'Z':lift2.inbutton_state[11]=1;cout<<"請輸入電梯內2的人準備在第9層出去的人數:"<<endl;cin>>lift2.out_persons[11];break;
case 'x':operation_state=0;break;
}
ResumeThread(hprint); //恢復print線程
ResumeThread(hprint2); //恢復print2線程
}
return 0;
}
//用于輸出電梯1的線程
DWORD WINAPI print(PVOID PARAM2)
{int t_inperson=0,t_outperson=0; //用于臨時存放電梯狀態
bool flag1,flag2,flag3,flag4,flag5,flag6,flag7,flag8,flag9,flag10,flag11,flag12,flag13,flag14,flag15,flag16,flag17,flag18,flag_up,flag_stay,flag_down;
bool in_high=0,in_low=0,out_high=0,between=0,high_down=0,between_near=0,between_up=0,high=0,low=0,between_down=0,low_up=0,out_low=0;
int i,t_high_floor,t_low_floor;
while (operation_state) //用于輸出電梯狀態的死循環
{
Sleep(2500); //先讓print線程睡眠2.5秒,這樣在這段時間里電梯能響應電梯內、外的按鈕
in_high=0;
out_high=0;
for (i=lift1.stay_in_floor+3;i<=11;i++)
{
in_high=in_high || lift1.inbutton_state[i];
out_high=out_high || floor[i].up || floor[i].down;
}
in_low=0;
for (i=lift1.stay_in_floor+1;i>=0;i--)
in_low=in_low || lift1.inbutton_state[i];
t_high_floor=(lift1.stay_in_floor>lift2.stay_in_floor)? lift1.stay_in_floor:lift2.stay_in_floor;
t_low_floor=(lift1.stay_in_floor<lift2.stay_in_floor)? lift1.stay_in_floor:lift2.stay_in_floor;
between=0;
for (i=t_low_floor+3;i<t_high_floor+2;i++)
between=between || floor[i].up || floor[i].down;
high_down=0;
for (i=lift2.stay_in_floor+3;i<=11;i++)
high_down=high_down || floor[i].down;
between_near=0;
for (i=t_low_floor+3;i<=t_low_floor+(t_high_floor-t_low_floor)/2+2;i++)
between_near=between_near || floor[i].up || floor[i].down;
between_up=0;
for (i=t_low_floor+3;i<t_high_floor+2;i++)
between_up=between_up || floor[i].up;
high=0;
for (i=lift2.stay_in_floor+3;i<=11;i++)
high=high || floor[i].up || floor[i].down;
between_down=0;
for (i=t_low_floor+3;i<=t_high_floor+2;i++)
between_down=between_down || floor[i].down;
low_up=0;
for (i=lift2.stay_in_floor+1;i>=0;i--)
low_up=low_up || floor[i].up;
out_low=0;
for (i=0;i<lift1.stay_in_floor+2;i++)
out_low=out_low || floor[i].up || floor[i].down;
low=0;
for (i=lift2.stay_in_floor+1;i>=0;i--)
low=low || floor[i].up || floor[i].down;
if (lift1.up)
{
if (lift1.stay_in_floor<9)
lift1.stay_in_floor++; //更新電梯所在的樓層
floor[lift1.stay_in_floor+2].up=0; //更新電梯外的“上”按鈕狀態
lift1.inbutton_state[lift1.stay_in_floor+2]=0;//更新電梯內該層按鈕狀態
flag1=in_high;
flag2=!flag1&(lift1.stay_in_floor>lift2.stay_in_floor)&out_high;
flag3=!flag1&(lift1.stay_in_floor==lift2.stay_in_floor)&out_high&(lift2.down || lift2.stay);
flag4=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.up≬
flag5=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.up&high_down;
flag6=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.stay&between_near;
flag7=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.down&between_up;
flag8=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.down&high;
flag_up=flag1 || flag2 ||flag3 ||flag4 ||flag5 ||flag6 ||flag7 ||flag8;
if (flag_up)
{
lift1.all_persons=lift1.all_persons-lift1.out_persons[lift1.stay_in_floor+2]+floor[lift1.stay_in_floor+2].up_persons;//更新電梯1內的總人數
t_inperson=floor[lift1.stay_in_floor+2].up_persons;
floor[lift1.stay_in_floor+2].up_persons=0; //更新在該層樓等待進入電梯的人數
t_outperson=lift1.out_persons[lift1.stay_in_floor+2];
lift1.out_persons[lift1.stay_in_floor+2]=0; //更新在該層樓出去電梯的人數
}
else if (!flag_up)
{
flag9=!flag1&(lift1.stay_in_floor>lift2.stay_in_floor)&lift2.up&!out_high&!between_down;
flag10=!flag1&(lift1.stay_in_floor>lift2.stay_in_floor)&lift2.stay&!out_high&!between_near;
flag11=!flag1&(lift1.stay_in_floor>lift2.stay_in_floor)&lift2.down&!out_high&!between&!low_up;
flag12=!flag1&(lift1.stay_in_floor==lift2.stay_in_floor)&lift2.up&!out_low;
flag13=!flag1&(lift1.stay_in_floor==lift2.stay_in_floor)&lift2.stay&!out_low&!out_high;
flag14=!flag1&(lift1.stay_in_floor==lift2.stay_in_floor)&lift2.down&!out_high;
flag15=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.up&!out_low&!between&!high_down;
flag16=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.stay&!out_low&!between_near&!high_down;
flag17=!flag1&(lift1.stay_in_floor<lift2.stay_in_floor)&lift2.down&!out_low&!between_up&!high;
flag_stay=flag9 || flag10 || flag11 || flag12 || flag13 || flag14 || flag15 || flag16 || flag17;
if (flag_stay)
{
lift1.all_persons=lift1.all_persons-lift1.out_persons[lift1.stay_in_floor+2];//更新電梯1內的總人數
t_outperson=lift1.out_persons[lift1.stay_in_floor+2];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -