PDA

Orijinalini görmek için tıklayınız : c++ net yazılar ve örnek kodlar


SuperMan
01-22-2012, 08:58 PM
Lab icin hazirladigim kumeler ile ilgili bir C++ calismasi, paylastikca kisir donguyu kirmak umidiyle... ))


#include <iomanip>
#include <iostream>
#include <cstring>
using namespace std;

class Integerset
{
private :
int *set;
int len;
public:
Integerset();
Integerset(int arr[],int x);
~Integerset();
Integerset Unionofsets(Integerset &,Integerset &,Integerset &);
Integerset Intersection(Integerset &,Integerset &,Integerset &);

int memberof(int);
void Insertelem(int,int);

void DeleteElem(int);

int Equal(Integerset &,Integerset &);
void Display();
void operator =(const Integerset &);

};







//////////////////////// D-E-F-A-U-L-T //////////////////////////////
Integerset ::Integerset()
{
len=10;
set=new int[len];
for(int i=0;i<len;i++)
set[i]=-1;
}
/////////////// C-O-N-S-T-R-U-C-T-O-R ///////////////////////
Integerset ::Integerset(int arr[],int x)
{
set=new int[x];
len=x;
for(int i=0;i<x;i++)
set[i]=arr[i];
}
//////////////// D-E-S-T-R-U-C-T-O-R ///////////////////////
Integerset ::~Integerset()
{

delete[] set;
}
////////////// C-O-P-Y ///////////////
void Integerset :perator =(const Integerset & ob)
{
for(int i=0;i<ob.len;i++)
set[i]=ob.set[i];
}
////////////////// U-N-I-O-N-S-E-T /////////////////

Integerset Integerset ::Unionofsets(Integerset &obj1,Integerset &obj2,Integerset &obj3)
{



for(int i=0;i<len;i++)
obj1.set[i]=obj3.set[i];
int t=0;
int k=obj3.len;

int n=0;

for(i=k;i<k+obj2.len;i++)
{
for(int j=0;j<obj3.len;j++)
{
if(obj2.set[n]!=obj3.set[j])
t=1;
else
{
t=0;
break;
}
}
if(t==1)
{
obj1.set[i]=obj2.set[n];
++n;
}
if(t==0)
{

n++;
}
t=0;
}
return obj1;
}

//////////////// I-N-T-E-R-S-E-C-T-I-O-N //////////////////
Integerset Integerset ::Intersection(Integerset &obj1,Integerset &obj2,Integerset &obj3)
{
for(int i=0;i<obj3.len;i++)
obj3.set[i]=-1;

int t=0;
int n=0;
for(i=0;i<obj1.len;i++)
{ t=0;
for(int j=0;j<obj2.len;j++)
{
if(obj1.set[i]==obj2.set[j])
{ t=1;
break;
}
else
t=0;
}
if(t==1)
{
obj3.set[n]=obj1.set[i];
n++;
}

}
return obj3;
}

/////////////////// D-I-S-P-L-A-Y ///////////////////////////

void Integerset :isplay()
{
cout<<"{";
for(int j=0;j<len;j++)
{ if(*(set+j)!=-1)
{ if(*(set+j)!=*(set+(j+1)))
{ if(j+1==len||j+1==8)
cout<<*(set+j);
else
cout<<*(set+j)<<",";
}
}

}
cout<<"}"<<endl;
}
/////////////////// M-E-M-B-E-R O-F ///////////////////////
int Integerset ::memberof(int a)
{
int t=0;
for(int i=0;i<len;i++)
{ if(set[i]==a)
t=1;
}
if(t==1)
return 1;
else
return 0;
}
/////////////////// I-N-S-E-R-T - E-L-E-M-E-N-T ///////////////////////
void Integerset ::Insertelem(int a,int loc)
{


int t=0;
for(int i=0;i<len;i++)
{ if(set[i]==a)
t=1;
}
if(t==1)
cout<<"the number is in the set "<<endl;
else
{if(loc>len)
cout<<"the aaaaaaaa out of size"<<endl;
else
set[loc]=a;
}

}
/// D-E-L-E-T-E - E-L-E-M-E-N-T ///
void Integerset :eleteElem(int a)
{
int t=0;
for(int i=0;i<len;i++)
{ if(set[i]==a)
{
set[i]=-1;
t=1;
}
}
if(t==1)
cout<<" the number "<<a<<" deleted "<<endl;
else
cout<<"the number cannot found in the set"<<endl;
}


//// E-Q-U-A-L ////
int Integerset::Equal(Integerset & obj1,Integerset& obj2)
{
int t=0;
for(int i=0;i<obj1.len;i++)
{ for(int j=0;j<obj2.len;j++)
{ if(obj1.set[i]==obj2.set[j])
{
t=1;
break;
}
else
{
t=0;
}
}
if(t==0)
break;
}
if(t==1)
return 1;
else
return 0;
}

///////////////////////////////////////////////////////////////////////////////////
////////// *** M-A-I-N *** ////////////
///////////////////////////////////////////////////////////////////////////////////
int main()
{

int a[4]={1,2,3},m,k;
int b[4]={7,2,8,6};
int c[5]={3,4,5,6,7};
Integerset s1(a,3);
Integerset s2(b,4);
Integerset s3(c,5);


Integerset s9,s10;
Integerset set1,set2;
Integerset s4,s5,s6;
Integerset s11(a,3),st4,st5,st6;
Integerset s22(b,4);
Integerset s33(c,5);
Integerset s;

///////////// PRINT three SETS //////////////
cout<<" 1-first set : ";
s1.Display();
cout<<endl;
cout<<" 2-second set : ";
s2.Display();
cout<<endl;
cout<<" 3-third set : ";
s3.Display();
/////////////////////////////////////////////////////////////////////////////
///////// PRINT UNION of TWO SETS ///////////

//Integerset s4,s5,s6;


cout<<"Enter two set to take union of them "<<endl;

cin>>k;
cin>>m;
if(k==1)
{
if(m==2)
{
s5=s1.Unionofsets(s6,s2,s1);
cout<<" unionset of first and second set = ";
s5.Display();cout<<endl;
}

else if(m==3)
{
s5=s1.Unionofsets(s6,s3,s1);
cout<<" unionset of first and third set = ";
s5.Display();cout<<endl;
}
}
else if(k==2)
{
if(m==3)
{
s5=s6.Unionofsets(s6,s3,s2);
cout<<" union of second and third set = ";
s5.Display();cout<<endl;
}
} cout<<endl;
////////////////////////////////////////////////////////////////////////
////////// PRINT INTERSECTIONSET of TWO SET... ////////////



cout<<" enter two set to take intersection of them ";
cin>>k;
cin>>m;
if(k==1)
{
if(m==2)
{
st4=s11.Intersection(s11,s22,st5);
cout<<" intersectionset of first and second set = ";
st4.Display();cout<<endl;
}
else if(m==3)
{
st4=s11.Intersection(s11,s33,st5);
cout<<" intersectionset of first and third = ";
st4.Display();cout<<endl;
}
else if(m==4)
{
st4=s11.Intersection(s11,s5,st5);
cout<<" intersectionset of first and fourth = ";
st4.Display();cout<<endl;
}
}
else if(k==2)
{
if(m==3)
{
st4=s22.Intersection(s22,s33,st5);
cout<<" intersectionset of second and third = ";
st4.Display();cout<<endl;
}
else if(m==4)
{
st4=s22.Intersection(s22,s5,st5);
cout<<" intersectionset of second and fourth = ";
st4.Display();cout<<endl;
}
}
else if(k==3)
{
if(m==4)
{
st4=s33.Intersection(s33,s5,st5);
cout<<" intersectionset of second and third = ";
st4.Display();cout<<endl;
}
}
////////////////////////////////////////////////////////////////////////
//////// CHECK MEMBER or NOT... //////////// //|

cout<<endl;
cout<<endl;
int z,y;
cout<<" enter a number to check it is member of sets or not : ";
cin>>y;
cout<<" which set do you want to check ";
cin>>z;
cout<<endl;
if(z==1)
{ if(s1.memberof(y))
cout<<" yes "<<y<<" is member of first set "<<endl;
else
cout<<" No "<<y<<" is not member of first set "<<endl;
cout<<endl;
}
else if(z==2)
{ if(s2.memberof(y))
cout<<" yes "<<y<<" is member of second set "<<endl;
else
cout<<" No "<<y<<" is not member of second set "<<endl;
cout<<endl;
}
else if(z==3)
{ if(s3.memberof(y))
cout<<" yes "<<y<<" is member of third set "<<endl;
else
cout<<" No "<<y<<" is not member of third set "<<endl;
cout<<endl;
}
else if(z==4)
{ if(s5.memberof(y))
cout<<" yes "<<y<<" is member of unionset "<<endl;
else
cout<<" No "<<y<<" is not member of unionset "<<endl;
cout<<endl;
}
else if(z==5)
{ if(st4.memberof(y))
cout<<" yes "<<y<<" is member of intersectionset "<<endl;
else
cout<<" No "<<y<<" is not member of intersectionset "<<endl;
cout<<endl;
}



//////////////////////////////////////////////////////////////////////////
///////// INSERTING NUMBER to CURRENT SETS (s1,s2,s3) /////////
char ch;
cout<<" 1-first set : ";
s1.Display();
cout<<endl;
cout<<" 2-second set : ";
s2.Display();
cout<<endl;
cout<<" 3-third set : ";
s3.Display();
cout<<endl;
cout<<" 4-unionset : ";
s5.Display();
cout<<endl;
cout<<" 5-intersectionset : ";
st4.Display();
cout<<endl;

{

int f,g,loc;
cout<<" enter a number to insert ";
cin>>f;
cout<<endl;
cout<<" Which set would you like to insert number "<<f<<"";
cin>>g;
cout<<" Which aaaaaaaa would you like to insert ? :";
cin>>loc;
if(g==1)

{ s1.Insertelem(f,loc);
s1.Display();
}
else if(g==2)
{ s2.Insertelem(f,loc);
s2.Display();
}
else if(g==3)
{ s3.Insertelem(f,loc);
s3.Display();
}
else if(g==4)
{ s5.Insertelem(f,loc);
s5.Display();
}
else if(g==5)
{
st4.Insertelem(f,loc);
st4.Display();
}
else if(g==5)
{
s.Insertelem(f,loc);
s.Display();
}
//////////////////////////////////////////////////////////////////////////
/////////// DELETING NUMBER from CURRENT SETS (s1,s2,s3) /////////
cout<<endl;

cout<<" 1-first set : ";
s1.Display();
cout<<endl;
cout<<" 2-second set : ";
s2.Display();
cout<<endl;
cout<<" 3-third set : ";
s3.Display();
cout<<endl;
cout<<" 4-unionset : ";
s5.Display();
cout<<endl;
cout<<" 5-intersectionset : ";
st4.Display();
cout<<endl;

cout<<endl;


int r;
cout<<" enter a number to delete : ";
cin>>r;
cout<<" from which set would you like to delete number "<<r<<"";
cin>>g;


if(g==1)
{ s1.DeleteElem(r);
s1.Display();
}
else if(g==2)
{ s2.DeleteElem(r);
s2.Display();
}
else if(g==3)
{ s3.DeleteElem(r);
s3.Display();
}
else if(g==4)
{ s5.DeleteElem(r);
s5.Display();
}
else if(g==5)
{
st4.DeleteElem(r);
st4.Display();
}
else if(g==5)
{
s.DeleteElem(r);
s.Display();
}
}
////////////////////////////////////////////////////////////////////////////
///////////// FIRST AND SECOND set ARE EQUAL or NOT ///////////////////
cout<<endl;
int temp;
temp=s1.Equal(s1,s2);
if(temp==1)
{ cout<<"first set : ";
s1.Display();
cout<<"second set : ";
s2.Display();
cout<<"they are equal "<<endl;
}
else
{ cout<<"first set : ";
s1.Display();
cout<<"second set : ";
s2.Display();
cout<<"they are not equal "<<endl;
} cout<<endl;




return 0;
}