Question: Discrete Structures(I have 2 TODO in bold) please can you complete the code for me. thanks. Set.h #include #include #include #include #include using namespace std;
Discrete Structures(I have 2 "TODO" in bold) please can you complete the code for me.
thanks.
Set.h
#include
#include
#include
#include
#include
using namespace std;
class Set
{
int capacity; // Capacity of the set, e.g. max number of available elements
int size; // size of the set
string *elements; // Pointer to a string array, used to implement this set
public:
Set(int cap = 100);
~Set();
bool isFull(void); // Check whether this set reaches its capacity
bool isEmpty(void); // Check whether this set is empty
int find(string v); // find the index of a specific string in this set
void insert(string v); // Insert a single element to this set
void scan(istream &i); // Read the elements from a input stream
void print(ostream &o, const char *msg); // Print all the elements of this set
};
p02.cpp
#include
#include
#include
#include
#include
#include "Set.h"
#ifndef MAX_FILE_LENGTH
#define MAX_FILE_LENGTH 255
#endif // MAX_FILE_LENGTH
using namespace std;
/**
* Read the input set from the input stream, and output the subsets
* to the output stream.
*/
void get_subsets(istream &i1, ostream &o)
{
Set set1;
set1.scan(i1); // read the set from the input file
set1.print(o, "A="); // print the set to the output file
/* Please put your code here. */
// TODO: get all the subsets of the input set and print the result
}
/*
* This program accepts three command line arguments:
* argv[1] is for the path to input file
* argv[2] is for the path to the output file
*/
int main(int argc, char *argv[])
{
char file1[MAX_FILE_LENGTH], file2[MAX_FILE_LENGTH];
// file1 is for the input, file2 is for the output
strcpy(file1, argv[1]);
strcpy(file2, argv[2]);
ifstream i1(file1);
ofstream o(file2);
if (i1.is_open() && o.is_open())
{
get_subsets(i1, o);
i1.close();
o.close();
}
else
{
cout
}
}
Set.cpp
using namespace std;
#include "Set.h"
/*
* This exception is thrown when the status of this list is invalid.
*/
struct SetException
{
SetException(const char *m)
{
cout
cout
cout
}
};
/*
* Constructor
*/
Set::Set(int cap) : capacity(cap), size(0)
{
elements = new string[capacity];
}
/*
* Destructor
*/
Set::~Set()
{
if (elements)
{
delete[] elements;
}
}
/*
* Check whether this list is full.
*/
bool Set::isFull(void)
{
return size >= capacity - 1;
}
/*
* Check whether this list is empty.
*/
bool Set::isEmpty(void)
{
return size
}
/**
* Check whether a string target v is contained in this set.
* If found, return the index of this string. Otherwise return -1.
*/
int Set::find(string v)
{
for (int idx = 0; idx
{
if (v.compare(elements[idx]) == 0)
{
return idx;
}
}
return -1;
}
/*
* Insert a string v into the list.
* If string v has already been saved in this set, no need to add it again. This
* means no duplicates in this set.
* SetException would be thrown if the list is full.
*/
void Set::insert(string v)
{
if (isFull())
{
throw SetException("This set is full!");
}
/* Please put your code here. */
// TODO: if no duplicate check is needed, we can use "elements[size++] = v;"
elements[size++] = v;
}
/*
* Scan the input stream, and insert all the strings in this
* stream into the list.
*/
void Set::scan(istream &i)
{
string v;
while (i >> v)
{
insert(v);
}
}
/*
* Display all the string elements in this list.
*/
void Set::print(ostream &o, const char *msg)
{
o
for (int idx = 0; idx
{
if (idx > 0)
{
o
}
o
}
o
}
instruction:

Another example for input.dat could be (pay attention to the large white spaces in the last line): 123 345 123 345 543 2.4 Output Specification The output file consists of all the subsets created based on the input file. Elements of the set are enclosed in curly brackets, i.e., { }, and separated by commas. If the result is an empty set, just put nothing in the braces. Please also print the set read from input file as the first line in the output file. Here no duplicates should be involved. And then print the result of subsets starting from the second line. And at the end print the total number of subsets. Do you think you can predict this total number after reading the input file, instead of counting the result? An example for output.dat could be: A={123, 345, 543}. Subsets of A: {}, {123}, {345}, {543}, {123,345}, {123,543}, { 345,543}, {123,345,543}. There are 8 subsets of A
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
