Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Fisher's Assignment 15
- *NOTES*
- - Make sure to add comments and change file locations
- - Each division represents a different file, be sure to keep types
- ______________________________Set.cpp___________________________________
- #include <iostream>
- #include <sstream>
- #include "d:\assignment15\set.h"
- using namespace std;
- Set :: Set()
- {
- len = 0;
- }
- Set :: Set(int arr[], int hm)
- {
- len = hm;
- for (int x = 0; x < len; x++)
- {
- if (!in(arr[x]))
- val[x] = arr[x];
- else
- val[x] = -99;
- }
- }
- int Set :: getLength()
- {
- return len;
- }
- int Set :: getValue(int n)
- {
- return val[n];
- }
- bool Set :: in(int n)
- {
- for (int x = 0; x < len; x++)
- if (n == val[x])
- return true;
- return false;
- }
- void Set :: fill(int arr[], int hm)
- {
- len = hm;
- for (int x = 0; x < hm; x++)
- {
- if (!in(arr[x]))
- val[x] = arr[x];
- else
- val[x] = -99;
- }
- }
- void Set :: add(int n)
- {
- if (!in(n))
- {
- val[len] = n;
- len ++;
- }
- }
- void Set :: remove(int n)
- {
- if (in(n))
- {
- int c;
- for (int x = 0; x < len; x++)
- if (val[x] == n)
- {
- val[x] = -99;
- c = x + 1;
- }
- for (int y = c; y < len; y++)
- val[y-1] = val[y];
- len --;
- }
- }
- string Set :: toString()
- {
- stringstream s;
- s << "{";
- for (int x = 0; x < len; x++)
- {
- if (val[x] != -99)
- s << val[x];
- if (((x+1) < len) && (val[x+1] != -99))
- s << ", ";
- }
- s << "}";
- return s.str();
- }
- Set intersect(Set a, Set b)
- {
- int val[30], len = 0;
- for (int x = 0; x < a.getLength(); x++)
- if (b.in(a.getValue(x)))
- {
- val[len] = a.getValue(x);
- len ++;
- }
- Set i(val, len);
- return i;
- }
- Set getUnion(Set a, Set b)
- {
- Set i;
- for (int x = 0; x < a.getLength(); x++)
- i.add(a.getValue(x));
- for (int x = 0; x < b.getLength(); x++)
- i.add(b.getValue(x));
- return i;
- }
- Set difference(Set a, Set b)
- {
- Set i;
- for (int x = 0; x < a.getLength(); x++)
- if (!b.in(a.getValue(x)))
- i.add(a.getValue(x));
- return i;
- }
- _______________________________Set.h______________________________________
- #ifndef set_h
- #define set_h
- using namespace std;
- class Set
- {
- public:
- Set();
- Set(int arr[], int);
- int getLength();
- int getValue(int);
- bool in(int);
- void fill(int arr[], int);
- void add(int);
- void remove(int);
- string toString();
- private:
- int val[30];
- int len;
- };
- #endif
- Set intersect(Set, Set);
- Set getUnion(Set, Set);
- Set difference(Set, Set);
- _______________________________________main.cpp_____________________________
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- #include "d:\assignment15\set.h"
- #include "d:\assignment15\set.cpp"
- using namespace std;
- struct Input
- {
- Set a, b, c;
- };
- Input input();
- void output(Set, Set, Set);
- int main( )
- {
- Input i = input();
- output(i.a, i.b, i.c);
- system("PAUSE");
- return EXIT_SUCCESS;
- }
- Input input()
- {
- ifstream in;
- //in.open("s:\\computer science ii\\setData-test.txt");
- in.open("d:\\assignment15\\setData-test.txt");
- int n1, n2, n3, val1[20], val2[20], val3[20], val;
- in >> n1;
- for (int x = 0; x < n1; x++)
- {
- in >> val;
- val1[x] = val;
- }
- Set a(val1, n1);
- in >> n2;
- for (int x = 0; x < n2; x++)
- {
- in >> val;
- val2[x] = val;
- }
- Set b(val2, n2);
- in >> n3;
- for (int x = 0; x < n3; x++)
- {
- in >> val;
- val3[x] = val;
- }
- Set c(val3, n3);
- Input i;
- i.a = a;
- i.b = b;
- i.c = c;
- return i;
- }
- void output(Set a, Set b, Set c)
- {
- cout << "Set 1: " << a.toString() << endl;
- cout << "Set 2: " << b.toString() << endl;
- cout << "Set 3: " << c.toString() << endl;
- cout << endl;
- cout << "The intersection of 1 & 2: " << intersect(a, b).toString() <<endl;
- cout << "The intersection of 2 & 3: " << intersect(b, c).toString() <<endl;
- cout << "The intersection of 1 & 3: " << intersect(a, c).toString() <<endl;
- cout << endl;
- cout << "The union of 1 & 2: " << getUnion(a, b).toString() << endl;
- cout << "The union of 2 & 3: " << getUnion(b, c).toString() << endl;
- cout << "The union of 1 & 3: " << getUnion(a, c).toString() << endl;
- cout << endl;
- cout << "The difference of 1 & 2: " << difference(a, b).toString() << endl;
- cout << "The difference of 2 & 1: " << difference(b, a).toString() << endl;
- cout << "The difference of 2 & 3: " << difference(b, c).toString() << endl;
- cout << "The difference of 3 & 2: " << difference(c, b).toString() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement