-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
539 lines (449 loc) · 13.5 KB
/
Copy pathmain.cpp
File metadata and controls
539 lines (449 loc) · 13.5 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
typedef struct students{
string student_id;
string name;
string cpf;
struct students *next;
} student_regist;
typedef struct subjects{
string subj_id;
string name;
string teacher;
float credits;
struct subjects *next;
} subject_regist;
typedef struct periods{
float period;
vector<string> subjs_id;
struct periods *next;
} period_schedule;
typedef struct student_subj{
float period;
string student_id;
vector<string> subjs_id;
struct student_subj *next;
} period_class;
subjects *lista=NULL;
struct subjects *busca_subjects (string id, struct subjects *lista, struct subjects **ant)
{
struct subjects *aux=lista;
*ant = NULL;
while ( (aux != NULL) && (aux->subj_id != id))
{
*ant = aux;
aux = aux->next;
}
return aux;
}
int inserir_subjects (string id, string name, string teacher, float credits, struct subjects **lista){
struct subjects *aux=NULL;
struct subjects *ant=NULL;
aux = busca_subjects (id, *lista, &ant);
if (aux != NULL)
return 0;
else
{
aux = new subjects;
aux->subj_id = id;
aux->name = name;
aux->teacher = teacher;
aux->credits = credits;
aux->next = NULL;
if (ant == NULL)
*lista = aux;
else
ant->next = aux;
return 1;
}
}
void print_subjects (subjects *lista)
{
struct subjects *aux = lista;
while (aux != NULL)
{
cout << aux -> subj_id << endl << aux->name << endl << aux->teacher << endl << aux->credits << endl;
aux = aux->next;
}
cout << "\n";
}
int remove_subject (string id, struct subjects **lista)
{
struct subjects *aux = NULL;
struct subjects *ant = NULL;
aux = busca_subjects (id, *lista, &ant);
if (aux == NULL)
return 0;
else
{
if (ant == NULL)
{
*lista = aux->next;
delete (aux);
return 1;
}
else
{
ant->next = aux->next;
delete (aux);
return 1;
}
}
}
periods *listap=NULL;
struct periods *busca_periods (float period, struct periods *lista, struct periods **ant)
{
struct periods *aux=lista;
*ant = NULL;
while ( (aux != NULL) && (aux->period != period))
{
*ant = aux;
aux = aux->next;
}
return aux;
}
int inserir_periods (float period, vector<string> ids , struct periods **listap, subjects *lista){
for (string id : ids) {
subjects *ant;
subjects *subject_aux = busca_subjects(id, lista, &ant);
if (subject_aux == NULL) {
cout << "subject " << id << " not found. Please register it" << endl;
return 0;
}
}
struct periods *aux=NULL;
struct periods *ant=NULL;
aux = busca_periods (period, *listap, &ant);
if (aux != NULL)
return 0;
else
{
aux = new periods;
aux->period = period;
aux->subjs_id = ids;
aux->next = NULL;
if (ant == NULL)
*listap = aux;
else
ant->next = aux;
return 1;
}
}
void print_periods (periods *lista)
{
struct periods *aux = lista;
while (aux != NULL)
{
cout << aux->period << endl;
for (string id : aux->subjs_id) {
cout << id << " ";
}
cout << endl;
aux = aux->next;
}
cout << "\n";
}
int remove_period (float period, struct periods **listap)
{
struct periods *aux = NULL;
struct periods *ant = NULL;
aux = busca_periods (period, *listap, &ant);
if (aux == NULL)
return 0;
else
{
if (ant == NULL)
{
*listap = aux->next;
delete (aux);
return 1;
}
else
{
ant->next = aux->next;
delete (aux);
return 1;
}
}
}
void subjects_csv(subjects *lista) {
ofstream arquivo("subjects.csv");
if (arquivo.is_open()) {
subjects *aux = lista;
while (aux != NULL) {
arquivo << aux->subj_id << ",";
arquivo << aux->name << ",";
arquivo << aux->teacher << ",";
arquivo << aux->credits << endl;
aux = aux->next;
}
arquivo.close();
} else {
cout << "Erro ao abrir o arquivo 'assuntos.csv' para escrita." << endl;
}
}
void periods_csv(periods *listap) {
ofstream arquivo("periods.csv");
if (arquivo.is_open()) {
periods *aux = listap;
while (aux != NULL) {
arquivo << aux->period << ",";
for (size_t i = 0; i < aux->subjs_id.size(); ++i) {
arquivo << aux->subjs_id[i];
if (i != aux->subjs_id.size() - 1) {
arquivo << ",";
}
}
arquivo << endl;
aux = aux->next;
}
arquivo.close();
} else {
cout << "Erro ao abrir o arquivo 'periodos.csv' para escrita." << endl;
}
}
void register_student(student_regist** rptr){
//append a new student in the end of the struct
student_regist *new_regist;
students *it_ptr;
it_ptr = *rptr;
new_regist = new student_regist;
cout << "Please insert students credentials \nID: ";
cin >> (*new_regist).student_id;
cout << "\nName: ";
cin >> (*new_regist).name;
cout << "\nCPF: ";
cin >> (*new_regist).cpf;
while(it_ptr != NULL){
if ((*it_ptr).student_id == (*new_regist).student_id){
cout << "Student already registered with name " << (*it_ptr).name << " and CPF " << (*it_ptr).cpf;
return;
}
}
it_ptr = (**rptr).next;
if(*rptr == NULL){
// Empty list
*rptr = new_regist;
}
else{
while((*it_ptr).next != NULL){
it_ptr = (*it_ptr).next;
}
(*it_ptr).next = new_regist;
}
}
void remove_student(student_regist** rptr){
//remove student by its ID
string removed_id;
student_regist *it_ptr;
student_regist *bin;
it_ptr = (**rptr).next;
int removed = 0;
cout << "Please insert the ID you want to remove: ";
cin >> removed_id;
if((**rptr).student_id == removed_id){
*rptr = (**rptr).next;
}
else{
while((*it_ptr).next != NULL){
bin = (*it_ptr).next;
if((*bin).student_id == removed_id){
(*it_ptr).next = (*bin).next;
delete bin;
removed = 1;
}
it_ptr = (*it_ptr).next;
}
}
if (removed == 1){
cout << "Student removed";
}
else{
cout << "Student not found";
}
}
void search_std_subj(student_subj** rptr, subjects **sptr){
// search student's subject by its ID and its period
string search_std_id;
float search_period;
student_subj *it_ptr = *rptr;
subjects *sec_it_ptr = *sptr;
int is_found = 0;
cout << "Please insert the ID of the student: ";
cin >> search_std_id;
cout << "Please insert the period you wan't to search for: ";
cin >> search_period;
if (rptr == NULL){
// empty struct
cout << "Empty database";
return;
}
else{
while(it_ptr != NULL){
if (((*it_ptr).period == search_period) && ((*it_ptr).student_id == search_std_id)){
is_found = 1;
for(unsigned int i=0; i < (*it_ptr).subjs_id.size(); i++){
while(sec_it_ptr != NULL){
if ((*sec_it_ptr).subj_id == (*it_ptr).subjs_id[i]){
cout << (*sec_it_ptr).name << " -> " << (*sec_it_ptr).teacher << " -> " << (*sec_it_ptr).credits << "\n";
}
sec_it_ptr = (*sec_it_ptr).next;
}
sec_it_ptr = *sptr;
}
}
it_ptr = (*it_ptr).next;
}
if (is_found == 0){
cout << "Pair ID/period not found";
}
}
}
void search_subj_std(student_subj** rptr, students** sptr){
// search students doing a subject
float search_period;
string search_subj_id;
vector<string> student_list;
student_subj* it_ptr = *rptr;
students* sec_it_ptr = *sptr;
int is_found = 0;
cout << "Please insert the ID of the subject: ";
cin >> search_subj_id;
cout << "Please insert the period you wan't to search for: ";
cin >> search_period;
if (rptr == NULL){
// empty struct
cout << "Empty database";
return;
}
else{
while(it_ptr != NULL){
if (((*it_ptr).period == search_period) && (count((*it_ptr).subjs_id.begin(), (*it_ptr).subjs_id.end(), search_subj_id) >= 1)){
student_list.push_back((*it_ptr).student_id);
is_found = 1;
}
it_ptr = (*it_ptr).next;
}
for(unsigned int i=0; i < student_list.size(); i++){
while(sec_it_ptr != NULL){
if((*sec_it_ptr).student_id == student_list[i]){
cout << (*sec_it_ptr).name << " -> " << (*sec_it_ptr).cpf << "\n";
}
sec_it_ptr = (*sec_it_ptr).next;
}
sec_it_ptr = *sptr;
}
if (is_found == 0){
cout << "There's no student registered for this subject";
}
}
}
void register_new_std_class(student_subj** rptr, subjects** sptr, students** stdptr){
// register student's subject by period
student_subj new_regist;
student_subj* it_ptr = *rptr;
student_subj* last_el_ptr;
subjects* third_it_ptr = *sptr;
students* sec_it_ptr = *stdptr;
string subject;
int is_found = 0;
int is_std_regist = 0;
int is_subj_regist = 0;
cout << "Please insert the period you wan't to register: ";
cin >> new_regist.period;
cout << "Please insert the student's ID: ";
cin >> new_regist.student_id;
cout << "Please insert the subjects you wan't register and when finished type done: ";
while (cin >> subject && subject != "done") {
new_regist.subjs_id.push_back(subject);
}
// first check if the subject ids are registered in the subjects LE
// check it for the student id too
while(sec_it_ptr != NULL){
if ((*sec_it_ptr).student_id == new_regist.student_id){
is_std_regist = 1;
}
sec_it_ptr = (*sec_it_ptr).next;
}
if (is_std_regist != 1){
cout << "Student not found on the database. Please register it";
return;
}
for (unsigned int i=0; i < new_regist.subjs_id.size(); i++){
is_subj_regist = 0;
while(third_it_ptr != NULL){
if ((*third_it_ptr).subj_id == new_regist.subjs_id[i]){
is_subj_regist = 1;
}
third_it_ptr = (*third_it_ptr).next;
}
if (is_subj_regist == 0){
cout << "The following subject ID wasn't found: " << new_regist.subjs_id[i];
}
}
if (is_subj_regist == 0){
return;
}
// search for the period/std ID pair and append the current subject list if found
// else just add the struct in the end of the database
while(it_ptr != NULL){
if (((*it_ptr).period == new_regist.period) && ((*it_ptr).student_id) == new_regist.student_id){
(*it_ptr).subjs_id.insert((*it_ptr).subjs_id.end(), new_regist.subjs_id.begin(), new_regist.subjs_id.end());
is_found = 1;
}
last_el_ptr = it_ptr;
it_ptr = (*it_ptr).next;
}
if (is_found == 0){
(*last_el_ptr).next = &new_regist;
}
}
void remove_std_class(student_subj** rptr){
string removed_std_id;
float removed_std_period;
student_subj* it_ptr = *rptr;
student_subj* last_el = NULL;
cout << "Please insert the ID you want to remove: ";
cin >> removed_std_id;
cout << "Please insert the period associated: ";
cin >> removed_std_period;
if(((**rptr).period == removed_std_period) && ((**rptr).student_id == removed_std_id)){
*rptr = (**rptr).next;
}
else{
while(it_ptr != NULL){
if (((*it_ptr).student_id == removed_std_id) && ((*it_ptr).period == removed_std_period)){
(*last_el).next = (*it_ptr).next;
}
else{
last_el = it_ptr;
}
it_ptr = (*it_ptr).next;
}
}
}
int main(){
/*
inserir_subjects ("2411","Matematica discreta", "Veloso", 100, &lista);
inserir_subjects ("2412","Sitemas Digitais", "Rotava", 100, &lista);
inserir_subjects ("2321","Calculo 3", "Lima Vaz", 100, &lista);
print_subjects(lista);
inserir_periods (24.1,{"2411", "2412"} ,&listap, lista);
inserir_periods (23.2,{"2321"} ,&listap, lista);
print_periods(listap);
periods_csv(listap);
subjects_csv(lista);
*/
int is_running = 1;
int action;
while(is_running == 1){
cout << "Type the number in front of each option to choose an action:\n1)Register new student\n2)Register new subject\n3)Register new period\n4)Register student's subject\n5)Remove student\n6)Remove student's subjects\n7)Remove period\n8)Remove subject\n";
cin >> action;
cout << "Type 1 to keep using the aplications. Type 0 otherwise";
cin >> is_running;
}
}