-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-program.cpp
398 lines (380 loc) · 13.6 KB
/
main-program.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "Time.h"
#include "Queue.h"
#include "Stack.h"
#include "DataStructuresStudent.h"
#include "teachingAssistant.h"
//have to do this cuz mobile dont support project mode
/*
#include "Stack.cpp"
#include "Time.cpp"
#include "DataStructuresStudent.cpp"
#include "teachingAssistant.cpp"
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
//push elements onto stack from file
Stack readLaptop(ifstream & f);
//sort TAs alphabetically
void sort(teachingAssistant * , int);
//msg writing functions
void writeProg(ofstream & , int & );
void writeDay(ofstream & , int & );
void TAstarted(ofstream & , teachingAssistant & , Time & , int & );
void TAended(ofstream & , teachingAssistant & , Time & , Time, int & );
void Review(ofstream & , Time, int, int, int);
void getHelp(ofstream & , teachingAssistant & , Time & );
//tells when LAB starts and ENDs according to TA timing
Time closing(teachingAssistant * , int, int);
Time starting(teachingAssistant * , int, int);
int main() {
//read Laptop serial nums and add them in Laptop stack
ifstream in ("input.txt");
ofstream out("output.txt", ios::trunc);
Stack Laptop;
Laptop = readLaptop(in);
//read TAs and create a dynamic array
int t;
in >> t;
teachingAssistant * TAs = new teachingAssistant[t];
for (int i = 0; i < t; i++) {
in >> TAs[i];
}
sort(TAs, t);
//read number of programs
int programs;
in >> programs;
for (int i = 1; i <= programs; i++) {
writeProg(out, i);
for (int day = 1; day <= 3; day++) {
writeDay(out, day);
Time waqt(12, 00);
//num of std happy in a day
int happy = 0;
int track = 0, track2 = 0;
int TAtoLDM = 0;
//dont take students in ta after closing time
bool noMore = false;
//read students and add them in Door queue
int numOfStds;
in >> numOfStds;
Queue Door;
Queue LDM;
Queue TA;
bool stds = true;
if (numOfStds > 300) {
cout << "Too Many!/n";
stds = false;
} else {
for (int k = 0; k < numOfStds; k++) {
DataStructuresStudent * temp = new DataStructuresStudent;
in >> * temp;
if (stds) Door.enqueue(temp);
}
}
//check if lab is open
//2 conditions. either no TA available,
Time close = closing(TAs, t, day);
Time start = starting(TAs, t, day);
//or time is not 12
//if lab open, start waqt looo
if (waqt <= close) {
//if open, enque in LDM queue,time same only change.
for (; waqt <= Time(20, 00) && waqt <= close; ++waqt) {
for (int x = 0; x < t; x++) {
TAstarted(out, TAs[x], waqt, day);
}
if (!noMore) {
while (!Door.isEmpty() && waqt == Door.peek() -> getTime()) {
out << waqt << ": " << Door.peek() -> getName() << " has arrived in Arsalan\n";
LDM.enqueue(Door.dequeue());
}
}
//if student satisfy
//push laptop, dequeue them out
//if student unsatisfy
//pop laptop, deque them to TA queue
if (noMore) {
DataStructuresStudent * temp;
while (!TA.isEmpty()) {
LDM.enqueue(TA.dequeue());
}
if (!LDM.isEmpty()) {
if (LDM.peek() -> getSerialNum() == 0) {
temp = LDM.dequeue();
out << waqt << ": " << temp -> getName() << " never even got a laptop and went home FRUSTRATED.\n";
}
temp = LDM.dequeue();
Laptop.push(temp -> getSerialNum());
temp -> setSerialNum(0);
if (temp -> perc() >= 0.75) {
out << waqt << ": " << temp -> getName() << " has returned laptop " << Laptop.Top() << " and went home HAPPY.\n";
happy++;
} else {
out << waqt << ": " << temp -> getName() << " has returned laptop " << Laptop.Top() << " and went home FRUSTRATED.\n";
}
} //ldm empty check
} //if ta not taking anymore students
else if (!LDM.isEmpty()) {
DataStructuresStudent * temp;
if (LDM.peek() -> isSatisfy()) {
if (track2 == 1) {
temp = LDM.dequeue();
Laptop.push(temp -> getSerialNum());
temp -> setSerialNum(0);
if (temp -> isSatisfy()) {
out << waqt << ": " << temp -> getName() << " has returned laptop " << Laptop.Top() << " and went home HAPPY.\n";
happy++;
}
if (TAtoLDM >= 1) {
track2 = 1;
TAtoLDM--;
} else track2 = 0;
if (!LDM.isEmpty()) {
track = 1;
} else track = 0;
} else track2++;
} else {
if (!Laptop.isEmpty()) {
if (track == 2) {
if (LDM.count() > 1) track = 2;
else
track = 0;
temp = LDM.dequeue();
temp -> setSerialNum(Laptop.pop());
out << waqt << ": " << temp -> getName() << " has borrowed Laptop " <<
temp -> getSerialNum() << " and moved to the TA Line\n";
TA.enqueue(temp);
} else track++;
} //check for empty Laptop
}
} //check for empty LDM
//handling students with TA
for (int m = 0; m < t; m++) {
//if ta available at that time and day
if (TAs[m].isAvailable(day, waqt)) {
//give student if he is free
if (TAs[m].getStudent() == NULL) {
if (!TA.isEmpty()) {
TAs[m].setStudent(TA.dequeue());
ta1:
getHelp(out, TAs[m], waqt);
TAs[m] = 0;
}
}
//take student from him minute>5
else if (TAs[m].getMinute() == 4) {
TAs[m].getStudent() -> inc();
TAs[m] = 0;
//std happy? send to ldm
if (TAs[m].getStudent() -> isSatisfy()) {
out << waqt << ": " << TAs[m].getStudent() -> getName() << " has no more questions and will now return the laptop\n";
LDM.enqueue(TAs[m].getStudent());
if (LDM.count() > 1) {
track2 = 1;
TAtoLDM++;
}
TAs[m].setStudent(NULL);
//dont waste time, ta handle student
if (!TA.isEmpty()) {
TAs[m].setStudent(TA.dequeue());
getHelp(out, TAs[m], waqt);
}
} //is satisfy end
else {
//std not happy? send to TA queu
out << waqt << ": " << TAs[m].getStudent() -> getName() << " has had one question answered and gotten back in line.\n";
if (TA.isEmpty()) {
goto ta1;
} else {
//dont waste time, ta handle student
TA.enqueue(TAs[m].getStudent());
TAs[m].setStudent(TA.dequeue());
getHelp(out, TAs[m], waqt);
TAs[m] = 0;
}
} //not satisfy end
} else {
++TAs[m];
}
} //Ta available or nah
else { //TA leaving,take student from him
// if closing time equal to leaving time dont take anymore studnt in lab. bool noMore is used here
// display TA leaving
TAended(out, TAs[m], waqt, closing(TAs, t, day), day);
if (waqt == close) {
noMore = true;
if (TAs[m].getStudent() != NULL) {
track = 0;
if (TAs[m].getMinute() == 4) {
TAs[m].getStudent() -> inc();
if (TAs[m].getStudent() -> isSatisfy()) {
out << waqt << ": " << TAs[m].getStudent() -> getName() << " has no more questions and will now return the laptop\n";
LDM.enqueue(TAs[m].getStudent());
TAs[m].setStudent(NULL);
} else {
out << waqt << ": " << TAs[m].getStudent() -> getName() << " has had one question answered but must return the laptop now and exit the lab.\n";
TA.enqueue(TAs[m].getStudent());
TAs[m].setStudent(NULL);
}
TAs[m] = 0;
break;
} else {
++TAs[m];
++close;
}
} //if he has student
} //if leave time same as closing time
else {
//if ta leave before closing time
if (TAs[m].getStudent() != NULL) {
if (TAs[m].getMinute() == 4) {
TAs[m].getStudent() -> inc();
if (TAs[m].getStudent() -> isSatisfy()) {
out << waqt << ": " << TAs[m].getStudent() -> getName() << " has no more questions and will now return the laptop\n";
LDM.enqueue(TAs[m].getStudent());
TAs[m].setStudent(NULL);
} else {
out << waqt << ": " << TAs[m].getStudent() -> getName() << " has had one question answered and gotten back in line.\n";
TA.enqueue(TAs[m].getStudent());
TAs[m].setStudent(NULL);
}
TAs[m] = 0;
} else {
++TAs[m];
}
} //if he got student
} //if ta leave before closing time
} //ta leaving ends
} //TAs loop ends
} //waqt ends
//empty the lab
while (!TA.isEmpty())
LDM.enqueue(TA.dequeue());
bool flag = true;
if (LDM.count() == 1) {
++waqt;
flag = false;
}
while (!LDM.isEmpty()) {
DataStructuresStudent * temp = LDM.dequeue();
if (temp -> getSerialNum() != 0) {
Laptop.push(temp -> getSerialNum());
temp -> setSerialNum(0);
if (temp -> perc() >= 0.75) {
out << waqt << ": " << temp -> getName() << " has returned laptop " << Laptop.Top() << " and went home HAPPY.\n";
happy++;
} else {
out << waqt << ": " << temp -> getName() << " has returned laptop " << Laptop.Top() << " and went home FRUSTRATED.\n";
}
}
if (flag && !LDM.isEmpty()) ++waqt;
}
//lab empty now
cout << waqt << endl;
} //lab check finish
else {
//else display close msg
cout << "Lab Band\n";
}
//summary
Review(out, waqt - start, happy, numOfStds, day);
} //days finish
} // programs finish
out.close();
in.close();
delete[] TAs;
} //int main ends
//global functions
Stack readLaptop(ifstream & f) {
Stack result;
int size;
f >> size;
for (int i = 0, a; i < size; i++) {
f >> a;
result.push(a);
}
return result;
}
void sort(teachingAssistant * ta, int size) {
teachingAssistant temp;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (ta[i].getName() < ta[j].getName()) {
temp = ta[i];
ta[i] = ta[j];
ta[j] = temp;
}
}
}
}
void writeProg(ofstream & f, int & prog) {
f << "************\n";
f << "Program " << prog << ":\n";
f << "************\n\n";
}
void writeDay(ofstream & f, int & day) {
f << "Day: ";
if (day == 1) f << "Monday\n\n";
else if (day == 2) f << "Tuesday\n\n";
else if (day == 3) f << "Wednesday\n\n";
}
void TAstarted(ofstream & f, teachingAssistant & TA, Time & t, int & day) {
if (TA.isAvailable(day, t)) {
if (t == TA.getStart(day))
f << t << ": " << TA.getName() << " has begun Lab Hours\n";
}
}
void TAended(ofstream & f, teachingAssistant & TA, Time & t, Time close, int & day) {
if (!TA.isChuti(day)) {
if (t == TA.getEnd(day)) {
f << t << ": " << TA.getName() << " has finished Lab Hours\n";
if (t == close)
f << t << ": There are no TAs on duty. Arsalan is now closed.\n";
}
}
}
void getHelp(ofstream & f, teachingAssistant & TA, Time & t) {
f << t << ": " << TA.getStudent() -> getName() << " is getting help from " <<
TA.getName() << endl;
}
Time closing(teachingAssistant * TAs, int t, int day) {
Time * temp = new Time[t];
for (int i = 0; i < t; i++) {
temp[i] = TAs[i].getEnd(day);
}
Time max = temp[0];
for (int i = 0; i < t; i++) {
if (temp[i] > max)
max = temp[i];
}
delete[] temp;
return max;
}
Time starting(teachingAssistant * TAs, int t, int day) {
Time * temp = new Time[t];
for (int i = 0; i < t; i++) {
temp[i] = TAs[i].getStart(day);
}
Time min = temp[0];
for (int i = 0; i < t; i++) {
if (temp[i] < min)
min = temp[i];
}
delete[] temp;
return min;
}
void Review(ofstream & f, Time t, int happy, int stds, int day) {
f << "\n";
if (day == 1)
f << "MONDAY's Lab Summary:\n";
else if (day == 2)
f << "TUESDAY's' Lab Summary:\n";
else if (day == 3)
f << "WednesDAY's Lab Summary:\n";
f << "The TA Lab was open for " << t.getHour() << " hours and " << t.getMinute() << " minutes. ";
f << stds << " students visited the lab.\n";
f << "Out of those students, only " << happy << " left happy.\nremaining left frustrated.\n\nLesson Learned: Do not procrastinate! Start programs early!\n\n";
}