Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save denisachicarosa/2e7e6fc5c9d1205a9180251e263c91e8 to your computer and use it in GitHub Desktop.
Save denisachicarosa/2e7e6fc5c9d1205a9180251e263c91e8 to your computer and use it in GitHub Desktop.
Almost done
#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();
Vectori_de_vectori(int x, int d);
~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();
void n_obiecte();
};
Vectori_de_vectori::Vectori_de_vectori()
{
dim=0;
V=NULL;
}
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;
a.V=new Vector[a.dim];
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;
}
void Vectori_de_vectori::n_obiecte()
{
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<<"\n Obiectul "<<i<<" : \n "; cout<<k[i]; cout<<"\n";}
}
void comenzi ()
{
cout<<"Comanda 1: se va apela constructorul pentru initializarea cu un numar dat "<<endl;
cout<<"Comanda 2: se vor apela constructorii pentru initializare si copiere"<<endl;
cout<<"Comanda 3: supraincarcarea operatorilor << si >>"<<endl;
cout<<"Comanda 4: metoda publica prin care se creeaza o matrice"<<endl;
cout<<"Comanda 5: suma a doua obiecte prin supraincarcarea operatorului +"<<endl;
cout<<"Comanda 6: citirea a n obiecte, memorarea si afisarea acestora "<<endl;
}
void cazul_1()
{
cout<<"\n Se va initializa cu ";
int d,r;
cin>>r;
cout<<"Va avea dimensiunea ";
cin>>d;
Vectori_de_vectori o1(r,d);
cout<<o1;
}
void cazul_2()
{
cout<<"Setam vectorul ce va urma sa fie copiat : \n";
cout<<"\n Se va initializa cu ";
int d,r;
cin>>r;
cout<<"Va avea dimensiunea ";
cin>>d;
Vectori_de_vectori o1(r,d);
Vectori_de_vectori o2(o1);
cout<<"Vectorul copiat este: \n";
cout<<o2;
}
void cazul_3()
{
Vectori_de_vectori a;
cin>>a;
cout<<a;
}
void cazul_4()
{
Vectori_de_vectori a;
cin>>a;
int** M;
M=a.creez_matrice();
for(int i=0;i<a.get_dim();i++)
{for(int j=0;j<a.maxim_vectori();j++)
cout<<M[i][j]<<" ";
cout<<'\n';
}
for(int i=0;i<a.get_dim();i++)
delete[] M[i];
delete[] M;
}
void cazul_5()
{
int **M;
Vectori_de_vectori a;
Vectori_de_vectori b;
cout<<"\n Primul vector \n";
cin>>a;
cout<<"\n Al doilea vector \n ";
cin>>b;
M=a+b;
cout<<"\n Suma este: \n";
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<<M[i][j]<<" ";
cout<<endl;
}
for(int i=0;i<max(a.get_dim(),b.get_dim());i++)
delete[] M[i];
delete[] M;
}
void cazul_6()
{
Vectori_de_vectori a;
a.n_obiecte();
}
int main()
{
comenzi();
int comanda;
cout<<"Introduceti comanda dorita sau 0 pentru iesire ";
cin>>comanda;
while(comanda)
{
switch(comanda)
{
case 1:
{cazul_1(); break;}
case 2:
{cazul_2(); break;}
case 3:
{cazul_3(); break;}
case 4:
{cazul_4(); break;}
case 5:
{cazul_5(); break;}
case 6:
{cazul_6(); break;}
}
cout<<"Introduceti comanda dorita sau 0 pentru iesire ";
cin>>comanda;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment