In: Computer Science
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file prog1 output.txt. Write a title before the output of each operation.
1. Union
2. Intersection
3. A - B
4. Decide if (A ⊂ B).
program 2.
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file prog2 output.txt and the standard output. Write a title before the output of each operation.
1. (A∪B)∪C.
2. A∪(B∩C).
3. (A∪B)∩(A∪C).
Hints:
You may want to use arrays.
Pay special attention to the parentheses because they indicate the order. For example: (A ∪B) ∪ C means to compute (A ∪ B) first, and then ∪C.
Consider reusing the algorithms for union and intersection that
you have defined already. For example, to compute (A ∪ B) ∪
C:
First, compute A ∪ B using the function that computes the union
that you already defined. Let’s say that you store that result in
an array R.
Then, compute R ∪ C using the function that computes the union that you already defined. 1
PLease put COMMENTS. I am new to C++
Program 1:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
char sets[2][1000]; //for two sets
int index[2] = { 0, 0 }; //to keep track of index of each set
ifstream ifs("prog1 input.txt");
int ssets = 0;
//to read the elements from file
while (getline(ifs, line)) {
for (int i = 0; i < line.length(); i++) {
if (line[i] != ' ') {
sets[ssets][index[ssets]++] = line[i];
}
}
ssets++;
}
ifs.close(); //closing the input file
ofstream ofs;
ofs.open("prog1 output.txt", ios::trunc); //opening the output file
//to do Union of the two sets
ofs << "1. Union\n";
{
bool temp[256] = { 0 };
//printing elementsthat are present only in set 1
for (int i = 0; i < index[0]; i++) {
if (!temp[sets[0][i]]) { //checking if the element is flagged
temp[sets[0][i]] = !temp[sets[0][i]]; //flagging element and printing it
ofs << sets[0][i] << " ";
}
}
//printing elementsthat are present only in set 2
for (int i = 0; i < index[1]; i++) {
if (!temp[sets[1][i]]) { //checking if the element is flagged
temp[sets[1][i]] = !temp[sets[1][i]]; //flagging element and printing it
ofs << sets[1][i] << " ";
}
}
}
ofs << "\n";
ofs << "2. Intersection\n";
//to do Intersection of two sets
{
//flaging the elementsthat are in set 1
bool temp[256] = { 0 };
for (int i = 0; i < index[0]; i++) {
temp[sets[0][i]] = 1;
}
//traversing set 2 and printing only those elementsthat are flagged
for (int i = 0; i < index[1]; i++) {
if (temp[sets[1][i]]) {
ofs << sets[1][i] << " ";
}
}
}
ofs << "\n";
ofs << "3. A-B\n";
//to do A-B
{
//flaging the elementsthat are in set 1
bool temp[256] = { 0 };
for (int i = 0; i < index[1]; i++) {
temp[sets[1][i]] = 1;
}
//traversing set 2 and printing only those elementsthat are flagged
for (int i = 0; i < index[0]; i++) {
if (!temp[sets[0][i]]) {
ofs << sets[0][i] << " ";
}
}
}
ofs << "\n";
ofs << "4. Decide if (A is subsets of B)\n";
//to find if A is a subset of B
{
//flaging the elementsthat are in set 1
bool temp[256] = { 0 };
for (int i = 0; i < index[0]; i++) {
temp[sets[0][i]] = 1;
}
//traversing the set 2 and increasing counter for every element that was flagged
int cnt = 0;
for (int i = 0; i < index[1]; i++) {
if (temp[sets[1][i]]) {
cnt++;
}
}
//compparing counter with number of elements in set A
if (cnt == index[0]) {
ofs << "Yes, A is a subsets B";
}
else {
ofs << "No, A is not a subsets of B";
}
}
ofs.close();
return 0;
}
Program 2:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
char sets[3][1000]; //for 3 sets
int index[3] = { 0, 0, 0 }; //to keep track of index of each set
ifstream ifs("prog2 input.txt");
int ssets = 0;
//to read the elements from file
while (getline(ifs, line)) {
for (int i = 0; i < line.length(); i++) {
if (line[i] != ' ') {
sets[ssets][index[ssets]++] = line[i];
}
}
ssets++;
}
ifs.close(); //closing the input file
ofstream ofs;
ofs.open("prog2 output.txt", ios::trunc); //opening the output file
//Union (A u B) u C
ofs << "1. (A u B) u C\n";
{
bool temp[256] = { 0 };
//printing distinct elements of set A
for (int i = 0; i < index[0]; i++) {
if (!temp[sets[0][i]]) { //checking if the element is flagged
temp[sets[0][i]] = !temp[sets[0][i]]; //flagging element and printing it
ofs << sets[0][i] << " ";
}
}
//printing distinct elements of set B
for (int i = 0; i < index[1]; i++) {
if (!temp[sets[1][i]]) { //checking if the element is flagged
temp[sets[1][i]] = !temp[sets[1][i]]; //flagging element and printing it
ofs << sets[1][i] << " ";
}
}
//printing distinct elements of set C
for (int i = 0; i < index[2]; i++) {
if (!temp[sets[2][i]]) { //checking if the element is flagged
temp[sets[2][i]] = !temp[sets[2][i]]; //flagging element and printing it
ofs << sets[2][i] << " ";
}
}
}
ofs << "\n";
ofs << "2. A u (B n C)\n";
//A u (B n C)
{
bool temp[256] = { 0 };
bool temp1[256] = { 0 };
//flaging all elements of set B in temp array
for (int i = 0; i < index[1]; i++) {
temp[sets[1][i]] = 1;
}
//traversing set C and printing elementsthat are not flagged in temp
for (int i = 0; i < index[2]; i++) {
if (temp[sets[2][i]]) {
ofs << sets[2][i] << " ";
//flaging the printed elements in temp array
temp1[sets[2][i]] = 1;
}
}
//printing the elements of set A that are not flagged in temp
for (int i = 0; i < index[0]; i++) {
if (!temp1[sets[0][i]]) {
ofs << sets[0][i] << " ";
}
}
}
ofs << "\n";
ofs << "3. (A u B) n (A u C)\n";
//(A u B) n (A u C)
{
bool temp1[256] = { 0 };
char aUb[2000];
int iaUb = 0;
//stroing distinct elements (not present in B) from A in aUb array
for (int i = 0; i < index[0]; i++) {
if (!temp1[sets[0][i]]) {
temp1[sets[0][i]] = !temp1[sets[0][i]];
//ofs << sets[0][i] << " ";
aUb[iaUb++] = sets[0][i];
}
}
//stroing distinct elements (not present in A) from B in aUb array
for (int i = 0; i < index[1]; i++) {
if (!temp1[sets[1][i]]) {
temp1[sets[1][i]] = !temp1[sets[1][i]];
//ofs << sets[1][i] << " ";
aUb[iaUb++] = sets[1][i];
}
}
bool temp2[256] = { 0 };
char aUc[2000];
int iaUc = 0;
//stroing distinct elements (not present in B) from A in aUb array
for (int i = 0; i < index[1]; i++) {
if (!temp2[sets[0][i]]) {
temp2[sets[0][i]] = !temp2[sets[0][i]];
//ofs << sets[0][i] << " ";
aUc[iaUc++] = sets[0][i];
}
}
//stroing distinct elements (not present in A) from C in aUb array
for (int i = 0; i < index[2]; i++) {
if (!temp2[sets[2][i]]) {
temp2[sets[2][i]] = !temp2[sets[2][i]];
//ofs << sets[1][i] << " ";
aUc[iaUc++] = sets[2][i];
}
}
//printing the common elementsfrom arrays aUb and aUc to get (AuB)n(AuC)
bool temp[256] = { 0 };
for (int i = 0; i < iaUb; i++) {
temp[aUb[i]] = 1;
}
for (int i = 0; i < iaUc; i++) {
if (temp[aUc[i]]) {
ofs << aUc[i] << " ";
}
}
}
ofs.close();
return 0;
}
Output and input files: