Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below are the code for my workshop I just need a little help to get right output. w3p2.cpp // Workshop 3 - Templates // Created

Below are the code for my workshop I just need a little help to get right output.

w3p2.cpp

// Workshop 3 - Templates // Created by: Jeevan Pant // Date created: 2023/07/22

// Dont make any changes to this file

#include #include #include #include #include "Book.h" #include "Book.h" #include "Collection.h" #include "Collection.h" #include "OrderedCollection.h" #include "OrderedCollection.h"

int cout = 0; int endl = 0;

int main(int argc, char** argv) { std::cout << "Command Line arguments: "; std::cout << "--------------------------------- "; for (int i = 0; i < argc; i++) std::cout << std::setw(3) << i + 1 << ": " << *(argv + i) << ' '; std::cout << "--------------------------------- ";

// int, double, and Book data testing templated classes unsigned numBooks = 6; int ints[]{ 25, 30, 20, 15, 25, 5, 35 }; double doubles[]{ 7.8, 9.10, 3.4, 1.2, 2.3, 6.7 }; sdds::Book books[]{ sdds::Book("Software Architecture for Busy Developers", 7, 174), sdds::Book("Low-Code Application Development with Appian",16,462), sdds::Book("Natural Language Processing with TensorFlow", 11, 514), sdds::Book("Learning DevOps",17,560), sdds::Book("SQL for Data Analytics",9,540), sdds::Book("Deciphering Object-Oriented Programming with C++",21,594), };

std::cout << "1. Book tester >>> ===========================================================" << std::endl; std::cout << "total books: " << numBooks << std::endl; std::cout << "------------------------------------------------------------------------------" << std::endl; for (auto i = 0u; i < numBooks - 1; i++) { std::cout << "| " << i + 1 << "."; books[i].print(std::cout) << "|" << std::endl;; } std::cout << "| " << numBooks << "."; std::cout << books[numBooks - 1] << "|" << std::endl; std::cout << "============================================================== <<< Book tester" << std::endl << std::endl;

std::cout << "2. Collection tester for int and 10 >>> ======================================" << std::endl; { // Collection tester for int-type sdds::Collection icol; for (auto i : ints) icol += i; std::cout << "(smallest,largest) items: (" << icol.getSmallestItem() << "," << icol.getLargestItem() << ")" << std::endl; std::cout << "size/capacity: " << icol.size() << "/" << icol.capacity() << std::endl; std::cout << "Contents: "; std::cout << "["; icol.print(std::cout); std::cout << "]"; } std::cout << std::endl; std::cout << "========================================= <<< Collection tester for int and 10" << std::endl; std::cout << std::endl;

std::cout << "3. Collection tester for double and 10 >>>====================================" << std::endl; {// Collection tester for double-type sdds::Collection dcol; for (auto d : doubles) dcol += d; std::cout << "(smallest,largest) items: (" << dcol.getSmallestItem() << "," << dcol.getLargestItem() << ")" << std::endl; std::cout << "size/capacity: " << dcol.size() << "/" << dcol.capacity() << std::endl; std::cout << "Contents: "; std::cout << "["; dcol.print(std::cout); std::cout << "]"; } std::cout << std::endl; std::cout << "====================================== <<< Collection tester for double and 10" << std::endl; std::cout << std::endl;

std::cout << "4. Collection tester for Book and 10 >>> =====================================" << std::endl; {// Collection tester for Book-type, capacity 10 std::cout << "[After creating collection in empty state]" << std::endl; sdds::Collection bcol; std::cout << "Book with smallest pages-to-chapters ratio (initial-default state): " << std::endl; bcol.getSmallestItem().print(std::cout) << std::endl; std::cout << "Book with largest pages-to-chapters ratio (initial-deault-state): " << std::endl; bcol.getLargestItem().print(std::cout) << std::endl; std::cout << "size/capacity: " << bcol.size() << "/" << bcol.capacity() << std::endl; std::cout << std::endl;

// Part 2 tester follows from here

std::cout << "[After adding Six books to the collection]" << std::endl; for (auto& b : books) bcol += b; std::cout << "Book with smallest pages-to-chapters ratio: " << std::endl; bcol.getSmallestItem().print(std::cout) << std::endl; std::cout << "Book with largest pages-to-chapters ratio: " << std::endl; bcol.getLargestItem().print(std::cout) << std::endl; std::cout << "size/capacity: " << bcol.size() << "/" << bcol.capacity() << std::endl; std::cout << std::endl; std::cout << "Collection content: "; bcol.print(std::cout); } std::cout << "======================================== <<< Collection tester for Book and 10" << std::endl; std::cout << std::endl;

std::cout << "5. OrderedCollection tester for int >>> ===========================================" << std::endl; { // Collection tester for int-type sdds::OrderedCollection oicol; for (auto i : ints) oicol += i; std::cout << "(smallest,largest) items: (" << oicol.getSmallestItem() << "," << oicol.getLargestItem() << ")" << std::endl; std::cout << "size/capacity: " << oicol.size() << "/" << oicol.capacity() << std::endl; std::cout << "Contents: "; oicol.print(std::cout); } std::cout << "============================================= <<< OrderedCollection tester for int " << std::endl; std::cout << std::endl;

std::cout << "6. OrderedCollection tester for double >>> ========================================" << std::endl; {// Collection tester for double-type sdds::OrderedCollection odcol; for (auto d : doubles) odcol += d; std::cout << "(smallest,largest) items: (" << odcol.getSmallestItem() << "," << odcol.getLargestItem() << ")" << std::endl; std::cout << "size/capacity: " << odcol.size() << "/" << odcol.capacity() << std::endl; std::cout << "Contents: "; odcol.print(std::cout); } std::cout << "========================================== <<< OrderedCollection tester for double " << std::endl; std::cout << std::endl;

std::cout << "7. OrderedCollection tester for Book >>> ==========================================" << std::endl; {// OrderedCollection tester for Book-type std::cout << "[After creating collection in empty state]" << std::endl; sdds::OrderedCollection obcol; std::cout << "Book with smallest pages-to-chapters ratio (initial-default state): " << std::endl; obcol.getSmallestItem().print(std::cout) << std::endl; std::cout << "Book with largest pages-to-chapters ratio (initial-deault-state): " << std::endl; obcol.getLargestItem().print(std::cout) << std::endl; std::cout << "size/capacity: " << obcol.size() << "/" << obcol.capacity() << std::endl; std::cout << std::endl;

std::cout << "[After adding six books to the collection]" << std::endl; for (auto& b : books) obcol += b; std::cout << "Book with smallest pages-to-chapters ratio: " << std::endl; obcol.getSmallestItem().print(std::cout) << std::endl; std::cout << "Book with largest pages-to-chapters ratio: " << std::endl; obcol.getLargestItem().print(std::cout) << std::endl; std::cout << "size/capacity: " << obcol.size() << "/" << obcol.capacity() << std::endl; std::cout << std::endl; std::cout << "List of OrderedCollection Books: " << std::endl; obcol.print(std::cout); } std::cout << "============================================ <<< OrderedCollection tester for Book " << std::endl; std::cout << std::endl;

return cout + endl; }

Book.h

#ifndef SDDS_BOOK_H #define SDDS_BOOK_H

#include #include

namespace sdds { class Book {

public: Book(); std::string m_title{}; unsigned m_numChapters{}; unsigned m_numPages{}; Book(const std::string& title, unsigned nChapters, unsigned nPages);

bool isEmpty() const; bool operator<(const Book& other) const; bool operator>(const Book& other) const;

std::ostream& print(std::ostream& os) const;

std::string getTitle() const { return m_title;

}

// Public mutator to set the title void setTitle(const std::string& title) { m_title = title; } };

std::ostream& operator<<(std::ostream& os, const Book& bk); }

#endif // SDDS_BOOK_H

Book.cpp

#include "Book.h" #include #include #include

namespace sdds { Book::Book() : m_title(""), m_numChapters(0), m_numPages(0) {}

Book::Book(const std::string& title, unsigned nChapters, unsigned nPages) : m_title(title), m_numChapters(nChapters), m_numPages(nPages) {}

bool Book::isEmpty() const { return m_title.empty() || m_numChapters == 0 || m_numPages == 0; }

bool Book::operator<(const Book& other) const { // Compare based on the average pages per chapter return (static_cast(m_numPages) / m_numChapters) < (static_cast(other.m_numPages) / other.m_numChapters); }

bool Book::operator>(const Book& other) const { // Compare based on the average pages per chapter return (static_cast(m_numPages) / m_numChapters) > (static_cast(other.m_numPages) / other.m_numChapters); }

std::ostream& Book::print(std::ostream& os) const { if (!isEmpty()) { std::string leftColumn; leftColumn += m_title; leftColumn += ","; leftColumn += std::to_string(m_numChapters); leftColumn += ","; leftColumn += std::to_string(m_numPages);

/*std::string rightColumn; rightColumn += */

os << std::setw(56) << std::right << leftColumn << " | (" << std::to_string(static_cast(m_numPages) / m_numChapters) << ")" << std::setw(5) << std::right; } else { os << "| Invalid book data |"; } return os; }

std::ostream& operator<<(std::ostream& os, const Book& bk) { return bk.print(os); } }

Collection.h (contains both .h and .cpp codes)

#ifndef SDDS_COLLECTION_H #define SDDS_COLLECTION_H

#include #include "Book.h"

namespace sdds { template class Collection { //unsigned size_t m_size;

static T m_smallestItem; static T m_largestItem;

protected: T m_items[C]{}; void setSmallestItem(const T& item); void setLargestItem(const T& item); T& operator[](size_t index); // Mutator operation void incrSize(); // Mutator operation

public: Collection(); size_t size() const; unsigned capacity() const; bool operator+=(const T& item); static T getSmallestItem(); static T getLargestItem(); std::ostream& print(std::ostream& os) const; //void print(std::ostream& os) const;

std::ostream& printBookData(std::ostream& os, const Book& book) const; }; /* template T Collection::m_smallestItem{ 999 };*/

template <> Book Collection::m_smallestItem = Book("", 1, 10000);

template <> Book Collection::m_smallestItem = Book("", 1, 10000);

/*template T Collection::m_largestItem{ 999 };*/

template <> Book Collection::m_largestItem = Book("", 10000, 1);

template <> Book Collection::m_largestItem = Book("", 10000, 1);

template T Collection::m_smallestItem = T(9999);

template T Collection::m_largestItem = T(-9999);

template Collection::Collection() : m_size(0) {}

template size_t Collection::size() const { return m_size; }

template unsigned Collection::capacity() const { return C; }

template void Collection::setSmallestItem(const T& item) { if (item < m_smallestItem) { m_smallestItem = item; } }

template void Collection::setLargestItem(const T& item) { if (item > m_largestItem) { m_largestItem = item; } }

template T& Collection::operator[](size_t index) { if (index < m_size) { return m_items[index]; } else { throw std::out_of_range("Index out of range"); } }

template void Collection::incrSize() { if (m_size < C) { ++m_size; } }

template bool Collection::operator+=(const T& item) { if (m_size < C) { m_items[m_size] = item; ++m_size; setSmallestItem(item); setLargestItem(item); return true; } return false; }

template T Collection::getSmallestItem() { return m_smallestItem; }

template T Collection::getLargestItem() { return m_largestItem; }

template std::ostream& Collection::print(std::ostream& os) const { for (unsigned i = 0; i < m_size; ++i) { os << m_items[i]; if (i < m_size - 1) { os << ","; } } return os; }

}

#endif // SDDS_COLLECTION_H

OrderedCollection.h (contains both .h and .cpp codes)

#ifndef SDDS_ORDERED_COLLECTION_H #define SDDS_ORDERED_COLLECTION_H

#include "Collection.h" namespace sdds { template class OrderedCollection : public Collection { public: bool operator+=(const T& item); };

template bool OrderedCollection::operator+=(const T& item) { if (this->size() < this->capacity()) { // Find the correct position for insertion size_t insertIndex = 0; while (insertIndex < this->size() && item > this->operator[](insertIndex)) { insertIndex++; }

// Shift elements to make space for the new item for (size_t i = this->size(); i > insertIndex; i--) { this->m_items[i] = this->m_items[i - 1]; }

// Insert the new item this->m_items[insertIndex] = item;

// Update the size this->incrSize();

// Update smallest and largest items if (this->size() == 1 || item < this->getSmallestItem()) { this->setSmallestItem(item); } if (this->size() == 1 || item > this->getLargestItem()) { this->setLargestItem(item); }

return true; }

return false; }

// Explicit instantiation for OrderedCollection with Book type (if needed) template class OrderedCollection; }

#endif //SDDS_ORDERED

Sample output:

Command Line arguments: --------------------------------- 1: ws ---------------------------------

1. Book tester >>> =========================================================== total books: 6 ------------------------------------------------------------------------------ | 1. Software Architecture for Busy Developers,7,174 | (24.857143) | | 2. Low-Code Application Development with Appian,16,462 | (28.875000) | | 3. Natural Language Processing with TensorFlow,11,514 | (46.727273) | | 4. Learning DevOps,17,560 | (32.941176) | | 5. SQL for Data Analytics,9,540 | (60.000000) | | 6. Deciphering Object-Oriented Programming with C++,21,594 | (28.285714) | ============================================================== <<< Book tester

2. Collection tester for int and 10 >>> ====================================== (smallest,largest) items: (5,35) size/capacity: 7/10 Contents: [25,30,20,15,25,5,35] ========================================= <<< Collection tester for int and 10

3. Collection tester for double and 10 >>>==================================== (smallest,largest) items: (1.2,9.1) size/capacity: 6/10 Contents: [7.8,9.1,3.4,1.2,2.3,6.7] ====================================== <<< Collection tester for double and 10

4. Collection tester for Book and 10 >>> ===================================== [After creating collection in empty state] Book with smallest pages-to-chapters ratio (initial-default state): | Invalid book data | Book with largest pages-to-chapters ratio (initial-deault-state): | Invalid book data | size/capacity: 0/10

[After adding Six books to the collection] Book with smallest pages-to-chapters ratio: Software Architecture for Busy Developers,7,174 | (24.857143) Book with largest pages-to-chapters ratio: SQL for Data Analytics,9,540 | (60.000000) size/capacity: 6/10

Collection content: | ---------------------------------------------------------------------------| | Software Architecture for Busy Developers,7,174 | (24.857143) | | Low-Code Application Development with Appian,16,462 | (28.875000) | | Natural Language Processing with TensorFlow,11,514 | (46.727273) | | Learning DevOps,17,560 | (32.941176) | | SQL for Data Analytics,9,540 | (60.000000) | | Deciphering Object-Oriented Programming with C++,21,594 | (28.285714) | | ---------------------------------------------------------------------------| ======================================== <<< Collection tester for Book and 10

5. OrderedCollection tester for int >>> =========================================== (smallest,largest) items: (5,35) size/capacity: 7/72 Contents: [5,15,20,25,25,30,35] ============================================= <<< OrderedCollection tester for int

6. OrderedCollection tester for double >>> ======================================== (smallest,largest) items: (1.2,9.1) size/capacity: 6/72 Contents: [1.2,2.3,3.4,6.7,7.8,9.1] ========================================== <<< OrderedCollection tester for double

7. OrderedCollection tester for Book >>> ========================================== [After creating collection in empty state] Book with smallest pages-to-chapters ratio (initial-default state): | Invalid book data Book with largest pages-to-chapters ratio (initial-deault-state): | Invalid book data size/capacity: 0/72

[After adding six books to the collection] Book with smallest pages-to-chapters ratio: Software Architecture for Busy Developers,7,174 | (24.857143) Book with largest pages-to-chapters ratio: SQL for Data Analytics,9,540 | (60.000000) size/capacity: 6/72

List of OrderedCollection Books: | ---------------------------------------------------------------------------| | Software Architecture for Busy Developers,7,174 | (24.857143) | | Deciphering Object-Oriented Programming with C++,21,594 | (28.285714) | | Low-Code Application Development with Appian,16,462 | (28.875000) | | Learning DevOps,17,560 | (32.941176) | | Natural Language Processing with TensorFlow,11,514 | (46.727273) | | SQL for Data Analytics,9,540 | (60.000000) | | ---------------------------------------------------------------------------| ============================================ <<< OrderedCollection tester for Book

I just need code for this part with *this* in collection module:

Collection Module

Template Specializations

specialize the initializations of m_smallestItem and m_largestItem objects: when T = Book and C = 72, the values stored {"", 1, 10000} and {"", 10000, 1}, respectively.

*this* specialize the print() function when T = Book and C = 10 to print Book object's data differently, on multiple rows; see sample output for a hint on exact formatting of output.

*this* specialize the print() function when T = Book and C = 72 to print data in the same way as the previous specialization.

No other changes are necessary to this module.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions