Skip to content

Instantly share code, notes, and snippets.

@denisachicarosa
Created March 18, 2018 12:21
Show Gist options
  • Save denisachicarosa/0bd7103e02c8ec680b92d9b7784dd4b6 to your computer and use it in GitHub Desktop.
Save denisachicarosa/0bd7103e02c8ec680b92d9b7784dd4b6 to your computer and use it in GitHub Desktop.
Poo Version number 165468413
#include <iostream>
using namespace std;
class Vector
{
int nr_elem;
int *v;
public:
friend class Vectori_de_vectori;
Vector();
Vector(int, int);
Vector(int nr);
Vector(const Vector&);
~Vector();
void afisare();
void actualizare(int, int);
void operator=(const Vector);
friend istream& operator>> (istream&, Vector&);
friend ostream& operator<< (ostream&, Vector&);
};
class Vectori_de_vectori
{
int dim;
Vector *V;
public:
friend class altaclasa;
Vectori_de_vectori(int x=0, int d=0);
~Vectori_de_vectori();
Vectori_de_vectori(const Vectori_de_vectori&);
friend istream& operator>>(istream&, Vectori_de_vectori&);
friend ostream& operator<<(ostream&, Vectori_de_vectori&);
void citire();
void afisare();
int** operator+(Vectori_de_vectori&);
void dezaloca_v(int,Vectori_de_vectori& );
int maxim_vectori();
int** creez_matrice();
int get_dim();
};
int** Vectori_de_vectori:: operator+(Vectori_de_vectori& a )
{
int i, nr_ln, nr_cl,j;
int** A;
int** B;
A=a.creez_matrice();
B=this->creez_matrice();
int **C;
nr_ln=max(a.dim,dim);
nr_cl=max(a.maxim_vectori(),this->maxim_vectori());
C=new int* [nr_ln];
for(i=0;i<nr_ln;i++)
{
C[i]=new int[nr_cl];
for(j=0;j<nr_cl;j++) C[i][j]=0;
}
for(i=0;i<dim;i++)
for(j=0;j<this->maxim_vectori();j++) C[i][j]=B[i][j];
for(i=0;i<a.dim;i++)
for(j=0;j<a.maxim_vectori();j++) C[i][j]=C[i][j]+A[i][j];
return C;
}
int Vectori_de_vectori::get_dim()
{
return dim;
}
int** Vectori_de_vectori::creez_matrice()
{
int i,j,nr_coloane;
nr_coloane=maxim_vectori();
int **A;
A=new int* [dim];
for(i=0;i<dim;i++)
{
A[i]=new int[nr_coloane];
for(j=0;j<nr_coloane;j++) A[i][j]=0;
}
for(i=0;i<dim;i++)
for(j=0;j<this->V[i].nr_elem;j++) A[i][j]=V[i].v[j];
return A;
}
int Vectori_de_vectori::maxim_vectori()
{
int max=0,i,j;
for(i=0;i<dim;i++)
if(max<this->V[i].nr_elem) max=this->V[i].nr_elem;
return max;
}
ostream& operator<<(ostream& out, Vectori_de_vectori& b)
{
int i;
for(i=0;i<b.dim;i++)
cout<<b.V[i];
return out;
}
void Vectori_de_vectori::dezaloca_v(int i, Vectori_de_vectori& a)
{
delete a.V[i].v;
}
istream& operator>>(istream& in, Vectori_de_vectori& a)
{
for(int i=0;i<a.dim;i++)
a.dezaloca_v(i,a);
cout<<"Numarul de vectori este ";
in>>a.dim;
int i;
for(i=0;i<a.dim;i++)
cin>>a.V[i];
return in;
}
Vectori_de_vectori::Vectori_de_vectori(const Vectori_de_vectori& b)
{
int i;
dim=b.dim;
V=new Vector [dim];
for(i=0;i<dim;i++)
V[i]=b.V[i];
}
void Vectori_de_vectori::afisare()
{
int i;
for(i=0;i<dim;i++)
cout<<V[i];
}
void Vectori_de_vectori::citire()
{
cout<<"Numarul de vectori ";
cin>>dim;
int i;
for(i=0;i<this->dim;i++)
cin>>V[i];
}
Vectori_de_vectori::~Vectori_de_vectori()
{
for(int i=0;i<this->dim;i++)
V[i].~Vector();
delete V;
}
Vector::Vector()
{
nr_elem=0;
v=NULL;
}
Vectori_de_vectori::Vectori_de_vectori(int x, int d)
{
dim=d;
V=new Vector[dim];
for(int i=0;i<dim;i++)
{ delete[] V[i].v;
cout<<"Nr_elem= ";
cin>>V[i].nr_elem;
V[i].v=new int [V[i].nr_elem];
for(int j=0;j<V[i].nr_elem;j++)
V[i].v[j]=x;
}
}
ostream& operator<<(ostream& out, Vector&a)
{
int i;
for(int i=0;i<a.nr_elem;i++)
out<<a.v[i]<<" ";
out<<endl;
return out;
}
istream& operator>>(istream& in, Vector& a)
{
int nr;
cout<<"Numarul de coloane este ";
in>>nr;
delete[] a.v;
a.nr_elem=nr;
a.v=new int [a.nr_elem];
for(int i=0;i<a.nr_elem;i++)
{ cout<<"Elementul este: "; in>>a.v[i];}
return in;
}
void Vector::afisare()
{
int i;
for(i=0;i<nr_elem;i++) cout<<v[i]<<" ";
}
void Vector::actualizare(int x, int nr)
{
delete[] v;
nr_elem=nr;
v=new int [nr];
int i;
for(i=0;i<nr;i++)
v[i]=x;
}
void Vector::operator=(const Vector a)
{
delete[] v;
this->nr_elem=a.nr_elem;
v=new int [nr_elem];
int i;
for(i=0;i<nr_elem;i++) v[i]=a.v[i];
}
Vector::~Vector()
{
delete[] v;
nr_elem=0;
}
Vector::Vector(int nr)
{
nr_elem=nr;
v=new int[this->nr_elem];
}
Vector::Vector(const Vector& cop)
{
v=new int[cop.nr_elem];
nr_elem=cop.nr_elem;
int i;
for(i=0;i<nr_elem;i++)
v[i]=cop.v[i];
}
Vector::Vector(int x,int nr)
{
this->nr_elem=nr;
v=new int[nr];
for(int i=0;i<nr;i++)
v[i]=x;
}
int main()
{ /*
Vectori_de_vectori a(2,1);
int **C;
cin>>a;
Vectori_de_vectori b(3,5);
cout<<a;
cout<<endl;
cout<<b;
cout<<endl;
C=a+b;
for(int i=0;i<max(a.get_dim(),b.get_dim());i++)
{
for(int j=0;j<max(a.maxim_vectori(),b.maxim_vectori());j++)
cout<<C[i][j]<<" ";
cout<<endl;
}
for(int i=0;i<a.get_dim();i++)
delete[] C[i];
delete[] C;
*/
Vectori_de_vectori *k;
int nr_ob;
cout<<"Numarul de obiecte este ";
cin>>nr_ob;
k=new Vectori_de_vectori [nr_ob];
for(int i=0;i<nr_ob;i++)
cin>>k[i];
for(int i=0;i<nr_ob;i++)
cout<<k[i];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment