/*
* File: sets.cpp
* ------------------
* This program deals with sets of lower case letters.
* This program computes the union and intersection of two sets. This program
* also checks if set B is a subset of set A and returns true if set B is a subset
* of set A and returns false if not.
*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#define size 26
void init(bool set[]);
void initC(bool setC[]);
void displaySet(bool set[]);
bool unions(bool A[],bool B[],bool C[]);
bool intersection(bool A[],bool B[],bool C[]);
bool contain(bool A[],bool B[]);
typedef enum
{
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
}
letters;
int main()
{
bool A[size];
bool B[size];
bool C[size];
printf("This program deals with sets of lower case letters. This program computes the\n");
printf("union and intersection of two sets. This program also checks if set B is a\n");
printf("subset of set A and returns true if set B is a subset of set A and returns\n");
printf("false if not.\n");
printf("\n");
printf("Enter a list of lowercase letters as elements for set A.\n");
init(A);
printf("\n");
printf("Enter a list of lowercase letters as elements for set B.\n");
init(B);
initC(C);
unions(A,B,C);
displaySet(C);
initC(C);
intersection(A,B,C);
initC(C);
if(contain(A,B))
printf("B is a proper subset of A: true\n");
else
printf("B is a proper subset of A: false\n");
}
/*************************************...
/* This function initializes the values for each set. */
void init(bool set[])
{
char element;
letters l;
printf("Press enter after each element.\n");
printf("Signal the end of the list by pressing 'control' 'z' and 'enter'.\n");
printf("Enter the first element:");
for(l=a; l<=z;l=(letters)(l+1))
{
set[l]=false;
}
while(element=getchar()!=EOF)
{
element=element-97;
l=(letters)element;
if(l>=a&&l<=z)
set[l]=true;
}
}
/*************************************...
/* This function displays the values of a set to the output. */
void displaySet(bool set[])
{
letters l;
for(l=a; l<=z; l=(letters)(l+1))
{
if(set[l])
printf("%c", (l+97));
}
}
/*************************************...
/*This function computes the union of sets A and B and puts the value in set C.*/
bool unions(bool A[],bool B[],bool C[])
{
letters letter;
printf("The union of set A and set B = { ");
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if((A[letter])||(B[letter]))
C[letter]=true;
}
return(C);
}
/***********************************in...
/* This function computes the intersection of set A and set B and puts the result
* in set C. */
bool intersection(bool A[],bool B[],bool C[])
{
letters letter;
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if(A[letter]&&B[letter])
C[letter]=true;
}
return(C);
}
/************************************c...
/* This function returns true if B is a proper subset of A and returns false otherwise. */
bool contain(bool A[],bool B[])
{
bool isSubset;
letters letter;
isSubset=true;
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if(B[letter]&&A[letter])
{
isSubset=true;
A[letter]=false;
B[letter]=false;
}
if(B[letter]&&!A[letter])
isSubset=false; break;
}
if(isSubset)
{
isSubset=false;
for(letter=a; letter<=z; letter=(letters)(letter+1))
{
if(A[letter]&&!B[letter])
isSubset=true;
}
}
return(isSubset);
}
/*************************************...
/* This function re-initializes the values of set C to false. */
void initC(bool setC[])
{
letters l;
for(l=a; l<=z; l=(letters)(l+1))
{
setC[l]=false;
}
}
I am not able to get the sets to print on the screen. Can someone please help me with a solution to this.