Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DINSTRING:
- #ifndef DSTRING_HPP
- #define DSTRING_HPP
- #include <iostream>
- using namespace std;
- class DinString {
- private:
- int duzina;
- char *text;
- public:
- DinString(){ duzina=0; text=NULL; }
- DinString(const char[]);
- DinString(const DinString&);
- ~DinString() { delete[] text; }
- char& operator [](int);
- char operator [](int) const;
- DinString& operator =(const DinString&);
- DinString& operator +=(const DinString&);
- friend bool operator ==(const DinString &,const DinString&);
- friend bool operator !=(const DinString &,const DinString&);
- friend DinString operator +(const DinString &,const DinString&);
- friend ostream& operator <<(ostream &out, const DinString &s);
- int length()const{ return duzina; }
- };
- #endif
- // LIST:
- #ifndef LIST_DEF
- #define LIST_DEF
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- template <class T>
- class List{
- private:
- struct listEl{
- T content;
- struct listEl *next;
- };
- listEl *head;
- listEl *tail;
- int noEl;
- public:
- List(){
- head=tail=NULL;
- noEl=0;
- }
- List(const List<T>&);
- List<T>& operator=(const List<T>&);
- virtual ~List();
- int getH() const { return (int)head; }
- int getT() const { return (int)tail; }
- int size() const {return noEl;}
- bool empty() const {return head==NULL?1:0;}
- bool add(int, const T&);
- bool remove(int);
- bool read(int, T&)const;
- void clear();
- };
- template <class T>
- ostream& operator<<(ostream & out, const List<T> &l){
- out<<endl;
- out<<"--------"<<endl;
- for(int i=1;i<=l.size();i++){
- if(i!=1) out<<", ";
- T res;
- l.read(i,res);
- out<<res;
- }
- out<<endl<<"--------"<<endl;
- return out;
- }
- template <class T>
- List<T>::List(const List<T> &l){
- head=NULL;
- tail=NULL;
- noEl=0;
- for(int i=1;i<=l.noEl;i++){
- T res;
- if(l.read(i,res))
- add(i,res);
- }
- }
- template <class T>
- List<T>& List<T>::operator=(const List<T> &l){
- if(this!=&l){
- clear();
- head=NULL;
- tail=NULL;
- noEl=0;
- for(int i=1;i<=l.noEl;i++){
- T res;
- if(l.read(i,res))
- add(i,res);
- }
- }
- return *this;
- }
- template <class T>
- List<T>::~List(){
- while(!empty())
- remove(1);
- }
- template <class T>
- bool List<T>::add(int n, const T& newContent){
- if(n<1 || n>noEl+1)
- return false;
- else{
- listEl *newEl=new listEl;
- if(newEl==NULL)
- return false;
- else{
- newEl->content=newContent;
- if(n==1){
- newEl->next=head;
- head=newEl;
- }else if(n==noEl+1){
- newEl->next=NULL;
- tail->next=newEl;
- }else{
- listEl *temp=head;
- for(int i=2;i<n;i++)
- temp=temp->next;
- newEl->next=temp->next;
- temp->next=newEl;
- }
- noEl++;
- if(newEl->next==NULL)
- tail=newEl;
- return true;
- }
- }
- }
- template <class T>
- bool List<T>::remove(int n){
- if(n<1 || n>noEl)
- return false;
- else{
- if(n==1){
- listEl *del=head;
- head=head->next;
- if(tail==del)
- tail=NULL;
- delete del;
- noEl--;
- } else {
- listEl *temp=head;
- for(int i=2;i<n;i++)
- temp=temp->next;
- listEl *del=temp->next;
- temp->next=del->next;
- if(tail==del)
- tail=temp;
- delete del;
- noEl--;
- }
- return true;
- }
- }
- template <class T>
- bool List<T>::read(int n,T& retVal)const{
- if(n<1 || n>noEl)
- return false;
- else{
- if(n==1)
- retVal=head->content;
- else if(n==noEl)
- retVal=tail->content;
- else{
- listEl *temp=head;
- for(int i=1;i<n;i++)
- temp=temp->next;
- retVal=temp->content;
- }
- return true;
- }
- }
- template <class T>
- void List<T>::clear(){
- while(!empty())
- remove(1);
- }
- #endif
- // SPISAK:
- #ifndef SPISAK_HPP_INCLUDED
- #define SPISAK_HPP_INCLUDED
- #include "student.hpp"
- #include "list.hpp"
- class SpisakStudenata
- {
- private:
- DinString nazivPredmeta;
- DinString datumPolaganja;
- List<Student> lista;
- public:
- SpisakStudenata() {}
- void setNaziv(const DinString &ds)
- {
- nazivPredmeta = ds;
- }
- void setDatumPolaganja(const DinString &ds)
- {
- datumPolaganja = ds;
- }
- void insertStudent();
- void sortPoOceni();
- void sortPoIndeksu();
- void printSpisak() const;
- };
- #endif // SPISAK_HPP_INCLUDED
- // STUDENT:
- #ifndef STUDENT_HPP_INCLUDED
- #define STUDENT_HPP_INCLUDED
- #include "dinstring.hpp"
- class Student
- {
- protected:
- int brojIndeksa;
- DinString ime;
- DinString prezime;
- int ocena;
- public:
- Student (int i=0, const char *s1 = "", const char *s2 = "", int oc = 5) : brojIndeksa(i), ime(s1), prezime(s2), ocena(oc) {}
- Student (int i, const DinString &ds1, const DinString &ds2, int oc) : brojIndeksa(i), ime(ds1), prezime(ds2), ocena(oc) {}
- Student (const Student &s) : brojIndeksa(s.brojIndeksa), ime(s.ime), prezime(s.prezime), ocena(s.ocena) {}
- ~Student() {}
- void setBrojIndeksa(int bri)
- {
- brojIndeksa = bri;
- }
- void setIme(const DinString &ds)
- {
- ime = ds;
- }
- void setPrezime(const DinString &ds)
- {
- prezime = ds;
- }
- void setOcena(int x)
- {
- ocena = x;
- }
- int getBrojIndeksa() const
- {
- return brojIndeksa;
- }
- int getOcena() const
- {
- return ocena;
- }
- Student& operator=(const Student&);
- friend ostream& operator<<(ostream&, const Student&);
- };
- #endif // STUDENT_HPP_INCLUDED
- // DINSTRING.CPp
- #include "dinstring.hpp"
- DinString::DinString(const char ulazniStr[]) {
- duzina=0;
- while(ulazniStr[duzina]!='\0'){
- duzina++;
- }
- text=new char[duzina+1];
- for (int i=0;i<duzina;i++)
- text[i]=ulazniStr[i];
- text[duzina]='\0';
- }
- DinString::DinString(const DinString& ds) {
- duzina=ds.duzina;
- text=new char[duzina+1];
- for (int i=0;i<duzina;i++)
- text[i]=ds.text[i];
- text[duzina]='\0';
- }
- char& DinString::operator [](int i) { return text[i]; }
- char DinString::operator [](int i) const { return text[i]; }
- DinString& DinString:: operator =(const DinString& ds) {
- if (this!=&ds){
- delete[] text;
- duzina=ds.duzina;
- text=new char[duzina+1];
- for (int i=0;i<duzina;i++)
- text[i]=ds.text[i];
- text[duzina]='\0';
- }
- return *this;
- }
- DinString& DinString::operator +=(const DinString& ds) {
- int i;
- char *tempText=new char[duzina+ds.duzina+1];
- for (i=0;i<duzina;i++)
- tempText[i]=text[i];
- for (i=0;i<ds.duzina;i++)
- tempText[duzina+i]=ds.text[i];
- tempText[duzina+ds.duzina]='\0';
- duzina+=ds.duzina;
- delete []text;
- text=tempText;
- return *this;
- }
- bool operator ==(const DinString& ds1,const DinString& ds2) {
- if (ds1.duzina!=ds2.duzina)
- return false;
- for (int i=0;i<ds1.duzina;i++)
- if (ds1.text[i]!=ds2.text[i]) return false;
- return true;
- }
- bool operator !=(const DinString& ds1,const DinString& ds2) {
- if (ds1.duzina!=ds2.duzina)
- return true;
- for (int i=0;i<ds1.duzina;i++)
- if (ds1.text[i]!=ds2.text[i]) return true;
- return false;
- }
- DinString operator +(const DinString& ds1,const DinString& ds2) {
- DinString temp;
- temp.duzina=ds1.duzina+ds2.duzina;
- temp.text=new char[temp.duzina+1];
- int i;
- for(i=0;i<ds1.duzina;i++)
- temp.text[i]=ds1.text[i];
- for(int j=0;j<ds2.duzina;j++)
- temp.text[i+j]=ds2.text[j];
- temp.text[temp.duzina]='\0';
- return temp;
- }
- ostream& operator <<(ostream &out, const DinString &s) {
- if(s.duzina>0)
- out<<s.text;
- return out;
- }
- // MAIN.CPP
- #include "spisak.hpp"
- int main()
- {
- char odg = 'd';
- char s[100];
- SpisakStudenata spisak;
- cout << endl << endl << endl;
- cout << "Unesite datum polaganja ispita: ";
- cin >> s;
- spisak.setDatumPolaganja(s);
- cout << "Unesite naziv ispita: ";
- cin >> s;
- spisak.setNaziv(s);
- cout << "Unesite podatke o studentima..." << endl;
- while ((odg == 'd') || (odg = 'D'))
- {
- spisak.insertStudent();
- cout << "Da li zelite jos da unosite podatke? [D/N] ";
- cin >> odg;
- }
- cout << "Da li da spisak bude sortiran po ocenama? [D/N] ";
- cin >> odg;
- if ((odg = 'd') || (odg = 'D'))
- spisak.sortPoOceni();
- else
- {
- cout << "Da li da spisak bude sortiran po broju indeksa? [D/N] ";
- cin >> odg;
- if ((odg == 'd') || (odg = 'D'))
- spisak.sortPoIndeksu();
- }
- cout << endl << endl << endl;
- spisak.printSpisak();
- return 0;
- }
- // SPISAK.CPP
- #include "spisak.hpp"
- void SpisakStudenata :: insertStudent()
- {
- Student st;
- int x;
- char s[100];
- cout << "Unesite broj indeksa: ";
- cin >> x;
- st.setBrojIndeksa(x);
- cout << "Unesite ime studenta: ";
- cin >> s;
- st.setIme(s);
- cout << "Unesite prezime studenta: ";
- cin >> s;
- st.setPrezime(s);
- cout << "Unesite ocenu: ";
- cin >> x;
- st.setOcena(x);
- cout << "*****************************************************" << endl;
- cout << st << endl;
- cout << "*****************************************************" << endl;
- lista.add(1, st);
- }
- void SpisakStudenata :: sortPoOceni()
- {
- Student s1, s2;
- for (int i = 1; i<=lista.size()-1; i++)
- for (int j=i+1; j<=lista.size(); j++)
- {
- lista.read(i, s1);
- lista.read(j, s2);
- if (s1.getOcena() < s2.getOcena())
- {
- lista.remove(i);
- lista.add(i, s2);
- lista.remove(j);
- lista.add(j, s1);
- }
- }
- }
- void SpisakStudenata :: sortPoIndeksu()
- {
- Student s1, s2;
- for (int i=1; i<=lista.size()-1; i++)
- for (int j=i+1; j<=lista.size(); j++)
- {
- lista.read(i, s1);
- lista.read(j, s2);
- if (s1.getBrojIndeksa() < s2.getBrojIndeksa())
- {
- lista.remove(i);
- lista.add(i, s2);
- lista.remove(j);
- lista.add(j, s1);
- }
- }
- }
- void SpisakStudenata :: printSpisak() const
- {
- cout << "Datum: " << datumPolaganja << endl;
- cout << "Predmet: " << nazivPredmeta << endl;
- cout << "---------------------------- REZULTATI ISPITA ----------------------------" << endl;
- Student st;
- for (int i=1; i<=lista.size(); i++)
- {
- lista.read(i, st);
- cout << st << endl;
- }
- cout << "--------------------------------------------------------------------------" << endl;
- }
- // STUDENT.CPP
- #include "student.hpp"
- Student& Student :: operator=(const Student &s)
- {
- brojIndeksa = s.brojIndeksa;
- ime = s.ime;
- prezime = s.prezime;
- ocena = s.ocena;
- return *this;
- }
- ostream& operator<<(ostream &out, const Student &s)
- {
- out << "\t" << s.brojIndeksa;
- out << "\t" << s.ime;
- out << "\t" << s.prezime;
- out << "\t" << s.ocena;
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement