Skip to content

Commit

Permalink
update this semester homework
Browse files Browse the repository at this point in the history
  • Loading branch information
cylim committed Jun 26, 2015
1 parent 7bdc0b3 commit 4a0cd3d
Show file tree
Hide file tree
Showing 1,206 changed files with 178,507 additions and 1 deletion.
Binary file added csci203/Asgn2/Assignment 2.pdf
Binary file not shown.
Binary file added csci203/Asgn2/main
Binary file not shown.
180 changes: 180 additions & 0 deletions csci203/Asgn2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// \file main.cpp
// \brief a print job simulation program
// \author Lim Chee Yeong 4933643
// \date May, 2015

#include <iostream>
#include <fstream>
#include <iomanip>
#include "priorityQueue.h"


using namespace std;

const int size = 100; // size of the total printJob
printJob jobs[size]; // declaration the printJob object
printJob job; // current job the program is working on
priorityQueue pQueue;
int maxWaitJob; // the job which waited the longest
int currentJob = 0; // current job count that has been serviced
bool busy = false;
float processTime = 0.0;
float endCurrentServiceTime = 0.0; // keep track service end time of current job
float nextArrivalTime = 0.0; // keep track next arrival time of job
float waitTime = 0.0; // keep track of total waiting time of job
float previousTime = 0.0;

// populate data into the printJob object
bool populateData(){
ifstream in("print_jobs.txt"); // open file name 'print_jobs.txt' as in
// if the file is not exist, terminate the program.
if(!in){
cout << "File not found, program terminated." << endl;
return false; // to indicate that the function doesn't work properly
}

int counter = 0; // to indicate the right location to store in the printJob object
string temp;
getline(in, temp); // to eliminate the first line
// if it is not end of the file, proceed read in the data to printJob object
while(!in.eof()){
in >> jobs[counter].ID;
in >> jobs[counter].priority;
in >> jobs[counter].arrivalTime;
in >> jobs[counter].printTime;
jobs[counter].waitTime = 0;

counter++; // increase the counter, to indicate next object
}

in.close(); // close the file
return true; // to indicate that the function run properly
}

// Queue customer if server is busy
void queueUp(){
if (pQueue.getSize() > 0){
waitTime = waitTime + ((processTime - previousTime) * pQueue.getSize());
}
pQueue.insert(job);

// formatting to show the output nicely
cout << fixed << setprecision(2)
<< processTime << "\t " << job.ID << " queue up\t "
<< waitTime << "\t\t " << endCurrentServiceTime << "\t\t";
pQueue.showQueue(processTime - previousTime);
cout << endl;
}

// Start to process customer if server is not busy
void processArrival(){
if(busy){
queueUp();
} else {
busy = true;
endCurrentServiceTime = processTime + job.printTime;

// formatting to show the output nicely
cout << fixed << setprecision(2)
<< processTime << "\t " << job.ID << " starts\t "
<< waitTime << "\t\t " << endCurrentServiceTime << "\t\t";
pQueue.showQueue();
cout << endl;
}
}

// Finish servicing current customer
void processCompleted(){
previousTime = processTime;
processTime = endCurrentServiceTime;
busy = false;
currentJob++;

if(!pQueue.isEmpty()){
waitTime = waitTime +((processTime - previousTime) * pQueue.getSize());
// calculate the wait time for each job when it finished
jobs[currentJob-1].waitTime = processTime - jobs[currentJob-1].printTime - jobs[currentJob-1].arrivalTime;
// compare to get the longest wait time print job.
if(jobs[maxWaitJob-1].waitTime < jobs[currentJob-1].waitTime){
maxWaitJob = currentJob;
}
}

// formatting to show the output nicely
cout << fixed << setprecision(2)
<< processTime << "\t " << currentJob << " completes\t "
<< waitTime << "\t\t " << endCurrentServiceTime << "\t\t";
pQueue.showQueue(processTime - previousTime);
cout << endl;
}

int populateFiveMinutesCount(){
int fiveMinutesCount = 0;
for(int i=0; i<size; i++){
if(jobs[i].waitTime >= 5){
fiveMinutesCount += 1;
}
}
return fiveMinutesCount;
}

int main(){
if(!populateData()){ return 0; } // populate data into printJob object, if fail terminate the program

int count = 0;
// formatting to show the output nicely
cout << "Time\t Event\t\t Waiting Time\t Service Time\t Queue" << endl;
cout << "============================================================================" << endl;
cout << fixed << setprecision(2)
<< processTime << "\t " << "---\t\t "
<< waitTime << "\t\t " << endCurrentServiceTime << "\t\t";
pQueue.showQueue();
cout << endl;

do{
if(!busy){ //if server is free
if(pQueue.isEmpty()){ //if queue is empty
job = jobs[count];
nextArrivalTime = job.arrivalTime;
previousTime = processTime;
processTime = nextArrivalTime;
} else { //if got customer in the queue
pQueue.remove(job);
}
processArrival();
count++;
} else { //if server is busy
if(count < size){ //if still got customers to process
job = jobs[count];
nextArrivalTime = job.arrivalTime;
//if current customer finish first before the next arrival
if(endCurrentServiceTime <= nextArrivalTime){
processCompleted();
if(pQueue.isEmpty()){
job = jobs[count];
nextArrivalTime = job.arrivalTime;
previousTime = processTime;
processTime = nextArrivalTime;
} else {
pQueue.remove(job);
}
processArrival();
} else {
//if current customer finish after the next arrival
previousTime = processTime;
processTime = nextArrivalTime;
processArrival();
count++;
}
} else {//no more new customer
processCompleted();
}
}
} while((count < size) || (busy == true) || (pQueue.isEmpty() == false));

cout << "\nThe average waiting time is " << waitTime / size << " minutes." << endl;
cout << "The maximum waiting time job is " << maxWaitJob << ", which is " << jobs[maxWaitJob-1].waitTime << " minutes." << endl;
cout << "The probability of waiting time more than 5 minutes is " << populateFiveMinutesCount() / float(size) << "." << endl << endl;

return 0;
}
Binary file added csci203/Asgn2/main.o
Binary file not shown.
11 changes: 11 additions & 0 deletions csci203/Asgn2/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
main: main.o priorityQueue.o
g++ -o main main.o priorityQueue.o

main.o: main.cpp priorityQueue.h
g++ -c main.cpp

priorityQueue.o: priorityQueue.cpp priorityQueue.h
g++ -c priorityQueue.cpp

clean:
rm *.o
101 changes: 101 additions & 0 deletions csci203/Asgn2/print_jobs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
ID Priority Arrival Time Printing Time
1 4 0.87 3.17
2 1 1.69 3.39
3 3 1.94 3.37
4 1 2.49 1.99
5 1 2.92 0.58
6 4 3.34 4.57
7 2 3.64 2.32
8 1 3.81 3.03
9 3 4.07 3.55
10 1 4.94 1.13
11 4 5.77 2.81
12 3 6.22 2.59
13 4 7.19 3.08
14 3 7.5 3.2
15 3 8.06 4.47
16 2 8.91 1.32
17 1 9 4.56
18 1 9.64 3.64
19 4 9.81 4.19
20 1 10.01 3.47
21 2 10.17 1.03
22 2 11.14 3.24
23 1 11.91 4.81
24 3 12.11 4.32
25 4 12.33 3.6
26 1 13 3.15
27 2 13.25 1.72
28 1 14.18 2.13
29 2 14.41 3.52
30 3 14.89 3.73
31 1 15.59 0.61
32 2 16.09 0.6
33 1 16.48 0.52
34 3 16.78 1.6
35 4 17.74 2.31
36 2 18.58 2.52
37 4 19.35 2.42
38 2 20.02 1.29
39 3 20.54 0.71
40 4 21.1 4.71
41 3 21.39 3.27
42 3 22.13 1.4
43 2 22.56 2.39
44 2 23.52 3.19
45 2 23.8 3.21
46 1 23.8 3.82
47 4 23.95 4.46
48 1 24.01 1.4
49 1 24.35 1.46
50 3 24.35 1.19
51 4 24.93 4.6
52 1 25.74 3.92
53 1 26.13 3.86
54 4 26.97 2.12
55 3 27.74 4.29
56 4 28.38 3.61
57 2 28.71 3.57
58 3 28.93 4.7
59 2 29.88 1.69
60 2 30.75 2.22
61 4 31.09 2.1
62 4 31.63 3.32
63 2 32.01 4.06
64 1 32.84 2.15
65 2 33.56 1.64
66 3 33.72 2.23
67 2 34.37 0.72
68 3 35.31 4.9
69 2 35.39 4.59
70 1 35.72 0.99
71 3 36.63 2.89
72 1 37.02 0.83
73 1 37.57 2.06
74 2 37.7 0.96
75 2 37.78 2.93
76 2 38.65 1.38
77 2 38.71 3.23
78 2 38.95 0.88
79 2 39.33 4.18
80 3 40.17 4.31
81 2 40.22 3.83
82 3 40.6 2.81
83 4 41.58 3.07
84 4 42 2.8
85 1 42.23 3.7
86 2 42.54 3.81
87 4 43.09 3.27
88 4 43.73 4.66
89 3 44.17 3.27
90 1 45.09 1.71
91 1 45.98 1.71
92 4 46.3 2.71
93 4 46.98 2.62
94 4 47.12 2.08
95 4 47.46 2.69
96 2 47.84 4.91
97 1 48.26 4.74
98 2 48.46 2.51
99 3 49.24 4.28
100 3 49.57 3.65
99 changes: 99 additions & 0 deletions csci203/Asgn2/priorityQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// \file priorityQueue.cpp
// \brief a print job simulation program
// \author Lim Chee Yeong 4933643
// \date May, 2015

#include <iostream>
#include "priorityQueue.h"
using namespace std;

// inline function to get left or right
inline int left(int i) { return 2*i; }
inline int right(int i) { return 2*i+1; }

// Constructor
// Set the initial size as 0
priorityQueue::priorityQueue(){
size = 0;
}

// Add s to the heap with the given priority as its priority
void priorityQueue::insert(printJob node){
job[size] = node;
size++;

heap();
}

// Remove the string of highest priority and return that string
void priorityQueue::remove(printJob &node){
if (!isEmpty()){
node = job[0];
size--;

for(int i=0; i<size; i++){
job[i] = job[i+1];
}
}
}

// Heap the queue
void priorityQueue::heap(){
for(int i = size/2; i>=0; i--){
int position = i;
int child;
do{
child = position;
if(left(child) <= size && job[left(child)].priority > job[position].priority){
position = left(child);
}
if(right(child) <= size && job[right(child)].priority > job[position].priority) {
position = right(child);
}

if(position != child){
swap(job[position], job[child]);
}
} while(position != child);
}

//arrange();
}

// arrange the job by the shortest print time
void priorityQueue::arrange(){
for(int i=0; i < size-1; i++){
if(job[i].priority == job[i+1].priority){
if(job[i].printTime < job[i+1].printTime){
swap(job[i],job[i+1]);
}
}
}
}

void priorityQueue::showQueue(float waitTime){
if(!isEmpty()){
int count = 0;
while(count < size){
//calculate the lastest wait time,
//add it into the print job and then heap it.
job[count].waitTime += waitTime;
if(job[count].waitTime >= 2 && job[count].priority == 1){
job[count].priority = 2;
heap();
} else if(job[count].waitTime >= 6 && job[count].priority == 2){
job[count].priority = 3;
heap();
}

if(count < 8){
cout << job[count].ID << " ";
} else if(count == 8){
cout << "...";
}
count++;
}
} else {
cout << "---";
}
}
Loading

0 comments on commit 4a0cd3d

Please sign in to comment.