-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prog4.cpp
145 lines (129 loc) · 3.26 KB
/
Prog4.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
/*
Program 4
Rabih Salamey & Hassan Mehdi
CIS 200 - 02
12/13/2016
*/
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std; //Simplicities sake.
int main()
{
int t, p, nt, np, time = 0;
vector<int> itemsPerTrain, itemsPerPlane;
vector<stack<int>> dockStacks; //vector of stacks pertaining to train
queue<int> assemblyLine;
vector<int> trainFinalTime, planeFinalTime;
cin >> t >> p >> nt >> np;
int trainItemsToAdd = nt;
/*
Precondition: Initializes value placeholders for time
Postconditon: When the final time is found, finalTime will hold it.
*/
//temporary data for the time vectors
for (int i = 0; i < t; i++) {
trainFinalTime.push_back(0);
}
for (int i = 0; i < p; i++) {
planeFinalTime.push_back(0);
}
//inputting the number of items on a train
/*
Precondition: t must be greater than 0
PostCondition: each train is assigned a number of items to be given
*/
for (int i = 0; i < t; i++) {
int temp;
cin >> temp;
itemsPerTrain.push_back(temp);
}
//inputting the number if items on a plane
/*
Precondition: p must be greater than 0
PostCondition: each plane is assigned a number of items to be given
*/
for (int i = 0; i < p; i++) {
int temp;
cin >> temp;
itemsPerPlane.push_back(temp);
}
//loading the stacks on the dock
/*
Precondition: items must not be 0
PostCondition: stacks are loaded onto the dock
*/
for (int i = 0; i < t; i++) {
stack<int> tempStack;
if (trainItemsToAdd >= 5) { // Either >= or > will work, it only changes who handles the stack.
for (int j = 0; j < 5; j++) {
int temp;
cin >> temp;
tempStack.push(temp);
}
trainItemsToAdd -= 5;
dockStacks.push_back(tempStack);
}
else {
for (int j = 0; j < trainItemsToAdd; j++) {
int temp = 0;
cin >> temp;
tempStack.push(temp);
}
trainItemsToAdd = 0;
dockStacks.push_back(tempStack);
}
}
//calculating train time
/*
Precondition: dockStacks must not be empty
PostCondition: time for trains to be loaded is calculated
*/
for (int i = 0; i < t; i++)
while (dockStacks[i].empty() != true) {
time += dockStacks[i].top() * 2;
if (--itemsPerTrain[dockStacks[i].top() - 1] == 0)
trainFinalTime[dockStacks[i].top() - 1] = time - dockStacks[i].top();
dockStacks[i].pop();
}
//loading plane items
/*
Precondition: np must be greater than 0
PostCondition: items are loaded
*/
for (int i = 0; i < np; i++) {
int temp;
cin >> temp;
assemblyLine.push(temp);
}
//we reuse the time variable here.
time = 0;
//calculating plane time
/*
Precondition: assemblyLine must not be empty
PostCondition: time for planes to be loaded is calculated
*/
while (assemblyLine.empty() != true) {
time += assemblyLine.front() * 10;
if (--itemsPerPlane[assemblyLine.front() - 1] == 0)
planeFinalTime[assemblyLine.front() - 1] = time - assemblyLine.front() * 10 / 2;
assemblyLine.pop();
}
//printing time for each train
/*
Precondition: N/A
PostCondition: time for each train is printed
*/
for (int i = 0; i < t; i++)
cout << trainFinalTime[i] << " ";
cout << endl;
//printing plane printing time for each plane
/*
Precondition: N/A
PostCondition: time for each plane is printed
*/
for (int i = 0; i < p; i++)
cout << planeFinalTime[i] << " ";
return 0;
}