In: Computer Science
Program
#include <iostream>
#include <fstream>
using namespace std;
string unionOf(int a[], int b[]){
string s="Union of SET A and SET B is ";
for(int i=0; i<128; i++){
if(a[i]==1 || b[i]==1){
char c=i;
s+=c;
s+=" ";
}
}
return s;
}
string intersection(int a[], int b[]){
string s="intersection of SET A and SET B is ";
for(int i=0; i<128; i++){
if(a[i]==1 && b[i]==1){
char c=i;
s+=c;
s+=" ";
}
}
return s;
}
string difference(int a[], int b[]){
string s="Difference of SET A and SET B is ";
for(int i=0; i<128; i++){
if(a[i]-b[i]==1){
char c=i;
s+=c;
s+=" ";
}
}
return s;
}
string properSubset(int a[], int b[]){
string s;
int m=0;
int n=0;
for(int i=0; i<128; i++){
if(b[i]-a[i]==-1){
return "SET A is not a proper subset of SET B";
}
if(a[i]==1){
m++;
}
if(b[i]==1){
n++;
}
}
if(m==n){
return "SET A is not a proper subset of SET B";
}
return "SET A is a proper subset of SET B";
}
int main(){
int setA[128];
int setB[128];
for(int i=0; i<128; i++){
setA[i]=0;
setB[i]=0;
}
string str;
int j;
ifstream file("Read.txt");
getline(file, str);
cout<<str<<endl;
for(int i=9; i<str.length(); i++){
j=(int)str.at(i);
setA[j]=1;
}
getline(file,str);
cout<<str<<endl;
for(int i=0; i<str.length(); i++){
j=(int)str.at(i);
setB[j]=1;
}
ofstream myfile;
myfile.open ("output.txt");
string result=unionOf(setA,setB);
cout<<result<<endl;
myfile<<result<<endl;
result=intersection(setA,setB);
cout<<result<<endl;
myfile<<result<<endl;
result=difference(setA,setB);
cout<<result<<endl;
myfile<<result<<endl;
cout<<properSubset(setA,setB)<<endl;
myfile<<result<<endl;
myfile.close();
}
Output