Data Structures using C++ assignment Create 3 files only 1) header file for Log
ID: 670573 • Letter: D
Question
Data Structures using C++ assignment
Create 3 files only
1) header file for Log file (source file given should NOT be changed)
2)Header file to be created for worker class
3) a .cpp file that contains main() and used to display the log of workers
__________________________
The following output should look like this:
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter:
--------------------------------------------------------------------
//If pressed 1 without pressing 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 1
There is nothing in the log!
Press any key to continue...
--------------------------------------------------------------------
//If pressed 3 without pressing 1,2,4
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 3
You chose the search for a worker in the log.
Type worker ID# to be searched:
Log Empty!
Press any key to continue...
--------------------------------------------------------------------
//if pressed 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 2
You chose to add worker(s) to the Log.
Enter the Workers(s) that will be stored,
Press "n" when you are done:
Enter name: John Doe
Enter ID: 100
Salary: 1000.00
Enter another worker (y/n)? y
Enter name: Linda Johnson
Enter ID: 101
Salary: 10000.00
Enter another worker (y/n)? y
Enter name: Bob Jackson
Enter ID: 102
Salary: 100000.00
Enter another worker (y/n)? n
Press any key to continue...
-----------------------------------------------------------------
//If pressed 1 after entering option 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 1
Here are the contents:
name: John Doe ID: 100 Salary: 1000.00
name: Linda Johnson ID: 101 Salary: 10000.00
name: Bob Jackson ID: 102 Salary: 100000.00
Log size: 3
Press any key to continue...
-----------------------------------------------------------------
//If pressed 3 aftering entering option 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 3
You chose the search for a worker in the log.
Type worker ID# to be searched: 101
Name: Linda Johnson ID: 101 Salary 10000.00
found at node # 2
and moved to the front of the log
Press any key to continue...
-------------------------------------------------------------------
//If pressed 3 aftering entering option 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 3
You chose the search for a worker in the log.
Type worker ID# to be searched: 105
Value 105 not found!
Press any key to continue...
-------------------------------------------------------------------
//If pressed 1 after entering option 3
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 1
Here are the contents:
name: Linda Johnson ID: 101 Salary: 10000.00
name: John Doe ID: 100 Salary: 1000.00
name: Bob Jackson ID: 102 Salary: 100000.00
Log size: 3
Press any key to continue...
--------------------------------------------------------------------
//If pressed 4 aftering entering option 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 4
You chose to remove a worker from the log
Type worker ID # to be removed from Log: 101
Value Name: Linda Johnson ID: 101 Salary 10000.00
removed at node # 1
Press any key to continue...
------------------------------------------------------------------
//If pressed 4 aftering entering option 2
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 4
You chose to remove a worker from the log
Type worker ID # to be removed from Log: 105
Value 105 not found!
Press any key to continue...
-----------------------------------------------------------------
//If pressed 5
1. Show Log contents
2. Add infomation to the Log
3. Search for an item in the Log
4. Withdraw one infomation item from the list
5. Exit the program
What would you like to do? Select a number (1-5), then press Enter: 5
Good-Bye...
Press any key to continue...
-----------------------------------------------
the following is the log.h header file to use and DO NOT change
// File: Log.h
#ifndef LOG_H // // multiple inclusion guard
#define LOG_H
template <typename Object>
class Log
{
private:
struct Node // private member structure (could be class instead)
{
Object data;
Node* prev;
Node* next;
Node(const Object& d = Object(), Node* p = NULL, Node* n = NULL) // constructor
: data(d), prev(p), next(n) {}
Node(Object&& d, Node* p = NULL, Node* n = NULL) // move constructor (C++11)
: data(move(d)), prev(p), next(n) {}
};
public:
class const_iterator // **********************************************
{
public:
const_iterator() : current(NULL) {} // default constructor
const Object& operator*() const // const de-referencing
{
return retrieve();
}
const_iterator& operator++() // pre-increment
{
current = current->next;
return *this;
}
const_iterator operator++(int) // post-increment
{
const_iterator old = *this;
++(*this);
return old;
}
const_iterator& operator--() // pre-decrement
{
current = current->prev;
return *this;
}
const_iterator operator--(int) // post-decrement
{
const_iterator old = *this;
--(*this);
return old;
}
bool operator== (const const_iterator& rhs) const
{
return current == rhs.current;
}
bool operator != (const const_iterator& rhs) const
{
return !(*this == rhs);
}
protected:
Node* current;
Object& retrieve() const
{
return current->data;
}
const_iterator(Node* p) : current(p) {} // constructor
friend class Log<Object>;
}; // end class const_iterator // **********************************************
class iterator : public const_iterator // **************************************
{
public:
iterator() : current(NULL) {} // default constructor
Object& operator*() // de-referencing
{
return retrieve();
}
const Object& operator*() const // const de-referencing
{
return const_iterator::operator*();
}
iterator& operator++() // pre-increment
{
current = current->next;
return *this;
}
iterator operator++(int) // post-increment
{
iterator old = *this;
++(*this);
return old;
}
iterator& operator--() // pre-decrement
{
current = current->prev;
return *this;
}
iterator operator--(int) // post-decrement
{
iterator old = *this;
--(*this);
return old;
}
private: // we are not expecting to derive from iterator...
iterator(Node* p) : const_iterator(p) {} // constructor
friend class Log<Object>;
};// end class iterator // **********************************************
// back to class Log:
public:
// constructors
Log() // default
{
init();
}
Log(const Log& rhs) // copy constructor
{
init();
for (const Object& x : rhs) // range-based for loop (C++11)
push_back(x);
}
Log(Log&& rhs) // move constructor (C++11)
: theSize(rhs.theSize), head(rhs.head), tail(rhs.tail)
{
rhs.theSize = 0;
rhs.head = NULL;
rhs.tail = NULL;
}
// destructor
~Log()
{
clear();
delete head;
head = NULL;
delete tail;
tail = NULL;
}
// public assignment operator
Log& operator=(const List& rhs)
{
if (this != &rhs) // prevent assignment to itself
{
this->clear();
for (const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr)
this->push_back(*itr);
}
return *this;
}
iterator begin()
{
return iterator(head->next);
}
const_iterator begin() const
{
return const_iterator(head->next);
}
iterator end()
{
return iterator(tail);
}
const_iterator end() const
{
return const_iterator(tail);
}
size_t size() const
{
return theSize;
}
bool empty() const
{
return size() == 0;
}
void clear()
{
while (!empty())
pop_front();
}
Object& front()
{
return *begin();
}
const Object& front() const
{
return *begin();
}
Object& back()
{
return *--end();
}
const Object& back() const
{
return *--end();
}
void push_front(const Object& x)
{
insert(begin(), x);
}
void push_back(const Object& x)
{
insert(end(), x);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
// Insert copy of x on the free store before itr:
iterator insert(iterator itr, const Object& x)
{
Node* p = itr.current;
theSize++;
try
{
// new Node allocated on the freestore
p->prev = p->prev->next = new Node(x, p->prev, p);
}
// if memory allocation failed:
catch (...) // executes in case of exception (error)
{
cout << "Error encountered... quitting, sorry! ";
system("pause");
exit(EXIT_FAILURE); // quit
}
return iterator(p->prev);
}
// Erase item at itr:
iterator erase(iterator itr)
{
if (!empty())
{
Node* p = itr.current;
iterator retVal(p->next);
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
p = NULL;
theSize--;
return retVal;
}
else
{
cout << "Log empty! ";
return NULL;
}
}
// Erase items from start to end:
iterator erase(iterator start, iterator end)
{
for (iterator itr = start; itr != end;)
itr = erase(itr);
return end;
}
private:
size_t theSize;
Node* head;
Node* tail;
void init()
{
theSize = 0;
try
{
head = new Node; // on the free store
tail = new Node; // on the free store
}
// if memory allocation failed:
catch (...) // executes in case of exception (error)
{
cout << "Error encountered... quitting, sorry! ";
system("pause");
exit(EXIT_FAILURE); // quit
}
head->next = tail;
tail->prev = head;
}
};// end class Log
#endif
Explanation / Answer
//Workers.h file for worker class
#include <iostream> //for input and output operations
#ifndef WORKER_H
#define WORKER_H
class Worker
{
string ename; //data members declaration
int ID;
float salary;
public:
Worker(); //default constructor
Worker(string ename,int ID,float salary);//parameterized constructor
void readWorker(); //member function for input Worker data
void showWorker(); //member function for showing Worker data
};
#endif
#include "Worker.h"
using namespace std;
Worker::Worker()
{
ename=""; //assigning default values
ID=0;
salary=0.00;
}
Worker::Worker(string ename,int ID,float salary)
{
this->ename=ename; //this refers current class data members and assigned with parameter
this->ID=ID;
this->salary=salary;
}
void Worker::readWorker()
{
cout<<"Enter name : ";cin>>ename; //reading of each data member from user
cout<<"Enter ID : ";cin>>ID;
cout<<"Salary : ";cin>>salary;
}
void Worker::showWorker()
{
cout<<" Name : "<<ename<<" ID: "<<ID<<" Salary : "<<salary; //displaying Worker's data
}
//Main Worker - Main program
#include "Log.h"
#include "Worker.h"
using namespace std;
int main()
{
Worker w1;
w1.readData();
Log <Worker> l1(w1);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.