Repository navigation
Expand file tree
/
Copy pathStudent.cpp
More file actions
838 lines (728 loc) · 22.6 KB
/
Copy pathStudent.cpp
File metadata and controls
838 lines (728 loc) · 22.6 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
// 2 - 9
/*
Да се имплементира класа Trud во која се чуваат информации за: (5 поени)
вид на труд (еден знак и тоа C за конференциски труд, J за труд во списание)
година на издавање (цел број).
Да се имплементира класа Student во која се чува: (5 поени)
името на студентот (низа од најмногу 30 карактери)
индекс (цел број)
година на упис (цел број)
листа на оцени од положени предмети (низа од цели броеви)
број на положени предмети (цел број)
За оваа класа да се имплементираат следните методи:
функција rang() што пресметува просек од положените испити на студентот (5 поени)
оператор << за печатење на студентот во формат: (5 поени)
Индекс Име Година на упис ранг
Да се имплементира класа PhDStudent во која покрај основните информации за студентот дополнително се чува: (5 поени)
листа од објавени трудови (динамички резервирана низа од објекти од класата Trud)
бројот на трудови (цел број)
Во оваа класа да се препокрие соодветно функцијата rang() така што на просекот од положените испити ќе се додаде и збирот од поените од објавените трудови на PhD студентот. Во зависност од видот на трудот, секој универзитет има посебен начин на бодување на трудовите. Начинот на бодување е ист за сите PhD студенти. Иницијално да се смета дека конференциски труд се бодува со 1 поен, а труд во списание со 3 поени. Универзитетот има можност да ги менува вредностите на бодовите. (5 поени + 5 поени)
За оваа класа да се обезбеди:
оператор += за додавање нов објект од класата Trud во листата (5 поени). Ако се направи обид да се внесе труд што е издаден порано од годината на упис на студентот да се фрли исклучок (објект од класата Exception). Справувањето со исклучокот треба да се реализира во главната функција main каде што е потребно, но и во конструктор ако е потребно. Ако бил генериран исклучок треба да се отпечати соодветна порака за грешка "Ne moze da se vnese dadeniot trud", а новиот труд нема да се внесе во листата на трудови од студентот. (10 поени)
Напомена: Сите променливи на класите се чуваат како приватни.
Да се обезбедат сите потребни функции за правилно функционирање на програмата. (5 поени)
For example:
Input Result
2
3
1
PhDStudent_1
111222
2012
5
10 10 10 9 10
1
j
2015
1
PhDStudent_2
123456
2003
5
10 9 10 8 10
3
c
2003
j
2003
j
2005
0
Student
12234
2010
3
10 10 10
123456
C
2004
===== Testiranje na operatorot += ======
Lista na site studenti:
111222 PhDStudent_1 2012 12.8
123456 PhDStudent_2 2003 16.4
12234 Student 2010 10
Lista na site studenti:
111222 PhDStudent_1 2012 12.8
123456 PhDStudent_2 2003 17.4
12234 Student 2010 10
int main(){
int testCase;
cin >> testCase;
int god, indeks, n, god_tr, m, n_tr;
int izbor; //0 za Student, 1 za PhDStudent
char ime[30];
int oceni[50];
char tip;
Trud trud[50];
if (testCase == 1){
cout << "===== Testiranje na klasite ======" << endl;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
Student s(ime, indeks, god, oceni, n);
cout << s;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
PhDStudent phd(ime, indeks, god, oceni, n, trud, n_tr);
cout << phd;
}
if (testCase == 2){
cout << "===== Testiranje na operatorot += ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
// dodavanje nov trud za PhD student spored indeks
Trud t;
cin >> indeks;
cin >> t;
// vmetnete go kodot za dodavanje nov trud so pomos na operatorot +=
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
if (testCase == 3){
cout << "===== Testiranje na operatorot += ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
// dodavanje nov trud za PhD student spored indeks
Trud t;
cin >> indeks;
cin >> t;
// vmetnete go kodot za dodavanje nov trud so pomos na operatorot += od Testcase 2
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
if (testCase == 4){
cout << "===== Testiranje na isklucoci ======" << endl;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
PhDStudent phd(ime, indeks, god, oceni, n, trud, n_tr);
cout << phd;
}
if (testCase == 5){
cout << "===== Testiranje na isklucoci ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
// dodavanje nov trud za PhD student spored indeks
Trud t;
cin >> indeks;
cin >> t;
// vmetnete go kodot za dodavanje nov trud so pomos na operatorot += i spravete se so isklucokot
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
if (testCase == 6){
cout << "===== Testiranje na static clenovi ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
int conf, journal;
cin >> conf;
cin >> journal;
//postavete gi soodvetnite vrednosti za statickite promenlivi
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
return 0;
}
*/
#include <iostream>
#include <cstring>
using namespace std;
class Exception {
public:
static void message() {
cout << "Ne moze da se vnese dadeniot trud\n";
}
};
class Trud {
char type;
int year;
void copy_trud(const Trud& other) {
this->type = other.type;
this->year = other.year;
}
public:
Trud() {
this->type = 'A';
this->year = 0;
}
Trud(char type, int year) {
this->type = type;
this->year = year;
}
Trud(const Trud& other) {
copy_trud(other);
}
Trud& operator=(const Trud& other) {
if(this == &other) {
return *this;
}
copy_trud(other);
return *this;
}
friend istream& operator>>(istream& in, Trud& t) {
in >> t.type >> t.year;
return in;
}
char getType() {
return type;
}
int getYear() {
return year;
}
~Trud() { }
};
class Student {
protected:
char name[30];
int index;
int inYear;
int* grades;
int n;
void copy_student(const Student& other) {
strcpy(this->name, other.name);
this->index = other.index;
this->inYear = other.inYear;
this->grades = new int[other.n];
for(int i = 0; i < other.n; i++) this->grades[i] = other.grades[i];
this->n = other.n;
}
public:
Student() {
strcpy(this->name, "name");
this->index = this->inYear = this->n = 0;
this->grades = nullptr;
}
Student(char* name, int index, int inYear, int* grades, int n) {
strcpy(this->name, name);
this->index = index;
this->inYear = inYear;
this->grades = new int[n];
for(int i = 0; i < n; i++) {
this->grades[i] = grades[i];
}
this->n = n;
}
Student(const Student& other) {
copy_student(other);
}
Student& operator=(const Student& other) {
if(this == &other) {
return *this;
}
delete [] grades;
copy_student(other);
return *this;
}
friend ostream& operator<<(ostream& out, Student& other) {
out << other.index << " " << other.name << " " << other.inYear << " " << other.rang() << "\n";
return out;
}
int getIndex() {
return index;
}
virtual double rang() {
double s = 0;
for(int i = 0; i < n; i++) {
s += grades[i];
}
return s / n;
}
~Student() {
delete [] grades;
}
};
class PhDStudent : public Student {
Trud* trudovi;
int t_n;
static int c;
static int j;
void copy_phdstudent(const PhDStudent& other) {
this->trudovi = new Trud[other.t_n];
for(int i = 0; i < t_n; i++) {
this->trudovi[i] = other.trudovi[i];
}
this->t_n = other.t_n;
}
public:
PhDStudent() : Student() {
this->trudovi = nullptr;
this->t_n = 0;
}
PhDStudent(char* name, int index, int inYear, int* grades, int n, Trud* t, int t_n) : Student(name, index, inYear, grades, n) {
this->trudovi = new Trud[t_n];
for(int i = 0; i < t_n; i++) {
try {
t[i].getYear() < inYear ? throw Exception() : this->trudovi[i] = t[i];
} catch(Exception& e) {
e.message();
}
}
this->t_n = t_n;
}
PhDStudent(const PhDStudent& other) : Student(other) {
copy_phdstudent(other);
}
PhDStudent& operator=(const PhDStudent& other) {
if(this == &other) {
return *this;
}
Student::operator=(other);
delete [] trudovi;
copy_phdstudent(other);
return *this;
}
PhDStudent& operator+=(Trud& trud) {
if(trud.getYear() < inYear) {
throw Exception();
}
Trud* tmp = new Trud[t_n + 1];
for(int i = 0; i < t_n; i++) {
tmp[i] = trudovi[i];
}
tmp[t_n++] = trud;
delete [] trudovi;
this->trudovi = new Trud[t_n];
for(int i = 0; i < t_n; i++) {
trudovi[i] = tmp[i];
}
delete [] tmp;
return *this;
}
static void setC(int _c) {
c = _c;
}
static void setJ(int _j) {
j = _j;
}
double rang() {
double curr = Student::rang();
for(int i = 0; i < t_n; i++) {
if(tolower(trudovi[i].getType()) == 'c') {
curr += c;
} else if(tolower(trudovi[i].getType()) == 'j') {
curr += j;
}
}
return curr;
}
~PhDStudent() {
delete [] trudovi;
}
};
int PhDStudent::c = 1;
int PhDStudent::j = 3;
int main(){
int testCase;
cin >> testCase;
int god, indeks, n, god_tr, m, n_tr;
int izbor; //0 za Student, 1 za PhDStudent
char ime[30];
int oceni[50];
char tip;
Trud trud[50];
if (testCase == 1){
cout << "===== Testiranje na klasite ======" << endl;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
Student s(ime, indeks, god, oceni, n);
cout << s;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
PhDStudent phd(ime, indeks, god, oceni, n, trud, n_tr);
cout << phd;
}
if (testCase == 2){
cout << "===== Testiranje na operatorot += ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
// dodavanje nov trud za PhD student spored indeks
Trud t;
cin >> indeks;
cin >> t;
// vmetnete go kodot za dodavanje nov trud so pomos na operatorot +=
bool found = false;
try {
for(int i = 0; i < m; i++) {
if(niza[i]->getIndex() == indeks && dynamic_cast<PhDStudent*>(niza[i])) {
*dynamic_cast<PhDStudent*>(niza[i]) += t;
found = true;
}
}
} catch(Exception& e) {
e.message();
}
if(!found) cout << "Ne postoi PhD student so indeks " << indeks << "\n";
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
if (testCase == 3){
cout << "===== Testiranje na operatorot += ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
// dodavanje nov trud za PhD student spored indeks
Trud t;
cin >> indeks;
cin >> t;
// vmetnete go kodot za dodavanje nov trud so pomos na operatorot += od Testcase 2
try {
for(int i = 0; i < m; i++) {
if(niza[i]->getIndex() == indeks && dynamic_cast<PhDStudent*>(niza[i])) {
*dynamic_cast<PhDStudent*>(niza[i]) += t;
}
}
} catch(Exception& e) {
e.message();
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
if (testCase == 4){
cout << "===== Testiranje na isklucoci ======" << endl;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
PhDStudent phd(ime, indeks, god, oceni, n, trud, n_tr);
cout << phd;
}
if (testCase == 5){
cout << "===== Testiranje na isklucoci ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i<m; i++){
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0){
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else{
cin >> n_tr;
for (int j = 0; j < n_tr; j++){
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
// dodavanje nov trud za PhD student spored indeks
Trud t;
cin >> indeks;
cin >> t;
// vmetnete go kodot za dodavanje nov trud so pomos na operatorot += i spravete se so isklucokot
try {
for(int i = 0; i < m; i++) {
if(niza[i]->getIndex() == indeks && dynamic_cast<PhDStudent*>(niza[i])) {
*dynamic_cast<PhDStudent*>(niza[i]) += t;
}
}
} catch(Exception& e) {
e.message();
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
if (testCase == 6)
{
cout << "===== Testiranje na static clenovi ======" << endl;
Student **niza;
cin >> m;
niza = new Student *[m];
for (int i = 0; i < m; i++)
{
cin >> izbor;
cin >> ime;
cin >> indeks;
cin >> god;
cin >> n;
for (int j = 0; j < n; j++)
cin >> oceni[j];
if (izbor == 0)
{
niza[i] = new Student(ime, indeks, god, oceni, n);
}
else
{
cin >> n_tr;
for (int j = 0; j < n_tr; j++)
{
cin >> tip;
cin >> god_tr;
Trud t(tip, god_tr);
trud[j] = t;
}
niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
}
}
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
int conf, journal;
cin >> conf;
cin >> journal;
//postavete gi soodvetnite vrednosti za statickite promenlivi
PhDStudent::setC(conf);
PhDStudent::setJ(journal);
// pecatenje na site studenti
cout << "\nLista na site studenti:\n";
for (int i = 0; i < m; i++)
cout << *niza[i];
}
return 0;
}