Fill This Form To Receive Instant Help
Homework answers / question archive / sortedtype
sortedtype.h
#ifndef SORTEDTYPE_H_INCLUDED #define SORTEDTYPE_H_INCLUDED
const int MAX_ITEMS = 5; template <class ItemType> class SortedType {
public :
SortedType(); void MakeEmpty(); bool IsFull(); int LengthIs(); void InsertItem(ItemType); void DeleteItem(ItemType); void RetrieveItem(ItemType&, bool&);
void ResetList(); void GetNextItem(ItemType&); private:
int length; ItemType info[MAX_ITEMS]; int currentPos; }; #endif // SORTEDTYPE_H_INCLUDED
sortedtype.cpp
#include "sortedtype.h" template <class ItemType> SortedType<ItemType>::SortedType() {
length = 0; currentPos = - 1; } template <class ItemType> void SortedType<ItemType>::MakeEmpty() {
length = 0; } template <class ItemType> bool SortedType<ItemType>::IsFull() {
return (length == MAX_ITEMS); } template <class ItemType> int SortedType<ItemType>::LengthIs() {
return length; } template <class ItemType> void SortedType<ItemType>::ResetList() {
currentPos = - 1; } template <class ItemType> void SortedType<ItemType>::GetNextItem(ItemType& item) {
currentPos++; item = info [currentPos]; }
template <class ItemType> void SortedType<ItemType>::InsertItem(ItemType item) {
int location = 0; bool moreToSearch = (location < length);
while (moreToSearch) {
if(item > info[location]) {
location++; moreToSearch = (location < length); } else if(item < info[location])
moreToSearch = false; } for (int index = length; index > location; index--)
info[index] = info[index - 1]; info[location] = item; length++; } template <class ItemType> void SortedType<ItemType>::DeleteItem(ItemType item) {
int location = 0;
location++; for (int index = location + 1; index < length; index++)
info[index - 1] = info[index]; length--; } template <class ItemType> void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) {
int midPoint, first = 0, last = length - 1; bool moreToSearch = (first <= last); found = false; while (moreToSearch && !found) {
midPoint = (first + last) / 2; if(item < info[midPoint]) {
last = midPoint - 1; moreToSearch = (first <= last); } else if(item > info[midPoint]) {
first = midPoint + 1; moreToSearch = (first <= last); } else {
found = true; item = info[midPoint]; } } }
while (item != info[location])
Generate the driver file (main.cpp) where you perform the following tasks. Note that you cannot make any change to the header file or the source file.
Operation to Be Tested and Description of Action Input Values Expected Output
• Create a list of integers
• Print length of the list 0
• Insert five items 5 7 4 2 1
• Print the list 1 2 4 5 7
• Retrieve 6 and print whether found Item is not found
• Retrieve 5 and print whether found Item is found
• Print if the list is full or not List is full
• Delete 1
• Print the list 2 4 5 7
• Print if the list is full or not List is not full
• Write a class timeStamp that represents a time of the day. It must have variables to store the number of seconds, minutes and hours passed. It also must have a function to print all the values. You will also need to overload a few operators.
• Create a list of objects of class timeStamp.
• Insert 5 time values in the format ssmmhh 15 34 23 13 13 02 43 45 12 25 36 17 52 02 20
• Delete the timestamp 25 36 17
• Print the list 15:34:23 13:13:02 43:45:12 52:02:20
Give me the main file
Answer: //timeStamp.h #ifndef TIMESTAMP_H #define TIMESTAMP_H #include <iostream> #include <string> using namespace std; class timeStamp { public: timeStamp(); timeStamp(int, int, int); void display(); bool operator==(timeStamp); bool operator!=(timeStamp); bool operator>=(timeStamp); bool operator>(timeStamp); bool operator<(timeStamp); timeStamp& operator=(const timeStamp& f); timeStamp(const timeStamp &t) { sec = t.sec; min = t.min; hour = t.hour; } private: int sec; int min; int hour; }; #endif //timeStamp.cpp #include "timeStamp.h" timeStamp::timeStamp() { sec = 0; min = 0; hour = 0; } timeStamp::timeStamp(int h, int m, int s) { this->hour = h; this->min = m; this->sec = s; } void timeStamp::display() { cout << hour << ":" << min << ":" << sec << endl; } timeStamp& timeStamp::operator=(const timeStamp& f) { sec = f.sec; min = f.min; hour = f.hour; return *this; } bool timeStamp::operator>=(timeStamp t) { if (hour >= t.hour) { return true; } else { if (hour == t.hour) { if (min >= t.min) { return true; } else if (min == t.min) { if (sec >= t.sec) { return true; } } return false; } } return false; } bool timeStamp::operator>(timeStamp t) { if (hour >= t.hour) { if (min >= t.min) { if (sec > t.sec) return true; } else return false; } return false; } bool timeStamp::operator==(timeStamp t) { if (hour == t.hour && min == t.min && sec == t.sec) return true; else return false; } bool timeStamp::operator!=(timeStamp t) { if (hour == t.hour && min == t.min && sec == t.sec) return false; return true; } bool timeStamp::operator<(timeStamp t) { if (hour < t.hour) return true; else if (hour < t.hour && min < t.min) return true; else if (hour < t.hour && min < t.min && sec < t.sec) return true;
return false;}
//main.cpp #include<iostream> #include "sortedtype.cpp" #include "timeStamp.cpp" using namespace std; int main() { //• Create a list of integers SortedType<int> list = SortedType<int>(); //• Print length of the list 0 cout << "Length of list = " << list.LengthIs() << endl; //• Insert five items 5 7 4 2 1 cout << "Insert five items 5 7 4 2 1" << endl; list.InsertItem(5); list.InsertItem(7); list.InsertItem(4); list.InsertItem(2); list.InsertItem(1); //• Print the list 1 2 4 5 7 cout << "Print the list " << endl; int num = 0; for (int i = 0; i < list.LengthIs(); i++) { list.GetNextItem(num); cout << num << " "; } cout << endl; //• Retrieve 6 and print whether found Item is not found bool found; num = 6; list.RetrieveItem(num, found); if (found) { cout << "6 is found." << endl; } else { cout << "6 is not found." << endl; } //• Retrieve 5 and print whether found Item is found num = 5; list.RetrieveItem(num, found); if (found) { cout << "5 is found." << endl; } else { cout << "5 is not found." << endl; } //• Print if the list is full or not List is full if (list.IsFull()) cout << "The list is full." << endl; else cout << "The list is not full." << endl; //• Delete 1 cout << "Delete 1 " << endl; list.DeleteItem(1); list.ResetList(); //• Print the list 2 4 5 7 cout << "Print the list " << endl; num = 0; for (int i = 0; i < list.LengthIs(); i++) { list.GetNextItem(num); cout << num << " "; } cout << endl; //• Print if the list is full or not List is not full if (list.IsFull()) { cout << "The list is full." << endl; } else { cout << "The list is not full." << endl; } SortedType<timeStamp> listTimeStamp; listTimeStamp.InsertItem(timeStamp(15, 34, 23)); listTimeStamp.InsertItem(timeStamp(13, 13, 02)); listTimeStamp.InsertItem(timeStamp(43, 45, 12)); listTimeStamp.InsertItem(timeStamp(25, 36, 17)); listTimeStamp.InsertItem(timeStamp(52, 2, 20)); //listTimeStamp.ResetList(); //Delete the timestamp 25 36 17 listTimeStamp.DeleteItem(timeStamp(25, 36, 17)); listTimeStamp.ResetList(); cout << "\nPrint the time stamp list " << endl; timeStamp temp; for (int i = 0; i < listTimeStamp.LengthIs(); i++) { listTimeStamp.GetNextItem(temp); temp.display(); } return 0; }
Step-by-step explanation
Explanation:
SortedType<timeStamp> listTimeStamp; listTimeStamp.InsertItem(timeStamp(15, 34, 23)); listTimeStamp.InsertItem(timeStamp(13, 13, 02)); listTimeStamp.InsertItem(timeStamp(43, 45, 12)); listTimeStamp.InsertItem(timeStamp(25, 36, 17)); listTimeStamp.InsertItem(timeStamp(52, 2, 20));
listTimeStamp.DeleteItem(timeStamp(25, 36, 17));
bool operator==(timeStamp); bool operator!=(timeStamp); bool operator>=(timeStamp); bool operator>(timeStamp); bool operator<(timeStamp); timeStamp& operator=(const timeStamp& f); timeStamp(const timeStamp &t) { sec = t.sec; min = t.min; hour = t.hour; }
please use this google drive link to download the answer file.
https://drive.google.com/file/d/1wdAe6axFudAYcQdXyr_Mco7q7SIfF-Gb/view?usp=sharing
note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.
https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link