You are on page 1of 9

#include<iostream>

#include<string>
#include <list>
#include <map>
using namespace std;

// p1 (2,3); p2( 1,6);


class Pereche {

int valoare;
int frecventa;
public:

Pereche() {
this->frecventa = 0;
this->valoare = 0;
}

Pereche(int val, int frv) {


if (val >= 0) {
this->frecventa = frv;
}
else {
this->frecventa = 0;
}
this->valoare = val;
}

int getFrecventa() {
return this->frecventa;
}

int getValoare() {
return this->valoare;
}

};

class Serie {

string serie;//
int nrPerechi;
Pereche * perechi;// vector dinamic de obiecte

public:

Serie() {
this->serie = "na";
this->nrPerechi = 0;
this->perechi = NULL;
}

//Serie s1 ( "Note studenti", -3, vectorPerechi);


Serie( string numeSerie, int nrPerechi, Pereche * perechi ) {
if (numeSerie.length() > 1) {
this->serie = numeSerie;
}
else {
this->serie = "na";
}

if (nrPerechi > 0) {
this->nrPerechi = nrPerechi;
this->perechi = new Pereche[nrPerechi];// 2 12 1 18 3 15
for (int i = 0; i < nrPerechi; i++) {
this->perechi[i] = perechi[i];
}
}
else {
this->nrPerechi = 0;
this->perechi = NULL;
}

this->sortare();
}

Serie(const Serie& sursa) {


this->serie = sursa.serie;
this->nrPerechi = sursa.nrPerechi;
this->perechi = new Pereche[sursa.nrPerechi];// 2 12 1 18 3 15
for (int i = 0; i < sursa.nrPerechi; i++) {
this->perechi[i] = sursa.perechi[i];
}
}

Serie& operator= (const Serie& sursa) {

if (this->perechi) {
delete[] this->perechi;
}

this->serie = sursa.serie;
this->nrPerechi = sursa.nrPerechi;
this->perechi = new Pereche[sursa.nrPerechi];// 2 12 1 18 3 15
for (int i = 0; i < sursa.nrPerechi; i++) {
this->perechi[i] = sursa.perechi[i];
}
return *this;
}

~Serie() {
if (this->perechi) {
delete[] this->perechi;
}

void sortare() {
// 0 1 2
// p2 p1 p3
// 1 2 3
// aux =2 p1
for (int i = 0; i < this->nrPerechi; i++) {
for (int j = i + 1; j < this->nrPerechi; j++) {
if (this->perechi[i].getValoare() > this-
>perechi[j].getValoare() ) {
Pereche aux;
aux = this->perechi[i];
this->perechi[i] = this->perechi[j];
this->perechi[j] = aux;
}

// 2 2 3 2 2.5
// 4 6
float getMedie() {

float s = 0;
int n = 0;

if (this->nrPerechi > 0) {
for (int i = 0; i < this->nrPerechi; i++) {
s += this->perechi[i].getValoare() * this-
>perechi[i].getFrecventa();
}

for (int i = 0; i < this->nrPerechi; i++) {


n += this->perechi[i].getFrecventa();
}

s /= n;
}

return s;

}
Serie & operator+= (Serie serie) {

// // s1 ( advn )
//// s2 p2 p3

// s1 += s2;

// //s1 <= v (advn)---> p1 p2 p3 p2 p3


Pereche * v = new Pereche[this->nrPerechi + serie.nrPerechi];
for (int i = 0; i < this->nrPerechi; i++) {
v[i] = this->perechi[i];
}
for (int i = 0; i < serie.nrPerechi; i++) {
v[i + this->nrPerechi] = serie.perechi[i];
}

if (this->perechi) {
delete[] this->perechi;
}
this->perechi = v;
this->nrPerechi += serie.nrPerechi;
this->sortare();
return *this;
}

//cout<<s1;
// friend

friend ostream & operator<< (ostream& o, Serie sursa);

// s1 += p2; // p1 p2 p3 [p4 ]
Serie& operator+= (Pereche perecheNoua) {
Pereche *v = new Pereche[this->nrPerechi + 1];
for (int i = 0; i < this->nrPerechi; i++) {
v[i] = this->perechi[i];
}
v[this->nrPerechi] = perecheNoua;

if (this->perechi) {
delete[] this->perechi;
}
this->perechi = v;
this->nrPerechi++;
this->sortare();

return *this;
}

Serie & operator -= (Pereche scoasa) {


Pereche * v = NULL;
int poz = 0;
bool ok = false;

for (int i = 0; i < this->nrPerechi; i++) {


if (this->perechi[i].getValoare() == scoasa.getValoare()) {
ok = true;
poz = i;
break;
}
}

if (ok == true) {
v = new Pereche[this->nrPerechi - 1];
int k = 0;
for (int i = 0; i < this->nrPerechi; i++) {
if (poz != i) {
v[k++] = this->perechi[i];
}
}
}

if (this->perechi) {
delete[] this->perechi;
}
this->perechi = v;
this->nrPerechi -= 1;

return *this;
}

virtual void f() {


cout << "Serie" << endl;
}

};

class SerieSpeciala : public Serie {

int x;

public:

void f() {
cout << "Serie speciala" << endl;
}

};

ostream & operator<< (ostream& o, Serie sursa)


{

o << "Nume serie " << sursa.serie << endl;


o << "Nr perechi " << sursa.nrPerechi << endl;
for (int i = 0; i < sursa.nrPerechi; i++) {
o << "Perechea " << i + 1 << ": " << sursa.perechi[i].getValoare() <<
"-" << sursa.perechi[i].getFrecventa() << endl;
}

return o;
}

class Serie1 {

map < int, int > mapa; // Pereche * perechi int nrPerechi

public:

Serie1() {

Serie1(map < int, int > mapa)


{

this->mapa = mapa;

friend ostream& operator << (ostream& o, Serie1 sursa) {

map < int, int > ::iterator it1;


for (it1 = sursa.mapa.begin(); it1 != sursa.mapa.end(); it1++) {

cout << it1->first << "--" << it1->second << endl;


}

return o;
}

float medie() {

float s = 0;
int n = 0;
map < int, int > ::iterator it1;

for (it1 = this->mapa.begin(); it1 != this->mapa.end(); it1++) {

s += it1->first * it1->second;
}
for (it1 = this->mapa.begin(); it1 != this->mapa.end(); it1++) {

n += it1->second;
}

return s / n;

Serie1 & operator+= (pair <int, int > p) {

this->mapa.insert(p);
return *this;
}

//se1+=se2;
Serie1 & operator+= ( Serie1 serie) {

map < int, int > ::iterator it1;

for (it1 = serie.mapa.begin(); it1 != serie.mapa.end(); it1++) {


this->mapa.insert(*it1);
}

return *this;
}

};

int main() {

Pereche p1;
Pereche p2(2, 12);
Pereche p3(1, 18);
Pereche p4(3, 15);

Serie s;
Pereche vectorPerechi[3] = { p2,p3,p4 };
Serie s1 ("Note studenti", 3, vectorPerechi);
Serie s2(s1);

Serie * sp = new Serie();


delete sp;

s = s2= s1;
// cout << s1.getMedie() << endl;

//s1 p1 p2 p3
//s2 p2 p3

s1 +=s2;
s1 += p2;
s1 -= p2;
s1 -= p2;
s1 -= p2;

// cout << s1;

//s1 => p1 p2 p3 p2 p3

list< Pereche > serie;


serie.push_back(p2);//
serie.push_front(p1);
serie.push_front(p3);

list<Pereche>::iterator it;
for (it = serie.begin(); it != serie.end(); it++) {

// cout << (*it).getValoare() << " - " << (*it).getFrecventa() <<


endl;;
}

// 2, p2 (2 12 )
// 3, p3( 3,4);

map < int, Pereche > mapa;


mapa.insert(pair <int, Pereche >(p2.getValoare(), p2));
mapa.insert(pair <int, Pereche >(p3.getValoare(), p3));
mapa.insert(pair <int, Pereche >(p1.getValoare(), p1));

map < int, Pereche > ::iterator it1;

for (it1 = mapa.begin(); it1 != mapa.end(); it1++) {

// cout << it1->first << "--" << it1->second.getFrecventa() << endl;


}

map < int, int > mapa1;


mapa1.insert(pair <int, int >(3, 2));
mapa1.insert(pair <int, int >(1 ,2));
mapa1.insert(pair <int, int >(2, 2));

map < int, int > mapa2;


mapa1.insert(pair <int, int >(5, 2));
mapa1.insert(pair <int, int >(6, 2));
mapa1.insert(pair <int, int >(7, 2));
// (1,12) (2,11) (3,34)

Serie1 se1 (mapa1) ;


Serie1 se2(mapa2);

se1 += pair<int, int>(0,2);

se1 += se2;

// cout << se1 ;

Serie * vs[2] = { new SerieSpeciala(), &s1 };

for (int i = 0; i < 2; i++) {


vs[i]->f();
}

return 0;
}

You might also like