Total members 11895 |It is currently Sat Dec 21, 2024 1:55 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This is a Sets manuplation program that perform some basic operation on sets such as:

1. Intersection of two sets.
2. Union of two sets.
3. Difference of two sets.
4. Complement of one set.
5. Is Subset operation.
6. Is Disjoint operation.
7. Searching in one Set.
7. Sorting of one set.

cpp code
//main.cpp

#include<iostream.h>
#include<windows.h>
#include "Set.cpp"
void setcolor(unsigned short color);
/////////////////////////////////////////////////////////
int main()
{
Set<int>a,b,c;
int choice;
int num;
bool x;
char ch;
/////////////////////////////////////////////////////////
do
{
setcolor(14);

cout<<" \t * M E N U * "<<endl;
cout<<" \t ############# "<<endl<<endl;
cout<<"\t\t\t***********************************"<<endl;
cout<<"\t\t\t* 1.Intersection 5.Complement *"<<endl
<<"\t\t\t* 2.Union 6.Is Subset *"<<endl
<<"\t\t\t* 3.Difference 7.Searching *"<<endl
<<"\t\t\t* 4.Is Disjoint 8.Sorting *"<<endl;
cout<<"\t\t\t***********************************"<<endl;
/////////////////////////////////////////////////////////

cout<<"\n\t-Enter your Operation:(1 to 8): ";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:

a.setter();
b.setter();
c=a.getIntersect(b);
cout<<"\nThe intersection is: ";
c.Display();
break;

case 2:
a.setter();
b.setter();
c=a.getUnion(b);
cout<<"\nThe union is: ";
c.Display();
break;

case 3:
a.setter();
b.setter();
c=a.getDifference(b);
cout<<"\nThe difference is: ";
c.Display();
break;

case 4:
a.setter();
b.setter();
x=a.IsDisjoint(b);
if(x)
cout<<"\nThe two sets are Disjoints \n"<<endl;
else
cout<<"\nThe two sets are not Disjoints\n "<<endl;
break;

case 5:
a.setter();
b.setter();
c=a.getComplement(b);
cout<<"\nThe complement is: ";
c.Display();
break;

case 6:
a.setter();
b.setter();
x=a.IsSubset(b);

setcolor(12);
if(x)
cout<<"\nThe first set is subset from the second \n"<<endl;
else
cout<<"\nThe first set is not subset from the second \n"<<endl;
break;

case 7:
a.setter();
cout<<endl<<"Enter the number to search: ";
cin>>num;
a.Search(num);
break;

case 8:
a.setter();
a.Sort();
cout<<"The sorted one is: ";
a.Display();
break;

default:

cout<<"\nPlease enter number between 1->6 "<<endl;
}

setcolor(9);

cout<<"\nAre you want to continue (y / n): ";
cin>>ch;

system("cls");

}while(ch=='y');

return 0;

}

/////////////////////////////////////////////////////////

void setcolor(unsigned short color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}

cpp code
//Set.h
//interface for the Set class.
/////////////////////////////////////////////////////////

#if !defined(AFX_SET_H__1DF8A41D_D648_4137_8CCD_39B0C0A002D7__INCLUDED_)
#define AFX_SET_H__1DF8A41D_D648_4137_8CCD_39B0C0A002D7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif
/////////////////////////////////////////////////////////

template<class SetElementType>
class Set
{
public:

Set();
virtual ~Set();
void setter();
Set getIntersect(Set);
Set getUnion(Set);
Set getDifference(Set);
Set getComplement(Set);
void Sort();
void Search(SetElementType);
bool IsSubset(Set);
bool IsDisjoint(Set);
bool IsEmpty();
void Display();

private:

SetElementType data[100];
int Size;
};

#endif


cpp code
// Set.cpp: implementation of the Set class

#include<iostream.h>
#include "Set.h"

/////////////////////////////////////////////////////////
// Construction/Destruction
/////////////////////////////////////////////////////////

template<class SetElementType>
Set<SetElementType>::Set()
{

Size=0;
}

/////////////////////////////////////////////////////////

template<class SetElementType>
Set<SetElementType>::~Set()
{
//delete data;

}

//////////////////////////////////////////////////////////////////////////////////

template<class SetElementType>
void Set<SetElementType>::setter()
{
setcolor(10);

cout<<"Enter the Size: ";

cin>>Size;

cout<<"\nEnter the elements: ";

for(int i=0;i<Size;i++)

cin>>data[i];

cout<<endl;

}

/////////////////////////////////////////////////////////


template<class SetElementType>
Set<SetElementType> Set<SetElementType>::getIntersect(Set a)
{
Set intersection;

for(int i=0;i<Size;i++)

for(int j=0;j<a.Size;j++)

if(data[i]==a.data[j])

{
intersection.data[intersection.Size]=data[i];
intersection.Size++;
}


return intersection;

}

/////////////////////////////////////////////////////////

template<class SetElementType>
Set<SetElementType> Set<SetElementType>::getUnion(Set a)
{
Set Union;

for(int i=0;i<Size;i++)
{
bool flag=true;

for(int j=0;j<a.Size;j++)

if(data[i]==a.data[j])
flag=false;


if(flag)
{
Union.data[Union.Size]=data[i];
Union.Size++;
}
}

for(i=0;i<a.Size;i++)
{
Union.data[Union.Size]=a.data[i];
Union.Size++;
}


return Union;

}

/////////////////////////////////////////////////////////

template<class SetElementType>
Set<SetElementType> Set<SetElementType>::getDifference(Set a)

{
Set difference;

bool flag=true;

for(int i=0;i<Size;i++)
{
for(int j=0;j<a.Size;j++)

if(data[i]==a.data[j])
flag=false;

if(flag)
{
difference.data[difference.Size]=data[i];
difference.Size++;
}

}

return difference;
}

/////////////////////////////////////////////////////////

template<class SetElementType>
void Set<SetElementType>::Display()
{
setcolor(12);

cout<<"{ ";

if(IsEmpty())
cout<<"Phay";

else

for(int i=0;i<Size;i++)
cout<<data[i]<<" , ";

cout<<" }"<<endl<<endl;

}

/////////////////////////////////////////////////////////

template<class SetElementType>
Set<SetElementType> Set<SetElementType>::getComplement(Set U)
{
Set complement;

bool flag=true;

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

for(int j=0;j<U.Size;j++)

if(data[i]==U.data[j])
flag=false;

if(flag)
{
complement.data[complement.Size]=data[i];
complement.Size++;
}

}

return complement;

}

/////////////////////////////////////////////////////////
template<class SetElementType>
bool Set<SetElementType>::IsDisjoint(Set a)
{
Set disjoint=this->getIntersect(a);

if(disjoint.Size==0)
return true;

return false;
}

/////////////////////////////////////////////////////////

template<class SetElementType>
bool Set<SetElementType>::IsSubset(Set a)
{
bool flag;

for(int i = 0; i < Size; i++)
{
flag = false;

for(int j = 0; j < a.Size; j++)
if(data[i] == a.data[j])
flag = true;

if(flag == false)
return false;
}

return true;

}

/////////////////////////////////////////////////////////template<class SetElementType>
bool Set<SetElementType>::IsEmpty()
{
return Size==0;
}

/////////////////////////////////////////////////////////
template<class SetElementType>
void Set<SetElementType>::Search(SetElementType item)
{
for(int i=0;i<Size;i++)
if(data[i]==item)
{
cout<<"\nThe position of the element that you search is: "<<i+1<<endl;

break;
}
if(i==Size)
cout<<"\nThe element that you search not found "<<endl;

}

/////////////////////////////////////////////////////////

template<class SetElementType>
void Set<SetElementType>::Sort() //Buble sort
{
int temp,sorted=0;
for(int i=0;i<Size && !sorted;i++)
{
sorted=1;
for(int j=Size-1;j>0;j--)

if(data[j]<data[j-1])
{
sorted=0;

temp=data[j];

data[j]=data[j-1];

data[j-1]=temp;
}
}

}

//////




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Sets Manuplation (C++)
 compare two sets     -  
 Dynamic Frame-sets with java script protocols     -  



Topic Tags

C++ Math
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com