Sunteți pe pagina 1din 7

#include<iostream>

#include<string>
#include<fstream>
#include<vector>
using namespace std;
class Examen
{
private:
const int idExamen;
protected:
string denumire;
int nota;
string data;
int subiecte;
float* punctajPeSubiect;
public:
static int contor;
Examen() :idExamen(contor++)
{
this->denumire = "";
this->nota = 1;
this->data = "01/06/2020";
this->subiecte = 0;
this->punctajPeSubiect = NULL;
}
Examen(string denumire, int nota, string data, int subiecte, float*
punctajPeSubiect) :idExamen(contor++)
{
if (denumire.size() > 2)
{
this->denumire = denumire;
}
if (nota < 1)
this->nota = 1;
if (nota > 10)
this->nota = 10;
else
this->nota = nota;
if (data.size() == 8)
this->data = data;
this->subiecte = subiecte;
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < this->subiecte; i++)
{
this->punctajPeSubiect[i] = punctajPeSubiect[i];
}

}
Examen(const Examen& e) :idExamen(e.idExamen)
{
if (e.denumire.size() > 2)
{
this->denumire = e.denumire;
}
if (e.nota < 1)
this->nota = 1;
if (e.nota > 10)
this->nota = 10;
else
this->nota = e.nota;
if (e.data.size() == 8)
this->data = e.data;
this->subiecte = e.subiecte;
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < this->subiecte; i++)
{
this->punctajPeSubiect[i] = e.punctajPeSubiect[i];
}
}
Examen& operator =(const Examen& e)
{
if (this != &e)//evitarea auto-asignarii
{
if (this->punctajPeSubiect != NULL)
delete[]this->punctajPeSubiect;
if (e.denumire.size() > 2)
{
this->denumire = e.denumire;
}
if (e.nota < 1)
this->nota = 1;
if (e.nota > 10)
this->nota = 10;
else
this->nota = e.nota;
if (e.data.size() == 8)
this->data = e.data;
this->subiecte = e.subiecte;
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < this->subiecte; i++)
{
this->punctajPeSubiect[i] = e.punctajPeSubiect[i];
}
return *this;
}
}
~Examen()
{
if (this->punctajPeSubiect != NULL)
delete[]this->punctajPeSubiect;
cout << "S-a apelat destructorul" << endl;
}
friend ostream& operator<<(ostream& out, const Examen& e)
{
out << "Examenul "<<e.idExamen << " la disciplina " << e.denumire << "
a avut nota " << e.nota << ", in data de " << e.data << " Punctajele sunt: ";
for (int i = 0; i < e.subiecte; i++)
{
out << e.punctajPeSubiect[i] << " ";
}
out << endl;
return out;
}
explicit operator string()
{
return this->denumire;
}
bool operator >(int prag)
{
return this->nota > prag;
}
string getDenumire()
{
return this->denumire;
}
int getNrSubiecte()
{
return this->subiecte;
}
float totalPunctaj()
{
float total = 0;
for (int i = 0; i < this->subiecte; i++)
{
total = total + punctajPeSubiect[i];
}
return total;
}
float SubiectCuPunctajulCelMaiMare()
{
float maxim = this->punctajPeSubiect[0];
for (int i = 0; i < this->subiecte; i++)
{
if (maxim < this->punctajPeSubiect[i])
maxim = this->punctajPeSubiect[i];
}
return maxim;
}
void salveaza_in_fisier(ofstream& out)
{

int dimensiuneDenumire = denumire.size();


out.write((char*)&dimensiuneDenumire, sizeof(int));
out.write(denumire.c_str(), dimensiuneDenumire + 1);
out.write((char*)&nota, sizeof(int));
int dimensiuneData = data.size();
out.write((char*)&dimensiuneData, sizeof(int));
out.write(data.c_str(), dimensiuneData + 1);
out.write((char*)&subiecte, sizeof(int));
for (int i = 0; i < this->subiecte; i++)
out.write((char*)&punctajPeSubiect[i], sizeof(int));

}
void restaureaza_din_fisier(ifstream& in)
{

if (punctajPeSubiect != NULL)
delete[]punctajPeSubiect;

int dimensiuneDenumire = 0;
in.read((char*)&dimensiuneDenumire, sizeof(int));

char* aux = new char[dimensiuneDenumire + 1];


in.read(aux, dimensiuneDenumire + 1);
denumire = aux;
in.read((char*)&nota, sizeof(int));

int dimData = 0;
in.read((char*)&dimData, sizeof(int));
char* aux_data = new char[dimData + 1];
in.read(aux_data, dimData + 1);
data = aux_data;

in.read((char*)&subiecte, sizeof(int));
this->punctajPeSubiect = new float[this->subiecte];
for (int i = 0; i < subiecte; i++)
{
in.read((char*)&punctajPeSubiect[i], sizeof(int));
}

}
};

int Examen::contor = 1;
class Student
{
string nume;
Examen** vectorExamene;
int nrExamene;

public:
Student()
{
this->nume = "";
this->vectorExamene = NULL;// Examen* joaca rolul lui int => Examen**
ar fi un int*
this->nrExamene = 0;
}
Student(string nume, int nrExamene, Examen** vectorExamene)
{
this->nume = nume;
this->nrExamene = nrExamene;
this->vectorExamene = new Examen * [this->nrExamene];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = vectorExamene[i];
}
}
Student(const Student& s)
{
this->nume = s.nume;
this->nrExamene = s.nrExamene;
this->vectorExamene = new Examen * [this->nrExamene];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = s.vectorExamene[i];
}
}
Student& operator =(const Student& s)
{

if (this->vectorExamene != NULL)
delete[] this->vectorExamene;
this->nume = s.nume;
this->nrExamene = s.nrExamene;
this->vectorExamene = new Examen * [this->nrExamene];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = s.vectorExamene[i];
}
return *this;
}
friend ostream& operator<<(ostream& out, Student& s)
{
out << s.nume << endl;
out << s.nrExamene << endl;
for (int i = 0; i < s.nrExamene; i++)
{
out << *s.vectorExamene[i] << endl;
}
return out;
}
int getnrExamene()
{
return this->nrExamene;
}

Examen** getvectorExamene()
{
return this->vectorExamene;
}
~Student()
{
if (this->vectorExamene != NULL)
delete[] this->vectorExamene;
}
Student& operator +=(Examen& e)
{
Student temp = *this;
this->vectorExamene = new Examen * [this->nrExamene + 1];
for (int i = 0; i < this->nrExamene; i++)
{
this->vectorExamene[i] = temp.vectorExamene[i];
}
this->vectorExamene[this->nrExamene] = &e;
this->nrExamene++;
return *this;
}
Student& operator -=(Examen& e)
{
Student temp = *this;
this->vectorExamene = new Examen * [this->nrExamene];
int i = 0;
int j = 0;
while ((i < this->nrExamene) && (j < this->nrExamene))
{
if (temp.vectorExamene[i]->getDenumire() != e.getDenumire())
{
this->vectorExamene[j] = temp.vectorExamene[i];
j++;
}
i++;
}
this->nrExamene = j;
return *this;

}
};
void main()
{
float punctaj[] = { 1.5,3,3.5,2 };
Examen e1;
cout << e1 << endl;
Examen e2("POO", 9, "18/06/2020", 4, punctaj);
Examen e12("SDD", 9, "18/06/2020", 4, punctaj);
Examen e13("PAW", 9, "18/06/2020", 4, punctaj);
Examen e14("PEAG", 9, "18/06/2020", 4, punctaj);
cout << e2 << endl;
Examen e3 = e2;
cout << e3 << endl;
e1 = e2;
cout << e1 << endl;
string numeExamen = (string)e2;
cout << numeExamen << endl;
if (e2 > 5)
{
cout << "Studentul a promovat examenul!" << endl;
}
else
{
cout << "Studentul nu a promovat examenul!" << endl;
}
cout << e2.getDenumire() << endl;
cout << e2.getNrSubiecte() << endl;
cout << e2.totalPunctaj() << endl;
cout << e2.SubiectCuPunctajulCelMaiMare() << endl;
string fisierBinar;
cout << "Introduceti nuemele fisierului binar: ";
cin >> fisierBinar;
fisierBinar = fisierBinar + ".bin";
ofstream outBinaryProiect(fisierBinar, ios::out | ios::binary);
if (outBinaryProiect.is_open()) {
e2.salveaza_in_fisier(outBinaryProiect);
cout << "e2 a fost scris in fisierul binar.\n";
outBinaryProiect.close();
}
cout << "\n\nCitire si afisare din fisier binar: \n";
Examen e6;
cout << e6 << endl;
ifstream inBinaryProiect(fisierBinar, ios::in | ios::binary);
if (inBinaryProiect.is_open()) {
e6.restaureaza_din_fisier(inBinaryProiect);
cout << "\n\ne6 citit din fisierul binar: \n" << e6 << endl;
inBinaryProiect.close();
}

cout << "\n\n Student\n\n";


Examen e7;
Examen** lista = new Examen * [3];
lista[0] = &e14;
lista[1] = &e12;
lista[2] = &e13;
Student s1;
Student s2("Andrei", 3, lista);
Student s3 = s2;
cout << "Constructor de copiere: " << s3 << endl;
s1 = s2;
cout << "Op= : " << s1 << endl;
cout << s2 << endl;
s2 += e6;
cout << s2 << endl;
s2 -= e7;
cout << s2 << endl;
s2 -= e14;
cout << s2 << endl;

cout << "\n\n STL \n\n";


vector<Examen*> vectExamen;
vectExamen.push_back(&e12);
vectExamen.push_back(&e13);
vectExamen.push_back(&e14);
vector<Examen*>::iterator it;
for (it = vectExamen.begin(); it != vectExamen.end(); it++)
{
cout << **it << endl;
}
}

S-ar putea să vă placă și