I Have a problem with my Sets ADT. Every thing of ADT is working Properly but only the Intersection... Function is not Working Properly... I tried many times... to get required output but some times it gives the Correct Intersection of Sets but Some times it Displayes Wrong Intersection...
Here is the Code...
Code:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void mainmenu(void);
class CSets
{
private:
int *s1,*s2,*s3,*temp;
int los1,los2,los3,cpos,ctr;
public:
CSets(); //Defualt Constructor....
void display1(void);
void display2(void);
void Input(void);
void Union(void);
void Intersec(void);
void Diff(void);
void Equality(void);
void setlos1(int leng);
void setlos2(int leng);
int los1len(void);
int los2len(void);
}obj1;
CSets::CSets()
{
cout<<"Constructor Fired.";
los1 = 1;
los2 = 1;
for (ctr=0; ctr<los1; ctr++ )
{
s1[ctr] = 0;
}
for (ctr=0; ctr<los2; ctr++ )
{
s2[ctr] = 0;
}
cpos=0; //Current Position Container Variable.
} //Constructor Defination Ends Here..
////////////////////////////////////////////////////////////////////////////
//////////////////////////////// Main Function//////////////////////////////
int main(void)
{
int len1,len2;
clrscr();
check1:
cout<<"\rEnter Length of Set 1 must be > 0: ";
cin>>len1;
if(len1 <= 0)
{
goto check1;/// heheehee
}
check2:
cout<<"Enter Length of Set 2 must be > 0: ";
cin>>len2;
if(len2 <= 0)
{
goto check2;/// heheehee
}
CSets obj();
obj1.setlos1(len1);
obj1.setlos2(len2);
mainmenu();
getch();
return 0;
}
//////////////////////////////////////////////////////////////
////////////////////////////Main Menu Function////////////////
void mainmenu(void)
{
int caseinp=0;
clrscr();
cout<<"Class CSets Main Menu."<<endl;
cout<<"1. Enter Values in the Sets."<<endl;
cout<<"2. To find Union of the Sets."<<endl;
cout<<"3. To find Intersection of the Sets."<<endl;
cout<<"4. To find Difference b/w Sets."<<endl;
cout<<"5. To check wheather Sets are Equal."<<endl;
cout<<"6. To Quit From the Program."<<endl;
check3:
cout<<"Enter Your Choice # [ 1 to 6 ]: ";
cin>>caseinp;
if (caseinp < 1 || caseinp > 8)
{
cout<<"Invalid Choice"<<endl;
goto check3;/// goto... useage.. to hell with it
}
if(caseinp == 1)
{
obj1.Input();
}
if(caseinp == 2)
{
cout<<"Union Function."<<endl;
obj1.Union();
}
if(caseinp == 3)
{
cout<<"Intersection Function."<<endl;
obj1.Intersec();
}
if(caseinp == 4)
{
obj1.Diff();
}
if(caseinp == 5)
{
obj1.Equality();
}
if(caseinp == 6)
{
cout<<"Bye Bye...";
getch();
exit(0);
}
if(caseinp == 7)
{
obj1.display1();
getch();
mainmenu();
}
if(caseinp == 8)
{
obj1.display2();
getch();
mainmenu();
}
}
////////////////////////////////////////////////////////////
///////////////////////////////Class Member Functions./////
/////////////////////////////////Input Function./////////////
void CSets::Input(void)
{
int inp,i;
cout<<"Los1 = "<<los1<<" and Los2 = "<<los2<<endl;
cout<<"Input Function."<<endl;
///////////////////////////Input of Set 1/////////////////////////
for(ctr=0;ctr<los1;ctr++)
{
if( cpos != los1)
{
los1inp: //goto.... pointer,,,
cout<<"Enter Value for Set 1\'s Location "<<ctr+1<<": ";
cin>>inp;
for(i=0; i<cpos;i++)
{
if(inp == s1[i])
{
cout<<"Duplicate Value Entery. Can Not Enter Value"<<endl;
goto los1inp; //
}
}
} //End of else
s1[cpos]=inp;
cpos++;
}
cpos=0;
////////////////////////////Input for Set 2/////////////////////
for(ctr=0;ctr<los2;ctr++)
{
if( cpos != los2)
{
los2inp: //goto........ sehhehsh
cout<<"Enter Value for Set 2\'s Location "<<ctr+1<<": ";
cin>>inp;
for(i=0; i<cpos;i++)
{
if(inp == s2[i])
{
cout<<"Duplicate Value Entery. Can Not Enter Value"<<endl;
goto los2inp; // ooops goto useage...
}
}
} //End of else
s2[cpos]=inp;
cpos++;
}
cpos=0;
/////////////////////////////////////////////////
getch();
mainmenu();
}
/////////////////////////////////Union Function.////////////////////////////
void CSets::Union(void)
{
int i,count=0;
los3=los1;
s3=new int[los3]; //
for(ctr=0;ctr<los3;ctr++) //
{ //
s3[ctr]=s1[ctr];
}
for(ctr=0;ctr<los2;ctr++) //
{
count=0;
for(i=0;i<los3;i++)
{
if(s2[ctr] == s3[i])
{
count++;
}
}
if(count == 0)
{
temp=new int[los3];
for(i=0;i<los3;i++)
{
temp[i]=s3[i];
}
los3++;
s3=new int[los3];
for(i=0;i<los3;i++)
{
s3[i]=temp[i];
}
s3[los3-1]=s2[ctr];
}
}
cout<<"The Union is"<<endl;
for(i=0;i<los3;i++)
cout<<"\t"<<s3[i];
getch();
mainmenu();
}
/////////////////////////////Intersection Function.//////////////////////////
void CSets::Intersec(void)
{
int i,count=0;
los3=0;
s3=new int[los3]; //
s3[0]=0;
for(ctr=0;ctr<los1;ctr++) //
{
count=0;
for(i=0;i<los2;i++)
{
if(s1[ctr] == s2[i])
{
count++;
}
}
if(count != 0)
{
temp=new int[los3];
for(i=0;i<los3;i++)
{
temp[i]=s3[i];
}
los3++;
s3=new int[los3];
for(i=0;i<los3;i++)
{
s3[i]=temp[i];
}
s3[los3-1]=s1[ctr];
}
}
cout<<"The Intersection is ";
cout<<(los3 == 0 ? "nothing.": "as follows.")<<endl;
for(i=0;i<los3;i++)
cout<<"\t"<<s3[i];
getch();
mainmenu();
}
/////////////////////////////Difference Function.///////////////////////////
void CSets::Diff(void)
{
int i,count=0,check=0;
cout<<"The difference b/w sets is"<<endl;
los3=0;
s3=new int[los3]; //
for(ctr=0;ctr<los1;ctr++) //
{
count=0;
for(i=0;i<los2;i++)
{
if(s2[ctr] == s1[i])
{
count++;
}
}
if(count == 0)
{
temp=new int[los3];
for(i=0;i<los3;i++)
{
temp[i]=s3[i];
}
los3++;
s3=new int[los3];
for(i=0;i<los3;i++)
{
s3[i]=temp[i];
}
s3[los3-1]=s1[ctr];
}
}
if(count == 0)
{
cout<<"Values of Set 1 not found in Set 2."<<endl;
for(i=0;i<los3;i++)
cout<<"\t"<<s3[i];
cout<<endl;
}
else if ( count > 0)
{
cout<<"No Value of Set 1 is missing in Set 2.";
}
cout<<endl;
check=los3;
//////////////////////////////////////////////////////////////////
for(ctr=0;ctr<los2;ctr++) //
{
count=0;
for(i=0;i<los1;i++)
{
if(s2[ctr] == s1[i])
{
count++;
}
}
if(count == 0)
{
temp=new int[los3];
for(i=0;i<los3;i++)
{
temp[i]=s3[i];
}
los3++;
s3=new int[los3];
for(i=0;i<los3;i++)
{
s3[i]=temp[i];
}
s3[los3-1]=s2[ctr];
}
}
if(count == 0)
{
cout<<"Values of Set 2 not found in Set 1."<<endl;
for(i=check;i<los3;i++)
cout<<"\t"<<s3[i];
cout<<endl;
}
else if ( count > 0 )
{
cout<<"No Value of Set 2 missing in Set 1.";
}
count=0;check=0;
getch();
mainmenu();
}
////////////////////////////Equality Function.//////////////////////////////
void CSets::Equality(void)
{
int i,count;
if(los1 != los2)
{
cout<<"Length of Sets are not Equal so Sets are not equal to each other."<<endl;
getch();
mainmenu();
}
else if(los1 == los2)
{
count=0;
for(ctr=0;ctr<los1;ctr++)
{
for(i=0;i<los2;i++)
{
if(s1[ctr] == s2[i])
{
count++;
}
}
}
if(count != los1)
{
cout<<"The Sets Are not Equal to each other."<<endl;
}
else if(count == los1)
{
cout<<"The Sets Are Equal to each other.";
}
} //end of else if
getch();
mainmenu();
}
/////////////////////////////Set LOS1 Function///////////////////////////////
void CSets::setlos1(int leng)
{
los1=leng;
s1 = new int[los1];
for (ctr=0;ctr<los1;ctr++)
{
s1[ctr]=0;
}
}
/////////////////////////////Set LOS2 Function///////////////////////////////
void CSets::setlos2(int leng)
{
los2=leng;
s2 = new int[los2];
for (ctr=0;ctr<los2;ctr++)
{
s2[ctr]=0;
}
}
///////////////////////////// LOS1 Length Function///////////////////////////////
int CSets::los1len(void)
{
return los1;
}
///////////////////////////// LOS2 Length Function///////////////////////////////
int CSets::los2len(void)
{
return los2;
}
///////////////////////////////////////////////////////////////////////////
void CSets::display1(void)
{
cout<<"Data in set 1"<<endl;
for(ctr=0;ctr<los1;ctr++)
cout<<"\t"<<s1[ctr];
}
///////////////////////////////////////////////////////////////////////////
void CSets::display2(void)
{
cout<<"Data in set 2"<<endl;
for(ctr=0;ctr<los2;ctr++)
cout<<"\t"<<s2[ctr];
}
Check out the Intersection Function.. Is there problem in finding Intersection or problem with the Display....????
[code][/code]tagged by Salem