-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
513 lines (419 loc) · 15 KB
/
Program.cs
File metadata and controls
513 lines (419 loc) · 15 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using System.Collections;
using System.Runtime.Remoting;
using Microsoft.VisualBasic.CompilerServices;
class Program
{
public static void Main()
{
string filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + "\\milliveri.txt";
Console.WriteLine("---------------------------------------------Project part 1---------------------------------------------");
List<NationalPark> list1 = new List<NationalPark>(), list2 = new List<NationalPark>();
List<NationalPark>[] genListArr = { list1, list2 }; //Generic List Array
AddByArea(genListArr, filePath);
WriteGenArr(genListArr);
Console.WriteLine("---------------------------------------------Project part 2---------------------------------------------");
var maxSize = 0;
foreach (var i in genListArr) maxSize += i.Count; // maxsize identifying from Project 1 total Parks.
var stackCherry = new StackCherry(maxSize);
var qCherry = new QCherry(maxSize);
var pqCherry = new PQCherry(maxSize);
Console.WriteLine("------------STACK------------");
stackCherry.AddAllItemsToStack(filePath);
stackCherry.RemoveAllItemsInStack();
Console.WriteLine("------------Queue------------");
qCherry.AddAllItemsToQuee(filePath);
qCherry.RemoveAllItemsInQ();
Console.WriteLine("---------------------------------------------Project part 3---------------------------------------------");
Console.WriteLine("------------PriorityQueue------------");
pqCherry.AddAllItemsToQ(filePath);
pqCherry.RemoveAllItemsInPQ();
Console.WriteLine("---------------------------------------------Project part 4---------------------------------------------");
Console.WriteLine("------------Queue/VS\\PriorityQueue------------");
int[] numberOfProduct1 = { 8, 9, 6, 7, 10, 1, 11, 5, 3, 4, 2 };
List<int> numberOfProduct2 = new List<int>(numberOfProduct1);
var qCherry1 = new QCherry(numberOfProduct1);
var pqCherry1 = new PQCherry(numberOfProduct2);
var costumerQ = new CostumerQandPQ(qCherry1, pqCherry1);
Console.WriteLine("Queue Average Of Waiting Time : " + costumerQ.CalculateQTime());
Console.WriteLine("Priority Queue Average Of Waiting Time : " + costumerQ.CalculatePQtime());
Console.WriteLine("---------------------------------------------------------------------------------------------------------------");
Console.ReadLine();
}
public static void AddByArea(List<NationalPark>[] genListArr, string filePath)
{
int MINIMUM_AREA = 15000;
string line;
string[] lineData;
using (StreamReader reader = new StreamReader(filePath))
{
while ((line = reader.ReadLine()) != null)
{
lineData = line.Split("\t");
var currentpark = new NationalPark(lineData[0], lineData[1], RemoveThousandSeparators(lineData[2]), lineData[3]);
if (currentpark.parkArea < MINIMUM_AREA)
{
genListArr[0].Add(currentpark);
}
else
{
genListArr[1].Add(currentpark);
}
}
reader.Close();
}
}
public static int RemoveThousandSeparators(string includeThousandSeparatorsData)
{
return int.Parse(includeThousandSeparatorsData.Replace(",", ""));
}
public static void WriteGenArr(List<NationalPark>[] genArr)
{
int total = 0;
Console.WriteLine("--------------------------------------------The Least of 15000--------------------------------------------\n");
for (int i = 0; i < genArr.Length; i++)
{
for (int j = 0; j < genArr[i].Count; j++)
{
Console.WriteLine(genArr[i][j].ToString());
total += genArr[i][j].parkArea;
}
Console.WriteLine("**** Total Area of List" + i + " : " + total + " ha(hectare)");
total = 0;
if (i == 0)
{
Console.WriteLine("--------------------------------------------The Above of 15000--------------------------------------------\n");
}
}
Console.WriteLine("----------------------------------------------------------------------------------------------------------\n");
}
}
public class NationalPark
{
public string parkName;
public string cityNames;
public string acceptedDate;
public int parkArea;
public NationalPark(string parkName, string cityNames, int parkArea, string acceptedDate)
{
this.parkName = parkName;
this.cityNames = cityNames;
this.acceptedDate = acceptedDate;
this.parkArea = parkArea;
}
public string ToString()
{
return parkName + ", " + cityNames + ", " + parkArea + ", " + acceptedDate;
}
}
public class StackCherry
{
private int maxSize;
private NationalPark[] stackArray;
private int top;
public StackCherry(int maxSize)
{
this.maxSize = maxSize;
stackArray = new NationalPark[maxSize];
top = -1;
}
public void AddAllItemsToStack(string filePath)
{
string line;
string[] lineData;
using (StreamReader reader = new StreamReader(filePath))
{
while ((line = reader.ReadLine()) != null && (maxSize - 1 != top))
{
lineData = line.Split("\t");
var currentpark = new NationalPark(lineData[0], lineData[1], int.Parse(lineData[2].Replace(",", "")), lineData[3]);
Push(currentpark);
}
}
}
public void RemoveAllItemsInStack()
{
while (top != -1)
{
Console.WriteLine("Removed Item : " + pop().parkName);
}
Console.WriteLine("--------------------------------------------------------------------------------------------------------");
}
public void Push(NationalPark nationalPark)
{
stackArray[++top] = nationalPark;
}
public NationalPark pop()
{
return stackArray[top--];
}
public bool IsEmpty()
{
return top == -1;
}
public bool IsFull()
{
return (top == maxSize - 1);
}
public int GetMaxSize()
{
return maxSize;
}
public NationalPark[] GetStackArray()
{
return stackArray;
}
public int GetTop()
{
return top;
}
}
public class QCherry
{
public int maxSize;
public Object queArray;
public int front;
public int rear;
public int nItems;
public QCherry(Object obj) // queArray could be int[] type so, the constructor have to Object class parameter. Object[] not able to because of int values are not objects.
{
queArray = obj;
}
public QCherry(int s) // constructor
{
maxSize = s;
front = 0;
rear = -1;
nItems = 0;
queArray = new Object[maxSize];
}
public void AddAllItemsToQuee(string filePath)
{
string line;
string[] lineData;
using (StreamReader reader = new StreamReader(filePath))
{
while ((line = reader.ReadLine()) != null && (maxSize - 1 != rear) && !IsFull())
{
lineData = line.Split("\t");
var currentPark = new NationalPark(lineData[0], lineData[1], int.Parse(lineData[2].Replace(",", "")), lineData[3]);
Insert(currentPark);
}
}
}
public void RemoveAllItemsInQ()
{
while (rear != -1 && !IsEmpty())
{
Console.WriteLine("Removed Item : " + ((NationalPark)Remove()).parkName);
}
Console.WriteLine("--------------------------------------------------------------------------------------------------------");
}
public void Insert(NationalPark nationalPark)
{
if (rear == maxSize - 1)
{
rear = -1;
} ((object[])queArray)[++rear] = nationalPark;
nItems++;
}
public Object Remove()
{
Object temp = ((object[])queArray)[front++];
if (front == maxSize)
{
front = 0;
}
nItems--;
return temp;
}
public int RemoveProduct()
{
int temp = ((int[])queArray)[front++];
if (front == maxSize)
{
front = 0;
}
nItems--;
return temp;
}
public Object PeekFront()
{
return ((object[])queArray)[front];
}
public bool IsEmpty()
{
return nItems == 0;
}
public int size()
{
return nItems;
}
public bool IsFull()
{
return (nItems == maxSize - 1);
}
}
public class PQCherry
{
public int capacity;
public Object pqList; // The type is Object because of Object type value contains the both of NationalPark[] int[] items. Object[] isn't be okay because of the int values are not object type.
public int nItems;
public PQCherry(int s) // constructor
{
capacity = s;
pqList = new List<NationalPark>();
nItems = 0;
}
public PQCherry(List<int> list) // constructor
{
capacity = list.Count;
pqList = list;
nItems = 0;
}
public void AddAllItemsToQ(string filePath)
{
string line;
string[] lineData;
using (StreamReader reader = new StreamReader(filePath))
{
while ((line = reader.ReadLine()) != null && !IsFull())
{
lineData = line.Split("\t");
var currentpark = new NationalPark(lineData[0], lineData[1], int.Parse(lineData[2].Replace(",", "")), lineData[3]);
Insert(currentpark);
}
}
}
public void RemoveAllItemsInPQ()
{
while (!IsEmpty())
{
Console.WriteLine("Removed Item : " + ((NationalPark)RemovePark()).parkName);
}
Console.WriteLine("--------------------------------------------------------------------------------------------------------");
}
public void Insert(NationalPark obj) // insert function for NationalPark items
{
((List<NationalPark>)pqList).Add(obj);
nItems++;
}
public void Insert(int value)
{
((List<int>)pqList).Add((value));
nItems++;
}
public NationalPark RemovePark() //Remove function for NationalPark items.
{
int minArea = 9999999;
int tempI = 0;
for (int i = 0; i < ((List<NationalPark>)pqList).Count; i++)
{
if (minArea > (((List<NationalPark>)pqList)[i]).parkArea) // This method created because of just NationalPark items so we know the pqList type. That is the reason of TypeCast.
{
minArea = ((List<NationalPark>)pqList)[i].parkArea;
tempI = i;
}
}
nItems--;
var removedItem = ((List<NationalPark>)pqList)[tempI];
((List<NationalPark>)pqList).RemoveAt(tempI);
return removedItem;
}
public int RemoveProducts() // Remoce function for List<int> type values.
{
List<int> tempArr;
int tempInt = 99999;
int tempIndex = 0;
if (pqList.GetType() == typeof(List<int>))
{
tempArr = (List<int>)pqList;
for (int i = 0; i < tempArr.Count; i++)
{
if (tempInt > tempArr[i])
{
tempInt = tempArr[i];
tempIndex = i;
}
}
((List<int>)pqList).RemoveAt(tempIndex);
return tempInt;
}
else
{
return tempInt; // impossible
}
}
public object PeekFront()
{
if (pqList.GetType() == typeof(List<int>))
{
int[] value = (int[])pqList;
return value.Last();
}
else
{
return ((List<NationalPark>)pqList).Last(); //
}
}
public bool IsEmpty()
{
return nItems == 0;
}
public int Size()
{
return nItems;
}
public bool IsFull()
{
return (nItems == capacity - 1);
}
}
public class CostumerQandPQ
{
public QCherry qCherry;
public PQCherry pqCherry;
public double productWaiting;
public CostumerQandPQ(QCherry qCherry, PQCherry pqCherry)
{
this.qCherry = qCherry;
this.pqCherry = pqCherry;
productWaiting = 3.0; // default 3 seconds.
}
public double CalculateQTime() // using q class and enter the int type values so, qCherry.qArr is equals the int[].
{
int[] productQ = ((int[])qCherry.queArray); // read the upper sentence. Actually the qCherry.qArr type is object but we know the qCherry.qArr equals to int[]. That is the reason of type cast.
double sumAllPeople = 0;
double sumOnePerson = 0;
double avarageSum;
int tempProduct;
Console.WriteLine("-------------------------------------Calculating Queue time...-------------------------------------");
for (int i = 0; i < productQ.Length; i++)
{
tempProduct = qCherry.RemoveProduct();
sumOnePerson += tempProduct * productWaiting;
Console.WriteLine("Number Of Product : " + tempProduct + ", Waiting Time : " + sumOnePerson);
sumAllPeople += sumOnePerson;
}
avarageSum = sumAllPeople / (double)productQ.Length;
return avarageSum;
}
public double CalculatePQtime() // using q class and enter the int type values so, pqCherry.qGenList is equals the int[].
{
List<int> collection = (List<int>)pqCherry.pqList; // read the upper sentence. Actually the pqCherry.qGenList type is object but we know the pqCherry.qGenList equals to List<int>. That is the reason of type cast.
int collectionCount = collection.Count;
double sumOnePerson = 0;
double sumAllPeople = 0;
double avarageSum;
int tempProduct;
Console.WriteLine("-------------------------------------Calculating Priority Queue time...-------------------------------------");
for (int i = 0; i < collectionCount; i++)
{
tempProduct = pqCherry.RemoveProducts();
sumOnePerson += tempProduct * productWaiting; // One person
Console.WriteLine( "Number Of Product : " + tempProduct + ", Waiting Time : " + sumOnePerson);
sumAllPeople += sumOnePerson;
}
avarageSum = sumAllPeople / (double)collectionCount;
return avarageSum;
}
}