rijads

workshop6

Dec 16th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include "kolekcija.h"
  3. #include "Student.h"
  4. using namespace std;
  5. void main(){
  6.     kolekcija<int> a;
  7.     student s(3020);
  8.     int unos;
  9.     do
  10.     {
  11.         cout << "unesi ocjenu: ";
  12.         cin >> unos;
  13.         if (unos > 0)
  14.         {
  15.             cin.ignore();
  16.             cout << "Unesite naziv predmeta (do 20 karaktera): ";
  17.             char nazivpr[20];
  18.             cin.getline(nazivpr, 19);
  19.             s.naziviPredmeta.Dodaj(nazivpr);
  20.             s.ocjene.Dodaj(unos);
  21.         }
  22.     } while (unos>0);
  23.     s.ispisOcjena();
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. #ifndef _KOLEKCIJA_H
  36. #define _KOLEKCIJA_H
  37.  
  38. #include <iostream>
  39. using namespace std;
  40. template<class T>
  41. class kolekcija{
  42.     T *sadrzaj;
  43.     int velicina;
  44.     int kapacitet;
  45. public:
  46.     kolekcija();
  47.     ~kolekcija();
  48.     void Dodaj (T element);
  49.     T Izbaci ();
  50.     T operator [](int index);
  51.     int GetVelicina();
  52. };
  53. template<class T>
  54. kolekcija<T>::kolekcija(){
  55.     velicina = 0;
  56.     kapacitet = 10;
  57.     sadrzaj = new T[kapacitet];
  58.  
  59. }
  60. template<class T>
  61. kolekcija<T>::~kolekcija(){
  62.     delete[]sadrzaj;
  63.     sadrzaj = NULL;
  64. }
  65. template<class T>
  66. void kolekcija<T>::Dodaj(T element){
  67.     if (velicina == kapacitet){
  68.         T *temp = new T[kapacitet + 10];
  69.         for (int i = 0; i < kapacitet; i++){
  70.             temp[i] = sadrzaj[i];
  71.         }
  72.         kapacitet += 10;
  73.         delete[]sadrzaj;
  74.         sadrzaj = temp;
  75.     }
  76.     sadrzaj[velicina] = element;
  77.     velicina++;
  78. }
  79. template<class T>
  80. T kolekcija<T>::Izbaci(){
  81.     if (velicina > 0){
  82.         T temp = sadrzaj[velicina - 1];
  83.         velicina--;
  84.         return temp;
  85.     }
  86.     else
  87.         return 0;
  88. }
  89. template<class T>
  90. T kolekcija<T>::operator[](int index){
  91.     if (index >= 0 && index < velicina){
  92.         return sadrzaj[index];
  93.     }
  94.     else
  95.         return 0;
  96. }
  97. template<class T>
  98. int kolekcija<T>::GetVelicina(){
  99.     return velicina;
  100. }
  101. #endif
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. #ifndef _STUDENT_H
  111. #define _STUDENT_H
  112. #include <iostream>
  113. #include "kolekcija.h"
  114. using namespace std;
  115. class student {
  116.     int brojIndeksa;
  117. public:
  118.     student(int a){
  119.         brojIndeksa = a;
  120.     }
  121.     int getBrojIneksa(){
  122.         return brojIndeksa;
  123.     }
  124.     kolekcija<int>ocjene;
  125.     kolekcija<char*> naziviPredmeta;
  126.     void ispisOcjena(){
  127.         cout << "Broj indeksa: " << brojIndeksa << "\n===================\n";
  128.         for (int i = 0; i < ocjene.GetVelicina(); i++){
  129.             cout << naziviPredmeta[i]<<": "<< ocjene[i] << endl;
  130.         }
  131.     }
  132. };
  133. #endif
Advertisement
Add Comment
Please, Sign In to add comment