Created
July 1, 2017 04:37
-
-
Save vnkdj5/ad2918066b63a46a37d65ee5e3c60e57 to your computer and use it in GitHub Desktop.
Understanding Various Set Operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class Student | |
{ | |
int sid[50]; | |
int total; | |
public: | |
Student() | |
{ | |
total=0; | |
} | |
void accept(); | |
void display(); | |
void intersection(Student,Student); | |
int unionS(Student,Student); | |
void diff(Student,Student); | |
}; | |
void Student::accept() | |
{ | |
cout<<"\nENter Number of Students: "; | |
cin>>total; | |
for(int i=0;i<total;i++) | |
{ | |
cout<<"Enter Student Id: "; | |
cin>>sid[i]; | |
for(int j=0;j<i;j++) | |
{ | |
if(sid[j]==sid[i]) | |
{ | |
cout<<"\nRepeated Record.Please Enter Again!!"; | |
i--; | |
break; | |
} | |
} | |
} | |
} | |
void Student::display() | |
{ | |
for(int i=0;i<total;i++) | |
{ | |
cout.width(5); | |
cout<<sid[i]; | |
} | |
cout<<endl; | |
} | |
void Student::intersection(Student s1,Student s2) | |
{ | |
for(int i=0;i<s1.total;i++) | |
{ | |
for(int j=0;j<s2.total;j++) | |
{ | |
if(s1.sid[i]==s2.sid[j]) | |
{ | |
sid[total]=s1.sid[i]; | |
total++; | |
break; | |
} | |
} | |
} | |
} | |
void Student::diff(Student s1,Student s2) | |
{ | |
int flag=1; | |
for(int i=0;i<s1.total;i++) | |
{ | |
for(int j=0;j<s2.total;j++) | |
{ | |
if(s1.sid[i]==s2.sid[j]) | |
{ | |
flag=0; | |
break; | |
} | |
else flag=1; | |
} | |
if(flag==1) | |
{ | |
sid[total]=s1.sid[i]; | |
total++; | |
} | |
} | |
} | |
int Student::unionS(Student s1,Student s2) | |
{ | |
int flag=0; | |
int i=0; | |
for(i=0;i<s1.total;i++) | |
{ | |
sid[total]=s1.sid[i]; | |
total++; | |
} | |
for(int j=0;j<s2.total;j++) | |
{ | |
flag=0; | |
for(i=0;i<s1.total;i++) | |
{ | |
if(s1.sid[i]==s2.sid[j]) | |
{ | |
flag=1; | |
break; | |
} | |
} | |
if(flag==0) | |
{ | |
sid[total]=s2.sid[j]; | |
total++; | |
} | |
} | |
return total; | |
} | |
int main() | |
{ | |
Student cric,bad,sunion,onlycric,onlybad,both; | |
int class_total,union_total; | |
cout<<"\nENter Number of Students in a class : "; | |
cin>>class_total; | |
cout<<"\nEnter INFO FOR Students who play cricket: \n"; | |
cric.accept(); | |
cout<<"\nEnter INFO FOR Students who play Badminton: \n"; | |
bad.accept(); | |
cout<<"\n---------------------------------------------------\n"; | |
cout<<"| Students' Information |"; | |
cout<<"\n---------------------------------------------------\n"; | |
cout<<"Cricket Set :"; | |
cric.display(); | |
cout<<"\nBadminton Students Set: "; | |
bad.display(); | |
cout<<"\nCricket or badminton or both Students set: "; | |
union_total= sunion.unionS(cric,bad); | |
sunion.display(); | |
cout<<"\nStudents play Both Cricket and Badminton Set: "; | |
both.intersection(cric,bad); | |
both.display(); | |
cout<<"\nStudents play only cricket set: "; | |
onlycric.diff(cric,bad); | |
onlycric.display(); | |
cout<<"\nStudents play only badminton set: "; | |
onlybad.diff(bad,cric); | |
onlybad.display(); | |
cout<<"\nTotal Number of students who play neither cricket nor badminton: "<<class_total-union_total; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment