-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
262 lines (259 loc) · 8.68 KB
/
main.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
#include <iostream>
#include <queue>
#include "Employee.h"
#include "Cashier.h"
#include "Barista.h"
#include <fstream>
#include "Customer.h"
#include <algorithm>
#include <iterator>
#include <sstream>
#include <iomanip>
using namespace std;
double utilization(Employee &e,double t){
return e.totalWork/t;
}
template<class Container>
void split1(const string& str, Container& cont){
istringstream iss(str);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),back_inserter(cont));
}
template<typename type>//ordering Customers acc to their arrival order
struct myCompRank {
bool operator()(const type & first, const type & second) const {
return first.rank > second.rank;
}
};
template<typename type>//ordering Customers according to their critical times
struct myCompTime {
bool operator()(const type & first, const type & second) const {
return first.t > second.t;
}
};
template<typename type>//ordering Customers according to their orders' price
struct myCompPrice {
bool operator()(const type & first, const type & second) const {
return first.price < second.price;
}
};
static void model1(priority_queue<Customer, vector<Customer>,myCompTime<Customer>> ×, int cash,char* argv[]);
static void model2(priority_queue<Customer, vector<Customer>,myCompTime<Customer>> ×, int cash,char* argv[]);
int main(int argc, char* argv[]) {
if(argc!=3){
cout<<"Run the code with following command: ./projext1 [input_file] [output_file]"<<endl;
return 1;
}
ifstream infile(argv[1]);
string line;
getline(infile, line);
const int cash = stoi(line);
getline(infile, line);
const int cus = stoi(line);
priority_queue<Customer, vector<Customer>,myCompTime<Customer>> times;
priority_queue<Customer, vector<Customer>,myCompTime<Customer>> times2;
for (int i = 0; i < cus; i++) {
getline(infile, line);
vector<string> words;
split1(line, words);
times.push(Customer(stod(words[0]), stod(words[1]), stod(words[2]), stod(words[3]),i));
times2.push(Customer(stod(words[0]), stod(words[1]), stod(words[2]), stod(words[3]),i));
}
model1(times,cash,argv);
cout<<endl;
model2(times2,cash,argv);
return 0;
}
static void model1(priority_queue<Customer, vector<Customer>,myCompTime<Customer>> ×,int cash,char* argv[]){
Cashier cashArr[cash];
Barista barArr[cash / 3];
queue<Customer> cashQ;
priority_queue<Customer, vector<Customer>, myCompPrice<Customer> > barQ;//Barista priority queue according to price of order
priority_queue<Customer, vector<Customer>,myCompRank<Customer>> last;
int maxLC = 0;//Holds maximum length of Cashier queue
int maxLB=0;//Holds maximum length of Barista queue
double t=0;//total work time
while(!times.empty()){
Customer c=times.top();
times.pop();
t=c.t;
if(c.action==1){
bool anyAvailable=false;//holds for is there any avaliable cashier
for (int i = 0; i < cash; i++) {//iterating till find a free cashier
if (cashArr[i].c== nullptr) {
cashArr[i].c=&c;
cashArr[i].totalWork+=c.orderTime;
c.action++;
c.e=i;
c.t=t+c.orderTime;
times.push(c);
anyAvailable=true;
break;
}
}
if(!anyAvailable){//not found available cashier, then push the customer to the queue
cashQ.push(c);
if(cashQ.size()>maxLC)
maxLC=cashQ.size();
}
}
else if(c.action==2){
cashArr[c.e].c=nullptr;
if(!cashQ.empty()){//cashierQ is not empty, then move the first customer to newly freed cashier.
Customer a=cashQ.front();
cashQ.pop();
cashArr[c.e].c=&a;
cashArr[c.e].totalWork+=a.orderTime;
a.action++;
a.e=c.e;
a.t=t+a.orderTime;
times.push(a);
}
bool anyAvailable=false;
for(int i=0;i<cash/3;i++){//iterating till find a free barista
if(barArr[i].c== nullptr){
barArr[i].c=&c;
barArr[i].totalWork+=c.brewTime;
c.action++;
c.e=i;
c.t=t+c.brewTime;
times.push(c);
anyAvailable=true;
break;
}
}
if(!anyAvailable){
barQ.push(c);
if(barQ.size()>maxLB)
maxLB=barQ.size();
}
}
else if(c.action==3){
barArr[c.e].c=nullptr;
last.push(c);
if(!barQ.empty()){//BaristaQ not empty, then move the Customer to the newly freed Barista
Customer a=barQ.top();
barQ.pop();
barArr[c.e].c=&a;
barArr[c.e].totalWork+=a.brewTime;
a.action++;
a.e=c.e;
a.t=t+a.brewTime;
times.push(a);
}
}
}
ofstream myfile;
myfile.open(argv[2],ios::trunc);
myfile<<fixed<<setprecision(2)<<t<<endl;
myfile<<maxLC<<endl;
myfile<<maxLB<<endl;
for(int i=0;i<cash;i++){
myfile<<utilization(cashArr[i],t)<<endl;
}
for(int i=0;i<cash/3;i++){
myfile<<utilization(barArr[i],t)<<endl;
}
while(!last.empty()){//prints customer's total time of order
myfile<<(last.top()).t-(last.top().arrTime)<<endl;
last.pop();
}
myfile<<endl;
myfile.close();
}
static void model2(priority_queue<Customer, vector<Customer>,myCompTime<Customer>> ×, int cash,char* argv[]){
Cashier cashArr[cash];
Barista barArr[cash / 3];
queue<Customer> cashQ;
priority_queue<Customer, vector<Customer>, myCompPrice<Customer> > barQs[cash/3];
priority_queue<Customer, vector<Customer>,myCompRank<Customer>> last;
int maxLC = 0;
int maxLB[cash/3];
for(int i=0;i<cash/3;i++){
maxLB[i]=0;
}
double t=0;
while(!times.empty()){
Customer c=times.top();
times.pop();
t=c.t;
if(c.action==1){
bool anyAvailable=false;
for (int i = 0; i < cash; i++) {
if (cashArr[i].c== nullptr) {
cashArr[i].c=&c;
cashArr[i].totalWork+=c.orderTime;
c.action++;
c.e=i;
c.t=t+c.orderTime;
times.push(c);
anyAvailable=true;
break;
}
}
if(!anyAvailable){
cashQ.push(c);
if(cashQ.size()>maxLC)
maxLC=cashQ.size();
}
}
else if(c.action==2){
cashArr[c.e].c=nullptr;
if(!cashQ.empty()){
Customer a=cashQ.front();
cashQ.pop();
cashArr[c.e].c=&a;
cashArr[c.e].totalWork+=a.orderTime;
a.action++;
a.e=c.e;
a.t=t+a.orderTime;
times.push(a);
}
if(barArr[c.e/3].c== nullptr){
barArr[c.e/3].c=&c;
barArr[c.e/3].totalWork+=c.brewTime;
c.action++;
c.e=c.e/3;
c.t=t+c.brewTime;
times.push(c);
}else{
barQs[c.e/3].push(c);
if(barQs[c.e/3].size()>maxLB[c.e/3]){
maxLB[c.e/3]=barQs[c.e/3].size();
}
}
}
else if(c.action==3){
barArr[c.e].c=nullptr;
last.push(c);
if(!barQs[c.e].empty()){
Customer a=barQs[c.e].top();
barQs[c.e].pop();
barArr[c.e].c=&a;
barArr[c.e].totalWork+=a.brewTime;
a.action++;
a.e=c.e;
a.t=t+a.brewTime;
times.push(a);
}
}
}
ofstream myfile;
myfile.open(argv[2], ios::app);
myfile<<fixed<<setprecision(2)<<t<<endl;
myfile<<maxLC;
for(int i=0;i<cash/3;i++) {
myfile << endl << maxLB[i];
}
for(int i=0;i<cash;i++){
myfile<<endl<<utilization(cashArr[i],t);
}
for(int i=0;i<cash/3;i++){
myfile<<endl<<utilization(barArr[i],t);
}
while(!last.empty()){
myfile<<endl<<(last.top()).t-(last.top().arrTime);
last.pop();
}
myfile.close();
}