-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput.c
389 lines (353 loc) · 9.7 KB
/
input.c
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
/*****************************************************************************
* Copyright (C) Aditya Sathe satheaditya2014@gmail.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input.h"
/*
* Three functions below initialises all the link lists.
*/
void init_subject_list(subject_list *sub_list) {
sub_list->head = sub_list->tail = NULL;
}
void init_teacher_list(teacher_list *teach_list) {
teach_list->head = teach_list->tail = NULL;
teach_list->teacher_count = 0;
}
void init_standard_list(standard_list *std_list) {
std_list->head = std_list->tail = NULL;
std_list->standard_count = 0;
}
/*
* Stores the subject information in a link_list from the input file of structures.
* It takes a lab and theory subjects linklist pointers as input and creates the list.
*/
void input_subject(subject_list *lab_sub_list, subject_list *th_sub_list, char *sub_ip) {
FILE *fp;
subject_info **node, *tmp[128];
int i = 0, j, flag = 0, cnt = 0, cred, num, k = 0, lab = 0, allocate = 0;
char ch, token[32];
fp = fopen(sub_ip, "r");
if(fp == NULL) {
perror("\n\n\n\n\t\t\tThere is some error while opening 'subject_input'\n\n\n\n");
exit (1);
}
node = (subject_info **)malloc(8 * sizeof(subject_info *));
tmp[0] = (subject_info *)malloc(sizeof(subject_info));
while((ch = getc(fp)) != EOF) {
if(ch == '$')
lab = 1;
if(ch == '"') {
cnt++;
flag = 0;
if(cnt % 2) {
flag = 1;
continue;
}
else {
token[i] = '\0';
if(cnt == 2)
strcpy(tmp[k]->subject_ID, token);
else if(cnt == 4)
strcpy(tmp[k]->subject_name, token);
else if(cnt == 6) {
j = cred = 0;
while(token[j] != '\0') {
num = token[j] - '0';
cred = 10 * cred + num;
j++;
}
tmp[k]->credits = cred;
tmp[k]->next = NULL;
if(lab) {
if(lab_sub_list->head) {
lab_sub_list->tail->next = tmp[k];
lab_sub_list->tail = tmp[k];
} else
lab_sub_list->tail = lab_sub_list->head = tmp[k];
} else {
if(th_sub_list->head) {
th_sub_list->tail->next = tmp[k];
th_sub_list->tail = tmp[k];
} else
th_sub_list->tail = th_sub_list->head = tmp[k];
}
allocate = 1;
}
flag = 0;
i = 0;
}
}
if(flag) {
token[i] = ch;
i++;
}
if(cnt == 6 && allocate) {
k++;
allocate = 0;
if(k == 8)
node = (subject_info **)realloc(node, 2 * k * sizeof(subject_info *));
tmp[k] = (subject_info *)malloc(sizeof(subject_info));
cnt = 0;
}
}
free(tmp[k]);
fclose(fp);
}
/*
* Stores the teacher information in a link list of structures - teacher_info.
* It takes teacher link list as argument and creates it from file teacher_input.
*/
void input_teacher(teacher_list *teach_list, char *teach_ip) {
FILE *fp;
teacher_info **node, *tmp[128];
int i = 0, j, flag = 0, cnt = 0, work, num, k = 0, sub_no = 0, spc_cnt = 1;
char ch, token[32];
fp = fopen(teach_ip, "r");
if(fp == NULL) {
perror("\n\n\n\n\t\t\tThere is some error while opening 'teacher_input'\n\n\n\n");
exit (1);
}
node = (teacher_info **)malloc(8 * sizeof(teacher_info *));
tmp[0] = (teacher_info *)malloc(sizeof(teacher_info));
while(1) {
ch = getc(fp);
if((ch == '"' || ch == EOF)&& cnt == 6) {
tmp[k]->next = NULL;
tmp[k]->subject_count = sub_no;
teach_list->teacher_count++;
if(teach_list->head) {
teach_list->tail->next = tmp[k];
teach_list->tail = tmp[k];
} else
teach_list->tail = teach_list->head = tmp[k];
k++;
if(k == 8)
node = (teacher_info **)realloc(node, 2 * k * sizeof(teacher_info *));
tmp[k] = (teacher_info *)malloc(sizeof(teacher_info));
cnt = sub_no = 0;
}
if(ch == EOF)
break;
if(ch == '"') {
cnt++;
flag = 0;
if(cnt % 2) {
flag = 1;
continue;
}
else {
token[i] = '\0';
if(cnt == 2)
strcpy(tmp[k]->teacher_name, token);
else if(cnt == 4)
strcpy(tmp[k]->teacher_ID, token);
else if(cnt == 6) {
j = work = 0;
while(token[j] != '\0') {
num = token[j] - '0';
work = 10 * work + num;
j++;
}
tmp[k]->workload = work;
}
i = 0;
continue;
}
}
if(flag && (cnt != 6)) {
token[i] = ch;
i++;
}
if(cnt == 6) {
if(ch == ' ' || ch == '\t' || ch == '\n') {
if(!spc_cnt) {
token[i] = '\0';
strcpy(tmp[k]->subject_ID[sub_no], token);
sub_no++;
i = 0;
}
spc_cnt++;
}else {
token[i] = ch;
i++;
spc_cnt = 0;
}
}
}
fclose(fp);
}
/*
* Stores the information about the standards in a link list of struct standard_info.
* It takes empty standard link list pointer as argument and creates list from input file standard_input.
*/
void input_standard(standard_list *std_list, char *std_ip) {
FILE *fp;
standard_info **node, *tmp[128];
int i = 0, j, flag = 0, cnt = 0, cred, num, k = 0, sub_no = 0, spc_cnt = 1, lab = 0, lab_no = 0;
char ch, token[32];
fp = fopen(std_ip, "r");
if(fp == NULL) {
perror("\n\n\n\n\t\t\tThere is some error while opening 'standard_input'\n\n\n\n");
exit (1);
}
node = (standard_info **)malloc(8 * sizeof(standard_info *));
tmp[0] = (standard_info *)malloc(sizeof(standard_info));
while(1) {
ch = getc(fp);
if((ch == '"' || ch == EOF)&& cnt == 2) {
tmp[k]->next = NULL;
tmp[k]->sem_credits = 0;
tmp[k]->subject_count = sub_no;
tmp[k]->lab_subject_count = lab_no;
if(std_list->head) {
std_list->tail->next = tmp[k];
std_list->tail = tmp[k];
std_list->standard_count++;
} else {
std_list->tail = std_list->head = tmp[k];
std_list->standard_count++;
}
k++;
if(k == 8)
node = (standard_info **)realloc(node, 2 * k * sizeof(standard_info *));
tmp[k] = (standard_info *)malloc(sizeof(standard_info));
cnt = sub_no = lab = lab_no = 0;
}
if(ch == EOF)
break;
if(ch == '"') {
cnt++;
flag = 0;
if(cnt % 2) {
flag = 1;
continue;
}
else {
token[i] = '\0';
if(cnt == 2)
strcpy(tmp[k]->standard_name, token);
i = 0;
continue;
}
}
if(flag && (cnt != 2)) {
token[i] = ch;
i++;
}
if(ch == '$') {
lab = 1;
continue;
}
if(cnt == 2) {
if(!lab) {
if(ch == ' ' || ch == '\t' || ch == '\n') {
if(!spc_cnt) {
token[i] = '\0';
strcpy(tmp[k]->subject_ID[sub_no], token);
tmp[k]->credits_allotted[sub_no] = 0;
sub_no++;
i = 0;
}
spc_cnt++;
}else {
token[i] = ch;
i++;
spc_cnt = 0;
}
}else if(lab) {
if(ch == ' ' || ch == '\t' || ch == '\n') {
if(!spc_cnt) {
token[i] = '\0';
strcpy(tmp[k]->lab_subject_ID[lab_no], token);
lab_no++;
i = 0;
}
spc_cnt++;
}else {
token[i] = ch;
i++;
spc_cnt = 0;
}
}
}
}
fclose(fp);
}
/*
* Assigns the teachers available for a particular subjects.
* The assigned value get stored in subjects' structure.
* It takes two list pointers theory subjects and teachers' list as arguments.
*/
void teach_sub(subject_list *th_sub_list, teacher_list *teach_list) {
int l;
subject_info *p = th_sub_list->head;
teacher_info *q = teach_list->head;
while(th_sub_list->head != NULL) {
th_sub_list->head->teacher_count = th_sub_list->head->free_teacher = 0;
th_sub_list->head = th_sub_list->head->next;
}
th_sub_list->head = p;
while(q != NULL) {
for(l = 0;l < q->subject_count;l++) {
th_sub_list->head = p;
while(th_sub_list->head != NULL) {
if(strcmp(q->subject_ID[l], th_sub_list->head->subject_ID) == 0)
strcpy(th_sub_list->head->teacher_ID[th_sub_list->head->teacher_count++], q->teacher_ID);
th_sub_list->head = th_sub_list->head->next;
}
}
q = q->next;
}
th_sub_list->head = p;
}
/*
* Assigns a teacher to each subject of each class randomly.
* It takes subjects' list and teachers' list as arguments.
*/
void assign_teacher2std(subject_list *sub_list, standard_list *std_list, teacher_list *teach_list) {
int l;
standard_info *p = std_list->head;
subject_info *q = sub_list->head;
teacher_info *r = teach_list->head;
while(std_list->head != NULL) {
for(l = 0;l < std_list->head->subject_count;l++) {
sub_list->head = q;
while(sub_list->head != NULL) {
if(strcmp(std_list->head->subject_ID[l], sub_list->head->subject_ID) == 0) {
std_list->head->sem_credits += sub_list->head->credits;
if(sub_list->head->free_teacher == sub_list->head->teacher_count)
sub_list->head->free_teacher = 0;
strcpy(std_list->head->teacher_ID[l], sub_list->head->teacher_ID[sub_list->head->free_teacher++]);
teach_list->head = r;
while(teach_list->head != NULL) {
if(strcmp(std_list->head->teacher_ID[l], teach_list->head->teacher_ID) == 0) {
teach_list->head->workload -= sub_list->head->credits;
}
teach_list->head = teach_list->head->next;
}
}
sub_list->head = sub_list->head->next;
}
}
std_list->head = std_list->head->next;
}
teach_list->head = r;
std_list->head = p;
sub_list->head = q;
}