Problem: Set Operations +-------------------------------+ Mathematically, a set
ID: 3762462 • Letter: P
Question
Problem: Set Operations
+-------------------------------+
Mathematically, a set is said to be consists of well-defined elements. For instance, the following are well-defined sets.
A = {1, 2, 3, A, 5, 6, 7, G}
B = {2, 3, 4, 8, 9, B}
A set may be described using other types of expression; specially, if the set has elements (members) that are numerous to list.
There are a well-defined operations that can be performed on sets.
The class should be called Operation
1. Determining a given set A is a subset of B, or not and set B is a subset of A, or not. (This should be a function called SubsetOfA and SubsetofB)
A is-a-subset-of B ==> false
B is-a-subset-of A ==> false
2. Union of sets: finds all elements of both A and B and generates a unique set. (This should be a function called UnionofAandB)
A union B = {1, 2, 3, 4, 5, 6, 7, 8, 9}
3. Intersection of sets: determines common elements that belong to both A and B. (This should be a function called IntersectionofAandB)
A intersection-of B = {2, 3, 4}
4. Complement of sets: given the following sets (This should be a function called ComplementAtoB and ComplementBtoA)
A = {1, 2, 3, 4} and B = {5, 2, 3, 8}
A - B = {1, 4} or B - A = {5, 8}
Tasks Requirement
+---------------------------+
1. You are to develop a "Set Operation Calculator" using C++ Object Oriented Programming. The cpp file should be name operationLastNameFirstName.cpp
2. Expressions are read from file call input.dat and their solutions are displayed on the display monitor and out to a file called output_LastNameFirstName.dat. (Hints: It could be letters too) Create a file with 2 line the first line is input for A and the second line should be the input for B
3. These operations must be supported: subset, union (+), intersection (^), and Complement (-). You must create 4 functions for each of the operations.
Example, Given these sets: A = {1, 2, 3, 4, F, Z} and B = {5, 2, 3, 8, Z}
A is-a-subset-of B ==> false
B is-a-subset-of A ==> false
A + B = {1, 2, 3, 4, 5, 8, F, Z}
A ^ B = {2, 3, Z}
A - B = {1, 4, F}
B - A = {5, 8}
so far this is my code.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Items {
string s;
Items() {
s = "";
}
Items(string st) {
s = st;
}
Items(char c) {
s = c;
}
public:
string getString() {
return s;
}
void setItem(string st) {
s = st;
}
void setItems(char c) {
s = c;
}
};
int Menu(vector<Items>, vector<Items>);
class Sets {
int length;
Sets() {
length = 0;
}
Sets(string st) {
length = st.length();
}
public:
////////////////////////////////////////////////////////////////////////////////////////////// break point before Sets function to help organize the code
void fillSet(string inputLine, vector<Items> A) {
int length = inputLine.length();
string intString = "";
int position = 0;
for (int i = 0; i < length; i++) {
if (inputLine[i] != ' ' || inputLine[i] != ',') {
if (inputLine[i] == '1') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '2') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '3') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '4') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '5') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '6') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '7') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '8') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '9') {
intString = intString + inputLine[i];
}
else if (inputLine[i] == '0') {
intString = intString + inputLine[i];
}
else {
if (intString == "") {
Items s();
char cha = inputLine[i];
string chas = cha + "";
s().setItem(chas);
A[position] = s();
position = position + 1;
}
else {
Items s();
s().setItem(intString);
intString = "";
Items d();
char cha = inputLine[i];
string chas = cha + "";
d().setItem(chas);
A[position] = s();
position = position + 1;
A[position] = d();
position = position + 1;
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////// break point after Sets function to help organize the code
void UnionofAandB(vector<Items> A, vector<Items> B) {
cout << "Union of A and B = { ";
for (unsigned int i = 0; i < A.size(); i++) {
cout << A[i].getString() << ", ";
}
for (unsigned int i = 0; i < B.size(); i++) {
cout << B[i].getString() << ", ";
}
cout << " } ";
}
void IntersectionofAandB(vector<Items> A, vector<Items> B) {
vector<Items> intersection;
for (unsigned int i = 0; i < A.size(); i++) {
for (unsigned int j = 0; j < B.size(); j++) {
if (A[i].getString() == B[j].getString()) {
intersection.push_back(A[i]);
}
}
}
cout << "Intersection of A and B = { ";
for (unsigned int i = 0; i < intersection.size(); i++) {
cout << intersection[i].getString() << ", ";
}
cout << " } ";
}
void ComplementAtoB(vector<Items> A, vector<Items> B) {
vector<Items> complement;
for (unsigned int i = 0; i < A.size(); i++) {
for (unsigned int j = 0; j < B.size(); j++) {
if (A[i].getString() != B[j].getString()) {
complement.push_back(A[i]);
i = i + 1;
}
}
}
cout << "Complement of A to B = {";
for (unsigned int i = 0; i < complement.size(); i++) {
cout << complement[i].getString() << ", ";
}
cout << " } ";
}
void ComplementBtoA(vector<Items> B, vector<Items> A) {
vector<Items> complement;
for (unsigned int i = 0; i < B.size(); i++) {
for (unsigned int j = 0; j < A.size(); j++) {
if (B[i].getString() != A[j].getString()) {
complement.push_back(B[i]);
i = i + 1;
}
}
}
cout << "Complement of B to A = {";
for (unsigned int i = 0; i < complement.size(); i++) {
cout << complement[i].getString() << ", ";
}
cout << " } ";
}
void SubsetOfA(vector<Items> A, vector<Items> B) {
vector<Items> subsetA;
for (unsigned int i = 0; i < A.size(); i++) {
for (unsigned int j = 0; j < B.size(); j++) {
if (A[i].getString() == B[i].getString()) {
subsetA.push_back(A[i]);
}
}
}
bool subbool;
for (unsigned int i = 0; i < subsetA.size(); i++) {
if (subsetA.size() == A.size()) {
if (subsetA[i].getString() == A[i].getString()) {
subbool = true;
}
else {
subbool = false;
i = subsetA.size();
}
}
else {
subbool = false;
i = subsetA.size();
}
}
cout << "A is-a-subset-of B ==> " << subbool << endl;
}
void SubsetOfB(vector<Items> B, vector<Items> A) {
vector<Items> subsetA;
for (unsigned int i = 0; i < A.size(); i++) {
for (unsigned int j = 0; j < B.size(); j++) {
if (A[i].getString() == B[i].getString()) {
subsetA.push_back(A[i]);
}
}
}
bool subbool;
for (unsigned int i = 0; i < subsetA.size(); i++) {
if (subsetA.size() == A.size()) {
if (subsetA[i].getString() == A[i].getString()) {
subbool = true;
}
else {
subbool = false;
i = subsetA.size();
}
}
else {
subbool = false;
i = subsetA.size();
}
}
cout << "B is-a-subset-of A ==> " << subbool << endl;
}
};
int main() {
vector<Items> A;
vector<Items> B;
string s1;
string s2;
ifstream input;
input.open("input.dat");
getline(input, s1);
getline(input, s2);
Sets set();
set().fillSet(s1, A);
set().fillSet(s2, B);
for (int i = 0; i < 1;) {
i = Menu(A, B);
}
system("pause");
return 0;
}
int Menu(vector<Items> a, vector<Items> b) {
Sets set();
cout << " 1) subsets. 2) Union. 3) Intersection. 4) Complements. 5) Exit. " << "Select an option by typing in the number of your choice : ";
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << " 1) Subset of A. 2) Subset of B. " << "Select an option by typing in the number of your choice : ";
int secChoice;
cin >> secChoice;
if (secChoice == 1) {
set().SubsetOfA(a, b);
}
else if (secChoice == 2) {
set().SubsetOfB(b, a);
}
else {
cout << "improper choice!!! Returning to main menu. ";
}
break;
case 2:
set().UnionofAandB(a, b);
break;
case 3:
set().IntersectionofAandB(a, b);
break;
case 4:
cout << " 1) Complements A to B. 2) Complements B to A. " << "Select an option by typing in the number of your choice : ";
cin >> secChoice;
if (secChoice == 1) {
set().ComplementAtoB(a, b);
}
else if (secChoice == 2) {
set().ComplementBtoA(b, a);
}
else {
cout << "improper choice!!! Returning to main menu. ";
}
break;
case 5:
return 1;
break;
default:
cout << "improper choice!!! Returning to main menu. ";
break;
}
return 0;
}
Explanation / Answer
question & answer is in the below link.click on lnk and njoyy
i hope this will help to u
https://social.msdn.microsoft.com/Forums/en-US/7a3aaaa7-fba3-41c3-a1ba-e06a69003388/hello-i-have-a-c-question-any-help-would-be-appreciated?forum=vclanguage
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.