-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
295 lines (276 loc) · 10.3 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
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
#include <iostream>
#include <string>
#include <vector>
#include "Station.cpp"
using namespace std;
string charArrayToString(char* a, int size) { //
int i;
string s;
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}
string stringSplitter(const string* string1, const char parameter, int index) { // splits the string by the given parameter
//and returns the preffered indexed string from splitted strings
char* buffer = new char[string1->length()];
int splitTime = -1;
int bufferIndex = 0;
for (char c : *string1) {
if (c != parameter) {
*(buffer + bufferIndex) = c;
bufferIndex++;
}
else {
splitTime++;
if (splitTime == index) {
return charArrayToString(buffer, bufferIndex);
}
bufferIndex = 0;
}
}
if(index>splitTime+1)
return "NULL";
return charArrayToString(buffer, bufferIndex);
}
void setPackages(ifstream* packages, vector<Station*>* stations) {
string command;
string initialPosition;
string packageName;
for (command; getline(*packages, command);) {
packageName = stringSplitter(&command,' ',0);
initialPosition = stringSplitter(&command,' ',1);
Packages* newPackage;
for(auto station: *stations){
if(station->gettName()== initialPosition){
newPackage = new Packages(packageName);
station->packages.push(newPackage);
}
}
}
}
void setDests(ifstream* dests, vector<Station*>* stations) {
string command;
Station* newStation;
for (command; getline(*dests, command); ) {
newStation = new Station(command);
stations->push_back(newStation);
}
}
void setTrucks(ifstream* trucks, vector<Station*>* stations){
string command;
string initialPosition;string truckName;string power;
Truck* tempTruck;
for (command; getline(*trucks, command); ) {
truckName = stringSplitter(&command,' ',0);
initialPosition = stringSplitter(&command,' ',1);
power = stringSplitter(&command,' ',2);
for(Station* station: *stations){
if(station->gettName()== initialPosition){
tempTruck = new Truck(truckName, truckName, initialPosition, power);
station->trucks.enqueue(tempTruck);
}
}
}
}
//_packages class is for moving truck and its' packages
class _packages{
public:
static _packages* head;
Packages* data;
_packages* next;
_packages* prev;
_packages(){}
_packages(Truck* truck){//we use _packages to implement moving truck
//when we initialize the object with a truck object. This constructor puts the truck* to head
this->head = new _packages;
this->head->data = truck;
this->head->next= NULL;
this->head->prev = NULL;
}
void addPackage(Packages* newPackage){
_packages* tempPackage = this->head;
for(; tempPackage->next!=NULL;tempPackage=tempPackage->next){}
tempPackage->next = new _packages();
tempPackage->next->data = newPackage;
tempPackage->next->prev = tempPackage;
tempPackage->next->next = NULL;
}
Packages* popPackage(int index){
_packages* tempPackage = this->head;
for(int i=-1; tempPackage!=NULL;tempPackage=tempPackage->next, i++){
if(i==index){
tempPackage->prev->next = tempPackage->next;
//if the element that we wanna pop isnt at the end of the list
//we change next nodes prev
if(tempPackage->next!=NULL){
tempPackage->next->prev = tempPackage->prev;
}
return tempPackage->data;
}
}
return NULL;
}
void printWhole(){
_packages* temp=head;
if(head==NULL){
return;
}
for(;temp!=NULL;temp=temp->next){
cout<<temp->data->getName() <<" ";
}
cout<<endl;
}
bool isEmpty(){
if(head->next==NULL)
return true;
return false;
}
};
_packages* movingTruck = new _packages;
_packages* _packages::head = new _packages;
void firstStationToMiddle(vector<Station*>* stations,int x,string startingStation){
Truck* tempTruck;
Packages* tempPackage;
for(Station* station: *stations){
if(station->gettName()== startingStation){
station->trucks.getFront(tempTruck);
if(tempTruck==NULL){
cout<<"There's no truck to be used in this station("<<station->gettName()<<")\n";
return;
}
station->trucks.dequeue();
movingTruck = new _packages(tempTruck);
//getting packages from first station
for(int i=0;i<x;i++){
station->packages.getTop(tempPackage);
if(tempPackage==NULL){
cout<<"There's no enough package in first statin\n";
return;
}
station->packages.pop();
movingTruck->addPackage(tempPackage);
}
}
}
}
void addAndReleasePacksToMiddle(vector<Station*>* stations, int y, string middleStation,string zValues){
Truck* tempTruck;
Packages* tempPackage;
for(Station* station: *stations){
if(station->gettName()== middleStation){
for(int i=0;i<y;i++){//getting packages from middle station
tempPackage = new Packages();
if(tempPackage==NULL){
cout<<"You cant get that much package from that station\n";
return;
}
station->packages.getTop(tempPackage);
station->packages.pop();
movingTruck->addPackage(tempPackage);
}
//releasing the given indeces of packages to middle station
int iThZvalue;
vector<int> zvalues;
vector<string> removedPackageNames;
_packages* tempNode= movingTruck->head->next;
for(int i=0;;i++){ //seperating the z indices at string
if(stringSplitter(&zValues,',',i) == "NULL"){
break;
}
iThZvalue = stoi(stringSplitter(&zValues,',',i));
zvalues.push_back(iThZvalue);
}
for(int i: zvalues){ //it stores the names of given indices, with given order
tempNode= movingTruck->head->next;//That will ease to remove the given indeces from linked list
for(int k= 0;k<i;k++){
tempNode=tempNode->next;
if(tempNode==NULL){
break;
}
}
removedPackageNames.push_back(tempNode->data->getName());
}
for(string packageName : removedPackageNames){
for(tempNode = movingTruck->head->next; tempNode!=NULL ; tempNode=tempNode->next){
if(tempNode->data->getName() == packageName){
if(tempNode->next==NULL & tempNode->prev==movingTruck->head){//if there s only 1 package in truck
movingTruck->head->next = NULL;
}
else if(tempNode->next ==NULL){ //if the given element is at the end of the linked list
tempNode->prev->next=NULL;
}
else{
tempNode->prev->next = tempNode->next;
tempNode->next->prev = tempNode->prev;
}
station->packages.push(tempNode->data);
}
}
}
break;
}
}
}
void releasingToFinalStation(vector<Station*>* stations,string endStation){
for(Station* station: *stations) {
if (station->gettName() == endStation) {
while(!movingTruck->isEmpty()){
station->packages.push(
movingTruck->popPackage(0) );
}
Truck* truck = static_cast<Truck *>(movingTruck->head->data);
station->trucks.enqueue(truck);
delete movingTruck;
break;
}
}
}
void moveTruck(string* command, vector<Station*>* stations){
string startingStation = stringSplitter(command,'-',0);
string middleStation = stringSplitter(command,'-',1);
string endStation = stringSplitter(command,'-',2);
int x = stoi(stringSplitter(command,'-',3));
int y = stoi(stringSplitter(command,'-',4));
const string zValues = stringSplitter(command,'-',5);
//gets the first truck from given station
//gets the packages from first station
firstStationToMiddle(stations,x,startingStation);
//gets y packages from middle station
//and releases the indexes inside zValues
addAndReleasePacksToMiddle(stations,y,middleStation,zValues);
//moving truck reachs the final destination
//we put truck to garage & we put packages to stack
releasingToFinalStation(stations,endStation);
}
void printStations(vector<Station*>* stations){
for(Station* station: *stations){
output<<station->gettName()<<endl;
output<<"Packages:\n";
station->packages.printWhole();
output<<"Trucks:\n";
station->trucks.printWhole();
output<<"-------------\n";
}
}
int main(int argc, char* argv[]) {
//argument order desk,package,truck,mission,result
string command;
ifstream dests, packages, trucks, missions;
dests.open(argv[1]);
packages.open(argv[2]);
trucks.open(argv[3]);
missions.open(argv[4]);
output.open(argv[5]);
vector<Station*> stations;
setDests(&dests,&stations);
setTrucks(&trucks,&stations);
setPackages(&packages,&stations);
//getting missions
for (command; getline(missions, command); ) {
moveTruck(&command,&stations);
}
printStations(&stations);
output.close();
return 0;
}