-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
221 lines (156 loc) · 4.12 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
#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <cstddef>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define BUFFER_SIZE 2000
int b,flg=0;
int ct;
int ins=0;
//Including the data structure files in the main file
ofstream fout;
#include "heap.h"
#include "rbt.h"
#include "heap.cpp"
#include "rbt.cpp"
class construct{
private:
int counter;
rbt* myrbt;
heap* myheap;
protected:
public:
void print(int buildingNum); //Print(buildingNumber,executed_time,total_time) for given buildingNum
void print(int buildingNum1,int buildingNum2); //Print(buildingNum,executed_time,total_time) for given range of buildingNums
void insert(int buildingNum, int time); //Insert buldingNum and and total time
construct();
void syncTime(int time); //Synchronizes time between global time and counter
int dispatch(int); //Send building for construction
int ifbuild(); //Checks if any building is left to build
};
int construct::dispatch(int x){
int retval = 0;
heapnode* thisnode = &(myheap->root[1]);
thisnode->exec_time+=x;
if(thisnode->total_time-thisnode->exec_time<=0){
retval = thisnode->exec_time-thisnode->total_time;
b=thisnode->bnum;
flg=1;
myrbt->deletenode(thisnode->twin);
myheap->removeMin();
}
myheap->heapify();
return retval;
}
void construct::syncTime(int time){
int param,remain;
while(time>counter){
if(time-counter < 5 && ins==0)
{
param = time-counter;
}
else{
param = 5;
}
if(ifbuild()){
remain = dispatch(param);
counter+=param-remain; ct=counter;
}
else{
counter=time;
ct=counter;
}
}
}
construct::construct(){
counter=0;
myheap = new heap(BUFFER_SIZE);
myrbt = new rbt;
}
void construct::print(int buildingNum){
myrbt->findnode(buildingNum);
}
void construct::print(int buildingNum1,int buildingNum2){
myrbt->inorder(buildingNum1,buildingNum2);
}
int construct::ifbuild(){
return myheap->last;
}
void construct::insert(int buildingNum,int totaltime){
rbtnode* temp2 = myrbt->insert(buildingNum);
//insert into heap
myheap->insert(buildingNum,0,totaltime,temp2);
}
int main(int argc, char** argv){
construct myconstruct; // create a construct object
char *ptr,*ptr1,buff[100];
int num;
ifstream file;
if(argc<2){
cout<<"Error: Not enough arguments"<<endl;
exit(1);
}
file.open(argv[1]); // open input file
fout.open("output_file.txt"); //open output file
for( std::string line; getline( file, line, '\n'); ){
//variables for string parsing
int temp1,temp2;
ptr = strcpy(buff,line.c_str());
ptr = strtok(ptr,":");
num = atoi(ptr);
ptr = strtok(NULL,"(");
if(strcmp(ptr+1,"Insert")==0){
ins=1;
}
else{
ins=0;
}
if(strcmp(ptr+1,"PrintBuilding")==0){
ptr = strtok(NULL,")");
int i,flag=0;
for(i=0;i<strlen(ptr);i++){
if(ptr[i]==',') {
flag=1;
break;
}
}
if(flag==1){
// print range of buildings
ptr1 = strtok(ptr,",");
ptr += strlen(ptr1)+1;
myconstruct.print(atoi(ptr1),atoi(ptr));
}
else{
// print single job
myconstruct.print(atoi(ptr));
}
}
//schedule buildings until counter = command time
myconstruct.syncTime(num);
// Insert new building
if(strcmp(ptr+1,"Insert")==0){
ptr = strtok(NULL,",");
temp1 = atoi(ptr);
ptr = strtok(NULL,")");
temp2 = atoi(ptr);
myconstruct.insert(temp1,temp2);
}
}
int r;
// End of input file. schedule all the pending buildings
while(myconstruct.ifbuild()){
r=myconstruct.dispatch(5);
ct+=5-r;
if(flg==1){
flg=0;
fout<<"("<<b<<","<<ct<<")"<<endl;
}
}
//close file handlers
file.close();
fout.close();
return 0;
}