Sunteți pe pagina 1din 7

/**

operatii cu multimi : intersectia, reuniunea, diferenta


ordinea elementelor in multime este oarecare
*/

#include <iostream>

using namespace std;

const int maxdim = 100;

void citire(int u[], int &n)


{
cout<<"numar elemente = "; cin>>n;
cout<<" tastati elementele :";
for(int i=0; i<n; i++) cin>>u[i];
}

void afisare(int u[], int n)


{
cout<<"{";
for(int i=0; i<n; i++) cout<<u[i]<<" ";
cout<<" }";
}

void elimDuplicate(int u[], int &n)


{
for(int i = 0; i<n; i++)
for(int j = i+1; j<n; j++)
if(u[i] == u[j]) u[j--]=u[--n];
}

void intersectie(int u[], int m, int v[], int n, int w[], int &p)
{
p=0;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
if(u[i] == v[j]) {w[p++]=u[i];break;}
}
void reuniune(int u[], int m, int v[], int n, int w[], int &p)
{
for(p=0; p<m; p++)
w[p]= u[p];
int ok;
for(int i=0; i<n;i++)
{
ok=0;
for(int j=0; j<m; j++)
if(v[i] == u[j]) {ok=1; break;}
if(ok==0) w[p++]=v[i];
}
}

void diferenta(int u[], int m, int v[], int n, int w[], int &p)
{ p=0;
int ok;
for(int i=0; i<m; i++)
{
ok=0;
for(int j=0; j<n; j++)
if(u[i] == v[j]) {ok = 1; break;}
if(ok == 0) w[p++] = u[i];
}
}

int main()
{
int a[maxdim], b[maxdim], c[maxdim], m, n, p;
cout<<" multimea A : ";citire(a,m);
elimDuplicate(a,m);
cout<<" multimea B : ";citire(b,n);
elimDuplicate(b,n);
afisare(a,m);
cout<<" * ";
afisare(b,n);
intersectie(a,m,b,n,c,p);
cout<<" = ";
afisare(c,p);
cout<<endl;

afisare(a,m);
cout<<" + ";
afisare(b,n);
reuniune(a,m,b,n,c,p);
cout<<" = ";
afisare(c,p);
cout<<endl;

afisare(a,m);
cout<<" - ";
afisare(b,n);
diferenta(a,m,b,n,c,p);
cout<<" = ";
afisare(c,p);
cout<<endl;
}
#include <iostream>
using namespace std;
/**
Operatii cu multimi v2:Intersectia, Reuniunea, Diferenta a 2 multimi
*/

const int maxdim=100;

void citire(int u[],int &n)


{
cout<<"Dati numarul de elemente ale multimii : ";cin>>n;
cout<<"introduceti "<<n<<" elemente ";
for(int i=0;i<n;i++)cin>>u[i];
}

void afisare(int u[],int n)


{
cout<<"{";
for(int i=0;i<n;i++)cout<<u[i]<<", ";
cout<<"\b\b}";
}

void eliminDuplicate(int u[],int &n)


{
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(u[i]==u[j]){u[j--]=u[--n];}
}

bool apartine(int x,int u[],int n)


{
for(int i=0;i<n;i++)if(x==u[i])return 1;
return 0;
}

void adaug(int x, int u[],int &n)


{
u[n++]=x;
}
void intersectie(int u[],int m,int v[],int n, int w[],int &p)
{
p=0;
for(int i=0;i<m;i++)
if(apartine(u[i],v,n))adaug(u[i],w,p);
}

void reuniune(int u[],int m,int v[],int n, int w[],int &p)


{
for(p=0;p<m;p++)
w[p]=u[p];
for(int i=0;i<n;i++)
if(!apartine(v[i],u,p))adaug(v[i],w,p);
}

void diferenta(int u[],int m,int v[],int n, int w[],int &p)


{
p=0;
for(int i=0;i<m;i++)
if(!apartine(u[i],v,n))adaug(u[i],w,p);
}

bool vida(int u[],int m)


{
return m==0; ///return !m;
}

bool plina(int u[],int m)


{
return m==maxdim;
}

///Diferenta simetrica (U\V) U (V\U)

void difSimetrica(int u[],int m,int v[],int n, int w[],int &p)


{
p=0;
for(int i=0;i<m;i++)
if(!apartine(u[i],v,n))adaug(u[i],w,p);
for(int i=0;i<n;i++)
if(!apartine(v[i],u,m))adaug(v[i],w,p);
}
///functia returneaza 1 daca u inclus in v si 0 in caz contrar

bool inclus(int u[],int m,int v[],int n)


{ if (m>n) return 0;
for(int i=0;i<m;i++)
if(!apartine(u[i],v,n))return 0;
return 1;
}

int main()
{
int a[maxdim],m,b[maxdim],n,c[maxdim],p;
cout<<"Multimea A: ";citire(a,m);
cout<<"Multimea B: ";citire(b,n);
afisare(a,m);
cout<<" * ";
afisare(b,n);
intersectie(a,m,b,n,c,p);
cout<<" = ";
afisare(c,p);
cout<<endl;

afisare(a,m);
cout<<" U ";
afisare(b,n);
reuniune(a,m,b,n, c,p);
cout<<" = ";afisare(c,p);
cout<<endl;
afisare(a,m);
cout<<" - ";
afisare(b,n);
diferenta(a,m,b,n, c,p);
cout<<" = ";
afisare(c,p);
cout<<endl;
afisare(b,n);
cout<<" - ";
afisare(a,m);
diferenta(b,n,a,m,c,p);
cout<<" = ";
afisare(c,p);
cout<<endl;
difSimetrica(a,m,b,n,c,p);
afisare(a,m);
cout<<" DifSimetrica ";
afisare(b,n);
cout<<" = ";
afisare(c,p);
cout<<endl;
afisare(a,m);
cout<<" Inclus? in ";
afisare(b,n);
cout<<(inclus(a,m,b,n)?" da ":" nu ");
return 0;
}