In: Computer Science
Write a C++ 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 – 50 points 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.
Program 1:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
char sets[2][1000];
int index[2] = { 0, 0 };
ifstream ifs("prog1 input.txt");
int ssets = 0;
while (getline(ifs, line)) {
for (int i = 0; i < line.length(); i++) {
if (line[i] != ' ') {
sets[ssets][index[ssets]++] = line[i];
}
}
ssets++;
}
ifs.close();
ofstream ofs;
ofs.open("prog1 output.txt", ios::trunc);
//Union
ofs << "1. Union\n";
{
bool temp[256] = { 0 };
for (int i = 0; i < index[0]; i++) {
if (!temp[sets[0][i]]) {
temp[sets[0][i]] = !temp[sets[0][i]];
ofs << sets[0][i] << " ";
}
}
for (int i = 0; i < index[1]; i++) {
if (!temp[sets[1][i]]) {
temp[sets[1][i]] = !temp[sets[1][i]];
ofs << sets[1][i] << " ";
}
}
}
ofs << "\n";
ofs << "2. Intersection\n";
//Intersection
{
bool temp[256] = { 0 };
for (int i = 0; i < index[0]; i++) {
temp[sets[0][i]] = 1;
}
for (int i = 0; i < index[1]; i++) {
if (temp[sets[1][i]]) {
ofs << sets[1][i] << " ";
}
}
}
ofs << "\n";
ofs << "3. A-B\n";
//A-B
{
bool temp[256] = { 0 };
for (int i = 0; i < index[1]; i++) {
temp[sets[1][i]] = 1;
}
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";
//A is subsets of B
{
bool temp[256] = { 0 };
for (int i = 0; i < index[0]; i++) {
temp[sets[0][i]] = 1;
}
int cnt = 0;
for (int i = 0; i < index[1]; i++) {
if (temp[sets[1][i]]) {
cnt++;
}
}
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];
int index[3] = { 0, 0, 0 };
ifstream ifs("prog2 input.txt");
int ssets = 0;
while (getline(ifs, line)) {
for (int i = 0; i < line.length(); i++) {
if (line[i] != ' ') {
sets[ssets][index[ssets]++] = line[i];
}
}
ssets++;
}
ifs.close();
ofstream ofs;
ofs.open("prog2 output.txt", ios::trunc);
//Union (A u B) u C
ofs << "1. (A u B) u C\n";
{
bool temp[256] = { 0 };
for (int i = 0; i < index[0]; i++) {
if (!temp[sets[0][i]]) {
temp[sets[0][i]] = !temp[sets[0][i]];
ofs << sets[0][i] << " ";
}
}
for (int i = 0; i < index[1]; i++) {
if (!temp[sets[1][i]]) {
temp[sets[1][i]] = !temp[sets[1][i]];
ofs << sets[1][i] << " ";
}
}
for (int i = 0; i < index[2]; i++) {
if (!temp[sets[2][i]]) {
temp[sets[2][i]] = !temp[sets[2][i]];
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 };
for (int i = 0; i < index[1]; i++) {
temp[sets[1][i]] = 1;
}
for (int i = 0; i < index[2]; i++) {
if (temp[sets[2][i]]) {
ofs << sets[2][i] << " ";
temp1[sets[2][i]] = 1;
}
}
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;
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];
}
}
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 bUc[2000];
int ibUc = 0;
for (int i = 0; i < index[1]; i++) {
if (!temp2[sets[1][i]]) {
temp2[sets[1][i]] = !temp2[sets[1][i]];
//ofs << sets[0][i] << " ";
bUc[ibUc++] = sets[1][i];
}
}
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] << " ";
bUc[ibUc++] = sets[2][i];
}
}
bool temp[256] = { 0 };
for (int i = 0; i < iaUb; i++) {
temp[aUb[i]] = 1;
}
for (int i = 0; i < ibUc; i++) {
if (temp[bUc[i]]) {
ofs << bUc[i] << " ";
}
}
}
ofs.close();
return 0;
}
Output and input files: