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
|
const int NUM_REPAIR_BAYS = 4;
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <queue>
using namespace std;
// Vehicle info
struct Vehicle
{
int id; // -Id number
string Type; // V1, V2, etc)
bool Armour[4]; // bool: F,S,R,P)
string Weapons; // (list of strings w1, w2, w3...etc)
// Drive train
bool Transmission;
bool Differential;
char Engine; // (O,N,D) O-operational N-No engine D-Damaged
};
// functions
// create a Vehicle
Vehicle* create_vehicle(int id);
// print out vehicle details
void print(Vehicle* v);
// return available vehicle
Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq);
// return repair bay availabe
int repair_bay_available(Vehicle* repairBay[]);
// return true if vehicle pased test
bool passed_test(Vehicle* v);
// return false if vehicle pass test
bool failed_test(Vehicle* v);
int main()
{
int i;
queue<Vehicle*> q; // vehicle que
queue<Vehicle*> pq; // priority queue
Vehicle* repair_bay[NUM_REPAIR_BAYS]={0}; // repair bay
vector<Vehicle*> salvage_yard; // salvage yard
vector<Vehicle*> active_duty; // active duty
Vehicle* v;
int id = 0;
srand((unsigned int)time(0));
for(int t=0;t<5;t++)
{
// Create a number (0-4) new random vehicles per day
int n = rand() % 4;
// As the new vehicles are created they are placed into a queue
for(i=0;i<n;i++)
{
v = create_vehicle(id++);
cout << "creating vehicle: " << endl;
print(v);
q.push(v);
}
// The members of the queue (#max/time) are then processed by a "yard manager
// The front of the queue is popped and added to: Salvage Yard
// or Repair Bay if available Vector[4]
v = vehicle_available(q,pq):
// get available vehicle
if(v != NULL)
{
cout << "processing vehicle: ";
print(v);
i = repair_bay_available(repair_bay);
if(i >= 0)
{
cout << "Vehicle sent to repair bay #" << (i+1) << endl;
repair_bay[i] = v;
}
else
{
cout << "All repair bays filled, vehicle sent to salvage yard" << endl;
salvage_yard.push_back(v);
}
// When a vehicle in a bay is finished it is then run to testing, each component is tested,
// if all pass the vehicle is returned to duty else returned to a priority queue which will pre-empt
// the repair queue (fastest first)
// for each repair bay
for(i=0;i<NUM_REPAIR_BAYS;i++)
{
v = repair_bay[i];
if(v != NULL)
{
if(passed_test(v)) // passed test?
{
cout << "vehicle passed repair test, vehicle sent to active duty" << endl;
active_duty.push_back(v);
repair_bay[i]=0;
}
else if(failed_test(v)) // failed test
{
cout << "vehicle failed repair test, vehicle sent to priority queue" << endl;
pq.push(v);
repair_bay[i]=0;
}
}
}
}
}
cout << endl;
// print out whats in queue
cout << "Vehicles in left in Queue: " << endl;
while(q.size() > 0)
{
print(q.front());
q.pop();
}
cout << endl;
// print out whats in priority queue
cout << "Vehicles in left in priority Queue: " << endl;
while(pq.size() > 0)
{
print(pq.front());
pq.pop();
}
cout << endl;
// print out whats in storage bay
cout << "Vehicles in Repair Bay: " << endl;
for(i=0;i<NUM_REPAIR_BAYS;i++)
if(repair_bay[i] != 0)
print(repair_bay[i]);
cout << endl;
// print out whats in salvage yard
cout << "Vehicles in Salvage Yard: " << endl;
for(i=0;i<salvage_yard.size();i++)
print(salvage_yard[i]);
cout << endl;
// print out whats in active duty
cout << "Vehicles in Active Duty: " << endl;
for(i=0;i<active_duty.size();i++)
print(active_duty[i]);
cout << endl;
return 0;
}
//************************************************************************************************************
// create a Vehicle
Vehicle* create_vehicle(int id)
{
Vehicle* v = new Vehicle;
v->id =id;
int x = rand() % 4;
// V1, V2, etc)
v->Type = "V1";
if(x == 1)
v->Type = "V2";
if(x == 2)
v->Type = "V3";
if(x == 3)
v->Type = "V4";
v->Armour[0] = true; // bool: F,S,R,P)
v->Armour[1] = rand() % 2==0;
v->Armour[2] = rand() % 2==0;
v->Armour[3] = rand() % 2==0;
x = rand() % 3 + 1;
if(x == 1)
v->Weapons = "w1"; // (list of strings w1, w2, w3...etc)
if(x == 2)
v->Weapons = "w1, w2";
if(x == 3)
v->Weapons = "w1, w2, w3";
if(x == 4)
v->Weapons = "w1, w2, w3, w4";
// Drive train
v->Transmission = rand() * 2 == 0;
v->Differential = rand() * 2 == 0;
x = rand() % 3;
if(x == 0)
v->Engine = 'O'; // (O,N,D) O-operational N-No engine D-Damaged
if(x == 1)
v->Engine = 'N';
if(x == 2)
v->Engine = 'D';
return v;
}
//************************************************************************************************************
// print out vehicle details
void print(Vehicle* v)
{
cout << v->id << " " << v->Type << " ";
if(v->Armour[0]) cout << "F";
if(v->Armour[1]) cout << "S";
if(v->Armour[2]) cout << "R";
if(v->Armour[3]) cout << "P";
cout << " ";
cout << v->Weapons << " ";
if(v->Transmission) cout << "Transmission";
if(v->Differential) cout << "Differential";
cout << v->Engine << endl;
}
//************************************************************************************************************
// return vehicle avaiable
Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq)
{
Vehicle* v=NULL;
// check priority queue first for vehicles
if(pq.size() > 0)
{
v = pq.front();
pq.pop();
}
// check queue for vehicles
else if(q.size() > 0)
{
v = q.front();
q.pop();
}
return v;
}
//************************************************************************************************************
// return repair bay availabLe
int repair_bay_available(Vehicle* repair_bay[])
{
for(int i=0;i<NUM_REPAIR_BAYS;i++)
{
if(repair_bay[i] == NULL)
return i;
}
return -1;
}
//************************************************************************************************************
// return true if vehicle pased test
bool passed_test(Vehicle* v)
{
// 50 % vehicle pass
if(rand()%100>50)
{
// set repaired
v->Transmission=false;
v->Differential=false;
v->Engine='O';
return true;
}
else return false;
}
//************************************************************************************************************
// return false if vehicle pass test
bool failed_test(Vehicle* v)
{
return rand()%100<20;
}
|