project in c++ Define & implement a class for Set to store a collection of Disti
ID: 3628847 • Letter: P
Question
project in c++Define & implement a class for Set to store a collection of Distinct strings.
Class name: Set. You should declare the class in set.h, and implement the class in set.cpp
Data Members:
you can use dynamic array (or linked list) to represent the set. You should also have a member that keeps the number of elements in the set.
Member Functions:
constructors: default constructor, copy-constructor and other constructors you want
cardinality: which returns the number of elements in the set
isMember(string s): which test if s appears in the set
set_union(Set otherSet): returns a set that contains all distinct elements from this set and otherSet
set_intersection(Set otherSet): return a set that contains elements that appear in both this set and otherSet
insert_element (which will inset an element to the set, if the element is not already in the set)
destructor: release the memory that has been dynamically allocated
assignment operator overload the operator so you can assign one set to another
operator << overload the operator so that it can be used to output sets to an output stream.
operator >> (this is optional) overload the operator so that it can be used to input sets from any input stream, say a file input stream or cin.
Test your class:
(1) Create set S1 which contains all distinct words from set.h, if you overload operator >>, use it
(2) Create set S2 which contains all distinct words from set.cpp, if you overload operator >>, use it
(3) Let S3 be the union of S1 and S2
(4) Let S4 intersection of S1 and S2
(5) Output the cardinalities of S1, S2, S3 and S4
(6) Output the sets S1, S2, S3 and S4, using the overloaded operator.
you can see this URL includes a solved project maybe has repleted with this question . So maybe we need to do it with different set.
......................
http://www.cramster.com/answers-sep-11/computer-science/write-program-write-program-count-number-distinct-words_1442316.aspx?rec=0
.......................
Explanation / Answer
Set.hpp:
#ifndef SET_HPP
#define SET_HPP
#include <string>
#include <sstream>
using namespace std;
class Set {
private:
unsigned int size;
unsigned int capacity;
string* elements;
void realloc(unsigned int);
public:
Set();
Set(const unsigned int);
Set(const Set&);
~Set();
Set& operator =(const Set& s);
const unsigned int cardinality() const;
void insert_element(const string);
const bool contains(const string) const;
Set* set_union(const Set&) const;
Set* set_intersection(const Set&) const;
friend ostream& operator <<(ostream&, const Set);
};
ostream& operator <<(ostream&, const Set);
istream& operator >>(istream&, Set&);
#endif /* SET_HPP */
Set.cpp:
#include "Set.hpp"
Set::Set() {
capacity = 0;
size = 0;
elements = NULL;
}
Set::Set(const unsigned int capacity) {
size = 0;
this->capacity = capacity;
elements = new string[capacity];
}
Set::Set(const Set& s) {
size = s.size;
capacity = s.capacity;
elements = new string[capacity];
for (unsigned int i = 0; i < s.size; i++) {
elements[i] = s.elements[i];
}
}
Set::~Set() {
delete [] elements;
}
Set& Set::operator =(const Set& s) {
if (this == &s) {
return *this;
}
if (elements) {
delete [] elements;
}
size = s.size;
capacity = s.capacity;
elements = new string[capacity];
for (unsigned int i = 0; i < s.size; i++) {
elements[i] = s.elements[i];
}
return *this;
}
const unsigned int Set::cardinality() const {
return size;
}
void Set::realloc(const unsigned int new_capacity) {
string* new_elements = new string[new_capacity];
for (unsigned int i = 0; i < size; i++) {
new_elements[i] = elements[i];
}
delete [] elements;
elements = new_elements;
capacity = new_capacity;
}
const bool Set::contains(const string element) const {
for (unsigned int i = 0; i < size; i++) {
if (elements[i] == element) {
return true;
}
}
return false;
}
void Set::insert_element(const string element) {
if (size < 1) {
realloc(1);
}
if (size == capacity) {
realloc(size * 2);
}
if (!contains(element)) {
elements[size] = element;
size++;
}
}
Set* Set::set_union(const Set& other) const {
Set* r = new Set(*this);
for (unsigned int i = 0; i < other.size; i++) {
r->insert_element(other.elements[i]);
}
return r;
}
Set* Set::set_intersection(const Set& other) const {
Set* r = new Set();
for (unsigned int i = 0; i < size; i++) {
if (other.contains(elements[i])) {
r->insert_element(elements[i]);
}
}
return r;
}
ostream& operator <<(ostream& os, const Set s) {
os << string("{");
for (unsigned int i = 0; i < s.size; i++) {
os << s.elements[i];
if (i < s.size - 1) {
os << string(", ");
}
}
os << string("}");
return os;
}
istream& operator >>(istream& is, Set& s) {
stringstream ss;
char c;
string str;
while (is.good()) {
c = is.get();
if (c == ' ' || c == ' ' || c == ' ') {
str = ss.str();
ss.str(string());
if (str.size() > 0) {
s.insert_element(str);
}
} else {
ss << c;
}
}
}
main.cpp:
#include <iostream>
#include <fstream>
#include "Set.hpp"
using namespace std;
int main() {
Set s1, s2, *s3, *s4;
ifstream s1file("Set.hpp");
ifstream s2file("Set.cpp");
s1file >> s1;
s2file >> s2;
s1file.close();
s2file.close();
s3 = s1.set_union(s2);
s4 = s1.set_intersection(s2);
cout << "s1: " << s1 << ' ' << endl;
cout << "s2: " << s2 << ' ' << endl;
cout << "s3: " << *s3 << ' ' << endl;
cout << "s4: " << *s4 << ' ' << endl;
delete s3;
delete s4;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.