1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| #include <iostream> #include "ElevatorLift.h" #include <time.h> #include "Passenger.h" #include "Group.h" using namespace std;
ElevatorLift * E[10]; Passenger * P[1000]; Group * G[5]; Time Nowtime; int K = 12; int N = 400; int M = 1; int L = 5; int S = 1; int T = 1; void GuideInput(); void Show(); void over(); void summary(); bool IsOver();
int main() { srand((int)time(0)); GuideInput(); SYSTEMTIME NowTime; GetLocalTime(&NowTime); Nowtime.set(NowTime.wHour, NowTime.wMinute, NowTime.wSecond); E[0] = new ElevatorLift(S, K, T, &Nowtime); E[1] = new ElevatorLift(S, K, T, &Nowtime); E[2] = new E1(S, K, T, &Nowtime); E[3] = new E1(S, K, T, &Nowtime); E[4] = new E2(S, K, T, &Nowtime); E[5] = new E2(S, K, T, &Nowtime); E[6] = new E3(S, K, T, &Nowtime); E[7] = new E3(S, K, T, &Nowtime); E[8] = new E4(S, K, T, &Nowtime); E[9] = new E4(S, K, T, &Nowtime); for (int i = 0; i < 5; i++) { G[i] = new Group(E[2*i], E[2*i + 1]); } for (int i = 0; i < N; i++) { P[i] = new Passenger(M, L, E, &Nowtime); }
VOID CALLBACK myTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime); MSG msg; SetTimer(NULL, 1, 2000, (TIMERPROC)myTimerProc); bool flag = true; while (flag) { if (IsOver()) { flag = false; } Show(); if (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
} KillTimer(NULL, 1); summary(); return 0;
}
VOID CALLBACK myTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime) { Nowtime = Nowtime + 1; for (int i = 0; i < 5; i++) { G[i]->action(); } for (int i = 0; i < N; i++) { P[i]->action(); } }
void GuideInput() { cout << "请输入乘客数量:" << endl; cin >> N; cout << "请输入电梯的最大载客数量:" << endl; cin >> K; cout << "请输入初始等待时间:" << endl; cin >> M; cout << "请输入乘客的乘梯次数:" << endl; cin >> L; cout << "请输入电梯的速度(单位:秒/层):" << endl; cin >> S; cout << "请输入每名乘客上下电梯的时间 :" << endl; cin >> T; }
void Show() { system("cls"); cout << "当前时间" << endl; printf("%d:%d:%d\n", Nowtime.hour, Nowtime.minute, Nowtime.second);
printf("乘客ID 当前状态 当前楼层 目标楼层 组号 电梯号 剩余乘梯次数 \n"); for (int i = 0; i < N; i++) { printf("%2d %s %2d %2d %2d %2d %2d\n" , i + 1, P[i]->GetStatus(), P[i]->GetNowFloor(), P[i]->GetNextFloor(), P[i]->Getgroup(), P[i]->Getwhich(), P[i]->Gettimes());
} printf("-------------------------------------------------------------------\n"); printf("电梯号 当前状态 当前楼层 目标楼层 当前人数:\n"); for (int i = 0; i < 10; i++) { printf(" %2d %4s %2d %2d %2d\n" , i + 1, E[i]->GetStatus(), E[i]->GetNowFloor(), E[i]->GetNextFloor(), E[i]->Getpeople());
} } void over() { system("cls");
for (int i = 0; i < 10; i++) { printf("%d号电梯:\n当前状态:%s 当前楼层:%d 目标楼层:%d 当前人数:%d\n", i + 1, E[i]->GetStatus(), E[i]->GetNowFloor(), E[i]->GetNextFloor(), E[i]->Getpeople()); printf("-------------------------------------------------------------------\n"); } for (int i = 0; i < N; i++) { printf("%d号乘客:\n当前状态: %s 当前楼层: %d 目标楼层:%d 次数:%d\n", i, P[i]->GetStatus(), P[i]->GetNowFloor(), P[i]->GetNextFloor(), P[i]->Gettimes()); printf("组号:%d ,电梯号 :%d\n", P[i]->Getgroup(), P[i]->Getwhich()); printf(" 下一次乘梯时间:%d:%d:%d\n ", P[i]->NextTime.hour, P[i]->NextTime.minute, P[i]->NextTime.second); printf("**********************************************************************\n"); } } void summary() { system("cls"); cout << "当前时间" << endl; printf("%d:%d:%d\n", Nowtime.hour, Nowtime.minute, Nowtime.second); printf("电梯号 忙碌时间(秒) 空闲时间(秒) \n"); for (int i = 0; i < 10; i++) {
printf("%2d %3d %3d\n", i + 1, E[i]->Getbusy(), E[i]->Getvacant());
} printf("**********************************************************************\n"); printf("乘客ID 等待时间(秒) \n"); for (int i = 0; i < N; i++) { printf("%2d %3d\n", i + 1, P[i]->GetTotal()); }
} bool IsOver() { for (int i = 0; i < N; i++) { if (P[i]->getstatus() !=3 ) { return false; } } return true; }
|