Problem: Write a C++ program that will allow a research lab manager to monitor t
ID: 3753881 • Letter: P
Question
Problem: Write a C++ program that will allow a research lab manager to monitor the access to a research lab. Each member of the lab has access provided by their personal ID Card Your program will provide information about the timing of the arrivals and departures of the lab staff. Your program will be provided with a dataset where each row records an event, which is represented by the following information 1) ID number - a positive integer number 2) Person's name a string 3) Time of the event - a string in "01:23AM" format ("dd:dd:AM" or "dd:dd:PM") Each event records the use of an ID card to either enter or exit the lab. A given data set corresponds to one day and is already sorted by ID number. You should assume that the lab is empty at the beginning of the day and events are consistent a given person cannot enter or exit without the event being recorded, a given person cannot enter the lab twice without exiting once in between, etc. The program should first read the events from a text file named "dataset.txt" It wil contain up to 100 events. See the sample file on the class website Then, it should offer the user a menu with the following options: 1. Display the events sorted by ID number 2. Display the events sorted by the time of event. 3. Print the person's name given his/her ID number. 4. Find out whether some person is still in the lab given his/her ID number 5. Quit The program should perform the selected operation and then re-display the menu Do not change the menu numbers associated with the operations. Display an error message if the user enters an inappropriate number For options 1 and 2, display the information for each event on a single, separate line The values should line up in columns (use setw). Headers for the table are optionalExplanation / Answer
#include <new>
#include "LinkedList.h"
LinkedList::LinkedList(){
}
LinkedList::~LinkedList(){
Node *x = first;
while(x != NULL) {
Node *y = x;
x = x ->next;
delete y;
}
}
LinkedList::LinkedList(const LinkedList &origList){
int count = 0;
Node *x = origList.first;
while(x != NULL) {
insert(x->data, count++);
x = x ->next;
}
}
const LinkedList & LinkedList::operator=(const LinkedList & rightHandSide){
resetList();
int count = 0;
Node *x = rightHandSide.first;
while(x != NULL) {
insert(x->data, count++);
x = x ->next;
}
return *this;
}
bool LinkedList::isEmpty() const{
return mySize == 0;
}
int LinkedList::getListSize() const{
return mySize;
}
void LinkedList::display(std::ostream & out) const{
Node *x = first;
while(x != NULL) {
out << x->data << " ";
x = x ->next;
}
out << " ";
}
LinkedList operator+(const LinkedList & x, const LinkedList & y){
LinkedList c;
int xSize = x.getListSize();
int ySize = y.getListSize();
LinkedList::ListElement *data1 = new LinkedList::ListElement[xSize];
LinkedList::ListElement *data2 = new LinkedList::ListElement[ySize];
x.getListElement(0, xSize-1, data1);
y.getListElement(0, ySize-1, data2);
int i=0, j=0, count = 0;
while(i < xSize || j < ySize) {
if(i < xSize) {
c.insert(data1[i], count++);
i++;
}
if(j < ySize) {
c.insert(data2[j], count++);
j++;
}
}
return c;
}
int operator==(const LinkedList & x, const LinkedList & y){
int xSize = x.getListSize();
int ySize = y.getListSize();
if(xSize != ySize) {
return 0;
}
LinkedList::ListElement *data1 = new LinkedList::ListElement[xSize];
LinkedList::ListElement *data2 = new LinkedList::ListElement[ySize];
x.getListElement(0, xSize-1, data1);
y.getListElement(0, ySize-1, data2);
for(int i=0; i<xSize; i++) {
LinkedList::ListElement d = data1[i];
bool found = false;
for(int j=0; j<ySize; j++) {
if(d == data2[j]) {
found = true;
break;
}
}
if(!found) {
return 0;
}
}
for(int i=0; i<ySize; i++) {
LinkedList::ListElement d = data2[i];
bool found = false;
for(int j=0; j<xSize; j++) {
if(d == data1[j]) {
found = true;
break;
}
}
if(!found) {
return 0;
}
}
return 1;
}
void LinkedList::resetList(){
Node *x = first;
while(x != NULL) {
Node *y = x->next;
delete x;
x = y;
}
first = NULL;
mySize = 0;
}
LinkedList::ErrorCode LinkedList::insert(ListElement item, int pos){
int size = getListSize();
if(pos < 0 || pos > size) {
return ILLEGAL_LIST_POSITION;
}
Node *x = new Node;
x->data = item;
x->next = NULL;
if(pos == 0) {
x->next = first;
first = x;
} else {
Node *y = first;
while(pos != 1) {
y = y ->next;
pos--;
}
x->next = y->next;
y->next = x;
}
mySize++;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::erase(int pos){
int size = getListSize();
if(pos < 0 || pos >= size) {
return ILLEGAL_LIST_POSITION;
}
if(pos == 0) {
Node *y = first;
first = first->next;
delete y;
} else {
Node *y = first;
while(pos != 1) {
y = y ->next;
pos--;
}
Node *x = y->next;
y->next = y->next->next;
delete x;
}
mySize--;
return NO_ERROR;
}
LinkedList::ErrorCode LinkedList::move(int n, int m){
int size = getListSize();
if(n < 0 || n >= size) {
return ILLEGAL_LIST_POSITION;
}
if(m < 0 || m >= size) {
return ILLEGAL_LIST_POSITION;
}
if(m != n) {
Node *a = first;
int count = 0;
while(a != NULL) {
if(n == count) {
break;
}
a = a ->next;
count++;
}
ListElement temp = a->data;
erase(n);
insert(temp, m);
}
return NO_ERROR;
}
void LinkedList::reverse(){
Node *current = first;
Node prev = NULL, next = NULL;
while (current != NULL) {
next = current->next;
// Reverse current node's pointer
current->next = prev;
prev = current;
current = next;
}
first = prev;
}
LinkedList::ErrorCode LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const {
int size = getListSize();
if(posStart < 0 || posStart >= size) {
return ILLEGAL_LIST_POSITION;
}
if(posEnd < 0 || posEnd >= size) {
return ILLEGAL_LIST_POSITION;
}
if(posEnd < posStart) {
return ILLEGAL_LIST_POSITION;
}
int index = 0;
Node x, y;
Node *a = first;
int count = 0;
while(a != NULL) {
if(posStart == count) {
x = a;
}
if(posEnd == count) {
y = a;
}
a = a ->next;
count++;
}
a = x;
while(a != y->next) {
rv[index++] = a->data;
a = a->next;
}
return NO_ERROR;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.