Sunteți pe pagina 1din 16

#include <iostream>

#include <string>
#include <fstream>
using namespace std;

class null_exception : public exception


{
public:
null_exception()
{

null_exception(const char* message) : exception(message)


{

}
};

class student
{
private:
const int id;
int* note;
int nrNote;
char* grupa;
int anulNasterii = 2002;
static string universitate;

public:
int matricol;
string nume;

student() : id(1)
{
nume = "Anonim";
matricol = 0;
grupa = nullptr;
note = nullptr;
nrNote = 0;
}

student(string nume, int matricol) : id(2)


{
this->nume = nume;
this->matricol = matricol;
grupa = nullptr;
note = nullptr;
nrNote = 0;
}

student(int id, string nume, int matricol, int* note, int nrNote) : id(id)
{
this->nume = nume;
if (matricol > 0)
{
this->matricol = matricol;
}
else
{
this->matricol = 0;
}
grupa = nullptr;
if (note != nullptr && nrNote > 0)
{
this->note = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
this->note[i] = note[i];
}
this->nrNote = nrNote;
}
else
{
this->note = nullptr;
this->nrNote = 0;
}
}

student(const student& s) : id(s.id)


{
nume = s.nume;
matricol = s.matricol;
if (s.grupa != nullptr)
{
int len = strlen(s.grupa) + 1;
grupa = new char[len];
strcpy_s(grupa, len, s.grupa);
}
else
{
grupa = nullptr;
}
anulNasterii = s.anulNasterii;
if (s.note != nullptr && s.nrNote > 0)
{
nrNote = s.nrNote;
note = new int[s.nrNote];
for (int i = 0; i < s.nrNote; i++)
{
note[i] = s.note[i];
}
}
else
{
note = nullptr;
nrNote = 0;
}
}

virtual ~student()
{
if (note != nullptr)
{
delete[] note;
}
if (grupa != nullptr)
{
delete[] grupa;
}
}

student& operator=(const student& s)


{
if (note != nullptr)
{
delete[] note;
}

nume = s.nume;
matricol = s.matricol;

delete[] grupa;
if (s.grupa != nullptr)
{
int len = strlen(s.grupa) + 1;
grupa = new char[len];
strcpy_s(grupa, len, s.grupa);
}
else
{
grupa = nullptr;
}
anulNasterii = s.anulNasterii;
if (s.note != nullptr && s.nrNote > 0)
{
nrNote = s.nrNote;
note = new int[s.nrNote];
for (int i = 0; i < s.nrNote; i++)
{
note[i] = s.note[i];
}
}
else
{
note = nullptr;
nrNote = 0;
}

return *this;
}

void serializare()
{
ofstream f("student.bin", ios::binary);
int length = nume.length() + 1;
f.write((char*)&length, sizeof(length));
f.write(nume.c_str(), length);
f.write((char*)&matricol, sizeof(matricol));
f.write((char*)&anulNasterii, sizeof(anulNasterii));
f.write(grupa, (long long)strlen(grupa) + 1);
f.write((char*)&nrNote, sizeof(nrNote));
for (int i = 0; i < nrNote; i++)
{
f.write((char*)&note[i], sizeof(note[i]));
}
f.close();
}

void deserializare()
{
ifstream f("student.bin", ios::binary);
int length = 0;
f.read((char*)&length, sizeof(length));
char* aux = new char[length];
f.read(aux, length);
nume = aux;
f.read((char*)&matricol, sizeof(matricol));
f.read((char*)&anulNasterii, sizeof(anulNasterii));
string buffer = "";
char c = 0;
while ((c = f.get()) != 0)
{
buffer += c;
}
delete[] grupa;
grupa = new char[buffer.length() + 1];
strcpy_s(grupa, buffer.length() + 1, buffer.c_str());
f.read((char*)&nrNote, sizeof(nrNote));
delete[] note;
note = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
f.read((char*)&note[i], sizeof(note[i]));
}
f.close();
}

virtual float punctajRepartizareTabara()


{
float medie = 0;
if (nrNote > 0 && note != nullptr)
{
for (int i = 0; i < nrNote; i++)
{
medie += note[i];
}
medie /= nrNote;
}
return medie;
}

void metoda()
{
cout << "student" << endl;
}

void schimba_grupa(const char* noua_grupa)


{
if (noua_grupa != nullptr)
{
if (grupa != nullptr)
{
delete[] grupa;
}
grupa = new char[strlen(noua_grupa) + 1];
strcpy_s(grupa, strlen(noua_grupa) + 1, noua_grupa);
}
else
{
throw null_exception("grupa null");
}
}

int getAnulNasterii()
{
return anulNasterii;
}

void setAnulNasterii(int anulNasterii)


{
if (anulNasterii <= 2002 && anulNasterii >= 1920)
{
this->anulNasterii = anulNasterii;
}
}

string getNume();

void setNume(string nume)


{
if (nume.length() > 0)
{
this->nume = nume;
}
}

int* getNote()
{
if (note != nullptr)
{
int* copie = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
copie[i] = note[i];
}
return copie;
}
else
{
return nullptr;
}
}

int getNrNote()
{
return nrNote;
}

void setNote(int* note, int nrNote)


{
if (this->note != nullptr)
{
delete[] this->note;
}
if (note != nullptr && nrNote > 0)
{
this->nrNote = nrNote;
this->note = new int[nrNote];
for (int i = 0; i < nrNote; i++)
{
this->note[i] = note[i];
}
}
else
{
this->note = nullptr;
this->nrNote = 0;
}
}

static string getUniversitate()


{
return universitate;
}

static void setUniversitate(string universitate)


{
student::universitate = universitate;
}

static float medieSerie(student* studenti, int nrStudenti)


{
float suma = 0;
int count = 0;
for (int i = 0; i < nrStudenti; i++)
{
if (studenti[i].note != nullptr)
{
for (int j = 0; j < studenti[i].nrNote; j++)
{
suma += studenti[i].note[j];
count++;
}
}
}
return (count > 0) ? suma / count : 0;
}

bool operator!()
{
return nrNote > 0;
}

student operator++()
{
this->anulNasterii++;
return *this;
}

student operator++(int i)
{
student copie = *this;
this->anulNasterii++;
return copie;
}

student operator+(int nrAni)


{
if (nrAni < 0)
{
throw 500;
}
student copie = *this;
copie.anulNasterii += nrAni;
return copie;
}

int& operator[](int index) throw (exception)


{
if (index >= 0 && index < nrNote && note != nullptr)
{
return note[index];
}
else
{
throw exception("index invalid!");
}
}

explicit operator int()


{
return anulNasterii;
}

string operator()()
{
return nume;
}

friend student operator+(int, student);


friend ostream& operator<<(ostream&, student);
friend istream& operator>>(istream&, student&);
friend ofstream& operator<<(ofstream&, student);
};

string student::universitate = "ASE";

string student::getNume()
{
return nume;
}

student operator+(int nrAni, student s)


{
s.anulNasterii += nrAni;
return s;
}

ostream& operator<<(ostream& out, student s)


{
out << "nume: " << s.nume << endl;
out << "matricol: " << s.matricol << endl;
if (s.grupa != nullptr)
{
out << "grupa: " << s.grupa << endl;
}
out << "anul nasterii: " << s.anulNasterii << endl;
out << "numar note: " << s.nrNote << endl;
out << "note: ";
if (s.note != nullptr)
{
for (int i = 0; i < s.nrNote; i++)
{
out << s.note[i] << " ";
}
}
return out;
}

ofstream& operator<<(ofstream& g, student s)


{
if (g.is_open())
{
g << s.nume << endl;
g << s.matricol << endl;
if (s.grupa != nullptr)
{
g << s.grupa << endl;
}
else
{
g << "N/A" << endl;
}
g << s.anulNasterii << endl;
g << s.nrNote << endl;
if (s.note != nullptr)
{
for (int i = 0; i < s.nrNote; i++)
{
g << s.note[i] << " ";
}
}
}
return g;
}

istream& operator>> (istream& in, student& s)


{
cout << "Nume: ";
in >> ws;
getline(in, s.nume);
cout << "Matricol: ";
in >> s.matricol;
cout << "Grupa: ";
delete[] s.grupa;

char buffer[100];
//in.clear();
//in.ignore(2);
in >> ws;
in.getline(buffer, 99);
s.grupa = new char[strlen(buffer) + 1];
strcpy_s(s.grupa, strlen(buffer) + 1, buffer);
cout << "Anul nasterii: ";
in >> s.anulNasterii;
if (s.note != nullptr)
{
delete[] s.note;
}
cout << "Numar note: ";
in >> s.nrNote;
if (s.nrNote > 0)
{
s.note = new int[s.nrNote];
for (int i = 0; i < s.nrNote; i++)
{
cout << "nota[" << i << "] = ";
in >> s.note[i];
}
}
else
{
s.nrNote = 0;
s.note = nullptr;
}
return in;
}

class voluntar
{
private:
int nrZile;
int* oreZilnice;

public:
voluntar()
{
nrZile = 0;
oreZilnice = nullptr;
}

voluntar(int nrZile, int* oreZilnice)


{
if (nrZile > 0 && oreZilnice != nullptr)
{
this->nrZile = nrZile;
this->oreZilnice = new int[nrZile];
for (int i = 0; i < nrZile; i++)
{
this->oreZilnice[i] = oreZilnice[i];
}
}
else
{
this->nrZile = 0;
this->oreZilnice = nullptr;
}
}

~voluntar()
{
delete[] oreZilnice;
}

voluntar(const voluntar& v)
{
if (v.nrZile > 0 && v.oreZilnice != nullptr)
{
nrZile = v.nrZile;
oreZilnice = new int[nrZile];
for (int i = 0; i < nrZile; i++)
{
oreZilnice[i] = v.oreZilnice[i];
}
}
else
{
nrZile = 0;
oreZilnice = nullptr;
}
}

voluntar& operator=(const voluntar& v)


{
if (this != &v)
{
delete[] oreZilnice;
if (v.nrZile > 0 && v.oreZilnice != nullptr)
{
nrZile = v.nrZile;
oreZilnice = new int[nrZile];
for (int i = 0; i < nrZile; i++)
{
oreZilnice[i] = v.oreZilnice[i];
}
}
else
{
nrZile = 0;
oreZilnice = nullptr;
}
}
return *this;
}

void metoda()
{
cout << "voluntar" << endl;
}
};

class student_voluntar : public student, public voluntar


{
private:
char* organizatie;

public:
student_voluntar()
{
organizatie = nullptr;
}

student_voluntar(int id, string nume, int matricol, int* note,


int nrNote, const char* organizatie) : student(id, nume, matricol,
note, nrNote)
{
if (organizatie != nullptr)
{
this->organizatie = new char[strlen(organizatie) + 1];
strcpy_s(this->organizatie, strlen(organizatie) + 1,
organizatie);
}
else
{
this->organizatie = nullptr;
}
}

student_voluntar(string nume, int matricol, int nrZile, int* oreZilnice,


const char* organizatie) :
voluntar(nrZile, oreZilnice), student(nume, matricol)
{
if (organizatie != nullptr)
{
this->organizatie = new char[strlen(organizatie) + 1];
strcpy_s(this->organizatie, strlen(organizatie) + 1,
organizatie);
}
else
{
this->organizatie = nullptr;
}
}

student_voluntar(const student_voluntar& s) : student(s), voluntar(s)


{
if (s.organizatie != nullptr)
{
this->organizatie = new char[strlen(s.organizatie) + 1];
strcpy_s(this->organizatie, strlen(s.organizatie) + 1,
s.organizatie);
}
else
{
this->organizatie = nullptr;
}
}

~student_voluntar()
{
if (organizatie != nullptr)
{
delete[] organizatie;
}
}

student_voluntar& operator=(const student_voluntar& s)


{
if (this != &s)
{
student::operator=(s);
voluntar::operator=(s);
if (organizatie != nullptr)
{
delete[] organizatie;
}
if (s.organizatie != nullptr)
{
this->organizatie = new char[strlen(s.organizatie) + 1];
strcpy_s(this->organizatie, strlen(s.organizatie) + 1,
s.organizatie);
}
else
{
this->organizatie = nullptr;
}
}
return *this;
}

void metoda()
{
student::metoda();
voluntar::metoda();
}

float punctajRepartizareTabara() override


{
float medie = student::punctajRepartizareTabara();
if (medie > 9)
{
return 10;
}
else
{
return medie + 1;
}
}

friend ostream& operator<<(ostream&, student_voluntar);


friend istream& operator>>(istream&, student_voluntar&);
friend ofstream& operator<<(ofstream&, student_voluntar);
friend ifstream& operator>>(ifstream&, student_voluntar&);
};

ostream& operator<<(ostream& out, student_voluntar s)


{
out << (student)s;
cout << endl;
if (s.organizatie != nullptr)
{
cout << "Organizatie: " << s.organizatie << endl;
}
return out;
}

ofstream& operator<<(ofstream& f, student_voluntar s)


{
if (f.is_open())
{
f << (student)s;
f << endl;
if (s.organizatie != nullptr)
{
f << s.organizatie << endl;
}
else
{
f << "N/A" << endl;
}
}
return f;
}

istream& operator>>(istream& in, student_voluntar& s)


{
in >> (student&)s;
if (s.organizatie != nullptr)
{
delete[] s.organizatie;
}
cout << "Organizatie: ";
string buffer;
in >> ws;
getline(in, buffer);
s.organizatie = new char[buffer.length() + 1];
strcpy_s(s.organizatie, buffer.length() + 1, buffer.c_str());

return in;
}

ifstream& operator>>(ifstream& g, student_voluntar& s)


{
if (g.is_open())
{
g >> (student&)s;
if (s.organizatie != nullptr)
{
delete[] s.organizatie;
}
string buffer;
g >> ws;
getline(g, buffer);
s.organizatie = new char[buffer.length() + 1];
strcpy_s(s.organizatie, buffer.length() + 1, buffer.c_str());
}
return g;
}

int main()
{
int note[] = { 10, 8, 9 };
student s1;
s1.setNume("Ionel Popescu");
cout << s1.getNume() << endl;
s1.schimba_grupa("1133");
student* ps = &s1;
cout << (*ps).getNume() << endl;
ps->matricol = 123;
cout << ps->matricol << endl;

cout << sizeof(s1) << endl;


s1.setNote(note, 3);
student s2 = s1;

student* ps2 = new student();


ps2->nume = "Marcel Ionescu";
cout << ps2->nume << endl;
delete ps2;
ps = nullptr;

student s3(55, "Vasile Petrescu", 123, note, 3);


cout << s3.matricol << endl;
cout << s3.getAnulNasterii() << endl;
s3.setAnulNasterii(1992);
cout << s3.getAnulNasterii() << endl;

s3.setNume("Adrian Pop");
cout << s3.getNume() << endl;
note[1] = 12;

int vector[] = { 10, 9, 10, 9 };


s3.setNote(vector, 4);

int* x = s3.getNote();

if (x != nullptr)
{
for (int i = 0; i < s3.getNrNote(); i++)
{
cout << x[i] << " ";
}
}
cout << endl;
student s4;
s4 = s3;
student::setUniversitate("Politehnica");
student studenti[] = { s1, s3 };
float result = student::medieSerie(studenti, 2);
cout << result << endl;
if (!s4)
{
cout << "are note" << endl;
}
else
{
cout << "nu are note" << endl;
}

student s5 = ++s4;
student s6 = s4++;
student s7 = s4 + 3;
student s8 = 4 + s4;
cout << s5;
student s9;
//cin >> s9;
//cout << s9;

cout << s5[1];


s5[1] = 5;
cout << s5;

cout << endl;


int an = (int)s5;
cout << an << endl;
cout << s5() << endl;

student s10;
student_voluntar sv1;
s10 = sv1;
student* pstud = &sv1;

student_voluntar sv2(55, "Ionel", 123, vector, 4, "SiSC");


student_voluntar sv3(sv2);
sv1 = sv3;

cout << sv3;


student_voluntar sv4;
//cin >> sv4;
//cout << sv4;

try
{
sv3[0];
sv3 + 2;
sv3.schimba_grupa(nullptr);
}
catch (null_exception& e)
{
cout << "NULL -> " << e.what();
}
catch (exception& e)
{
cout << e.what();
}
catch (int x)
{
cout << "cod eroare: " << x;
}
catch (...)
{
cout << "alta eroare";
}

ofstream f;
f.open("student.txt");
f << s4;
f.close();

ifstream g("student.txt");
student s12;
g >> s12;
g.close();

//ofstream h("student_voluntar.txt", ios::app|ios::_Nocreate);


ofstream h("student_voluntar.txt");
h << sv2 << sv3;
h.close();

ifstream k("student_voluntar.txt");
student_voluntar sv5, sv6;
k >> sv5 >> sv6;
k.close();

s4.schimba_grupa("1133");
s4.serializare();
student s14;
s14.deserializare();

fstream q("student.bin", ios::_Nocreate | ios::binary | ios::in);


int size = sizeof(int);
cout << "deplasament in fisier: " << q.tellg() << endl;
q.seekg(-size, ios::end);
cout << "deplasament in fisier: " << q.tellg() << endl;
int ultimaNota = 0;
q.read((char*)&ultimaNota, size);
q.close();
cout << "Ultima nota: " << ultimaNota << endl;

voluntar v1;
int ore[] = { 5, 3, 2, 1 };
voluntar v2(5, ore);
voluntar v3 = v2;
v1 = v2;

sv6.student::metoda();
sv6.metoda();

cout << s4.punctajRepartizareTabara() << endl;


cout << sv2.punctajRepartizareTabara() << endl;

student* stud = &sv2;


cout << stud->punctajRepartizareTabara() << endl;

student* grupa[3];
grupa[0] = new student(5, "Gigel", 123, vector, 4);
grupa[1] = new student_voluntar(6, "Ionel", 456, vector, 4, "SiSC");
grupa[2] = new student(7, "Vasile", 458, vector, 4);

cout << endl;


for (int i = 0; i < 3; i++)
{
cout << grupa[i]->punctajRepartizareTabara() << endl;
}

delete grupa[0];
delete grupa[1];
delete grupa[2];
}

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