COS30008 Data Structures andPatterns - Assignment 02 Data Structures and Patterns in C++Component Tasks Total Marks Student Marks Date Checked
1. Assignment Overview
Assignment Deadline: 16 Dec 2022 (subject to change, please confirm this with your co-teacher)Assignment Submission: Please discuss with your co-teacher about how and what to submit. This assignment has two components:Problem Sets (10% in total)Problem Sets 3 and 4 aredesigned to extend your work on Labs 07 and 13, respectively. 21. Problem Set 3 (4%) extends your work on Lab 07. In Lab 07, we develop an object adapter pattern. Here, we extend the work so that the application can work on a buffer of bytes (e.g. 128 bytes) at a time.2. Problem Set 4 (6%) extends your work on Lecture and Lab 13. In Lab 13, we deal with Stack and Queue. Here we extend our work so that we can handle Priority Queue.Conditional Compilation We will not be using conditional compilation in the following two
2.Problem Sets.
Problem Set 3 Object Adapter with a Buffer (4%) Getting the files ready1. Download and Expand A02_src_adapter.zip from Cloud Campus
This zip file contains the following three groups of files:2. Create a project, named A2_Adapter (or Adapter, or something else of your choosing), either in Visual Studio (MS Windows) or Xcode (Mac OSX), depending on your machine3. Add the following C++ files in the project first a. Main.cppPlease be very careful in modifying any of these given files as instructed in the relevant tasks.The new specification of class ofstream12In order to add support for a larger buffer size of characters, we use the following revisedspecification of class ofstream12 in ofstream12.h, which is shown below Revised specification of class ofstream12 [code segment]m>2. CPP file: Main.cpp and ofstream12.cpp3. Sample output files: mySample4096.lzw and mySample0171.lzw - for you to check your work
#define MAX_BITS 8
using byte = unsigned char;
class ofstream12
{
private:
std::ofstream fOStream;
// revised for A2
byte* fBuffer;
size_t fBufferSize;
size_t fByteIndex;
int fBitIndex;
void init();
index
void prepareOneByte();
void prepareBit0();
void prepareBit1();
void writeBuffer();
// output buffer
// output buffer size, default is 128
// current byte index, starts at 0
// current bit index (can be negative), starts at 0
// initialize buffer, buffer size, byte index and bit
// prepare one byte in the buffer
// store bit 0 to the appropriate byte in the buffer
// store bit 1 to the appropriate byte in the buffer
// output entire buffer
public:
// using C++11's nullptr
ofstream12(const char* aFileName = nullptr, size_t aBufferSize = 128);
~ofstream12();
void close();
bool good() const;
bool isOpen() const;
void flush();
ofstream12& operator<<(size_t aValue);
};
The implementation of the following methods have been given to you in ofstream12.cpp as is (Please do not modify these methods):1. the constructor ofstream12() and the destructor ~ofstream12() methods 2. the isOpen() method3. the prepareBit0() and prepareBit1() methods4. the operator<<() method4
3.What you need to do
Program the following six methods in ofstream12.cpp a. the good() methodThis method checks whether the output stream is good foroutput. Which method in the std::ofstream class can help?b. the init() method
This method initializes the characters stored in fBuffer to \0 (null character) and set the relevant byte index (fByteIndex) and bit index(fBitIndex) values.c. the flush() methodThis method flushes all remaining bytes and bits in the buffer to the output file.d. the close() methodThis method closes the output file. But, before the closing of the file, you need to flush all buffer out first.e. the writeBuffer() method2. Use the following code segment in yourMain.cpp to test your solution Code segment in Main.cpp
void write4096(const char* aFilename)
{
cout << "Write 4096 codes from 4095 to 0" << endl;
ofstream12 lWriter(aFilename);
if (!lWriter.good())
{
cerr << "Error: Unable to open output file " << aFilename << " !" << endl;
exit(1);
}
}
This method writes the entire buffer to the output file. f. the prepareOneByte() methodThis method prepares one complete byte in the buffer. When the buffer is full, you need to write the entire buffer
void write0171(const char* aFilename)
{
cout << "Write 171 codes from 4095 to 3925" << endl;
ofstream12 lWriter(aFilename);
if (!lWriter.good())
{
cerr << "Error: Unable to open output file " << aFilename << " !" << endl;
exit(1); }
for (size_t i = 4096; i > 3925; )
{
lWriter << --i;
}
}
int main() {
write4096("sample4096.lzw");
write0171("sample0171.lzw");
cout << "SUCCESS" << endl;
return 0;
}
The outputs should be similar to the relevant files as shown below
4.Output Files
There are two sample output files that you can use to check your solution: mySample4096.lzw and mySample0171.lzw.1. The mySample4096.lzw is the same as your Lab 07’s standard output file. You can view this using any binary editor. It has 6144 (= 4096 * 12 / 8) bytes2. The mySample0171.lzw is a new one. This is to check whether you are careful when there is some extra remaining bits that need to be output. For 171 12-bits, it is equivalent to 256.5 (= 171 x 12 / 8) 8-bits. This means that after two rounds of writeBuffer() assuming buffer size is 128, there are 4 remaining bits (0.5 bytes) in the system waiting to be output. It has 257 bytes. Remember,In Lecture 13, we use the Pair<K,V> as the data type for thePriorityQueue class where K stands for 9the key in the Pair and V stands for the value. The K was used in sorting the items in the PriorityQueue or SortedList. In fact, it is possible to just use a particular field in a class to perform the sorting. In this problem set, we do this using just the Product class. The relevant methods have been coded in the Product.cpp file for you.Here is your assignment tasks in this problem set.1. Program the following methods in SortedList.h (3%, 1% each)a. the insert(const T&) method
This method inserts an object of type T to the SortedList in the proper position so that the list is sorted in ascending order from smallest to largest. Since the SortedList is in the form of a SinglyLinkedNode, you need to create the relevant memory location using the new operator from the actual data and manage the links properly. Last, but not least, remember to update fCount.
b. the remove(const T&) methodhis method has the following complications:1. It removes the first object of type T found in the SortedList.2.It throws a no_such_element_error exception (defined in
NoSuchElementException.h) if it cannot find the object.3.It throws an underflow_error exception (defined instd::underflow_error) if the list is empty. c. the list indexer operator[](size_t aIndex) method
This method returns an object of type T using the index aIndex from the smallest item to the largest item. The calling module is using fElements[aIndex] to call this method, so fElements[0] is the smallest item in the SortedList. Please remember to throw an out_of_range exception (defined in std::out_of_range) if aIndex is out of the range of fElements.2. Program the following methods in PriorityQueue.h (3%, 1% each)3. Use the following code segment in your Main.cpp to test your solution Code segment in Main.cpp
int main() {
PriorityQueue<Product> lPQueue;
Product lProduct0("000000", "Cooler Power Supply", 200, 299.99);
Product lProduct1("000001", "Intel 12-th Gen i7 CPU", 50, 599.99f);
Product lProduct2("000002", "Kingston DDR4 16GB RAM", 100, 119.99f);
Product lProduct3("000003", "Seagate 2TB NVMe SSD", 150, 399.99f);
cout << "Adding lProduct0 to the priority queue: " << lProduct0;
lPQueue.enqueue(lProduct0);
cout << "Adding lProduct1 to the priority queue: " << lProduct1;
lPQueue.enqueue(lProduct1);
cout << "Adding lProduct2 to the priority queue: " << lProduct2;
lPQueue.enqueue(lProduct2);
cout << "Adding lProduct3 to the priority queue: " << lProduct3;
lPQueue.enqueue(lProduct3);
// correct result should be lProduct2 < lProduct0 < lProduct3 < lProduct1
cout << "Correct order of the priority queue is lProduct2 < lProduct0 <
lProduct3 < lProduct1" << endl;
cout << "Number of elements in the priority queue: " << lPQueue.size() << endl;
cout << "Remove the top 3 elements from the priority queue (lProduct1,
lProduct3, lProduct0) ..." << endl;
cout << lPQueue.top();
lPQueue.dequeue();
cout << lPQueue.top();
lPQueue.dequeue();
cout << lPQueue.top();
lPQueue.dequeue();
cout << "Number of elements remaining in the priority queue: " << lPQueue.
size() << endl;
cout << "The last element in the priority queue (lProduct2): " << lPQueue.top()
<< endl;
return 0;
11
}
The output should be similar to the one shown belowFigure 4. Output of Main.cpp for Problem Set 4Marking Criteria of Problem Set 4
Below is the marking criteria of Problem Set 4a. Correct implementation of the methods in SortedList and PriorityQueue including correct results - 6%b. If program cannot compile due to some errors, award 0% - 5% depending on how serious the problems are.
WX:codehelp
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。