Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #2

Open
wants to merge 1 commit into
base: Assignment1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions EmptyDataCollectionException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* EmptyDataCollectionException.cpp
*
* Class Description: Defines the exception that is thrown when the
* data collection ADT class is empty.
*
* Author: Inspired from our textbook's authors Frank M. Carrano and Tim Henry.
* Copyright (c) 2013 __Pearson Education__. All rights reserved.
*/


#include "EmptyDataCollectionException.h"

EmptyDataCollectionException::EmptyDataCollectionException(const string& message):
logic_error("Empty Data Collection ADT Class Exception: " + message)
{
} // end constructor

// End of implementation file.
23 changes: 23 additions & 0 deletions EmptyDataCollectionException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* EmptyDataCollectionException.h
*
* Class Description: Defines the exception that is thrown when the
* data collection ADT class is empty.
*
* Author: Inspired from our textbook's authors Frank M. Carrano and Tim Henry.
* Copyright (c) 2013 __Pearson Education__. All rights reserved.
*/

#pragma once

#include <stdexcept>
#include <string>

using namespace std;

class EmptyDataCollectionException : public logic_error
{
public:
EmptyDataCollectionException(const string& message = "");

}; // end EmptyDataCollectionException
79 changes: 79 additions & 0 deletions Event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Event.cpp
*
* Class Definition: Stores a type (arrival/departure),
* arrival/departure time, and the durature of an event
*
* Created on: July 4th, 2017
* Author: Jacky Lee & Joey Zhu
*/


#pragma once
#include <iostream>
#include <ostream>
#include <string>
#include "Event.h"

using namespace std;

Event::Event(){
Type = 'A';
Time = 0;
Length = 0;
}

Event::Event(char aType, int aTime){
Type = aType;
Time = aTime;
Length = 0;
}

Event::Event(char aType, int aTime, int aLength){
Type = aType;
Time = aTime;
Length = aLength;
}

Event::~Event(){
// not needed
}

char Event::getType() const{
return Type;
}

int Event::getTime() const{
return Time;
}

int Event::getLength() const{
return Length;
}

void Event::setType(char aType){
Type = aType;
}

void Event::setTime(int aTime){
Time = aTime;
}

void Event::setLength(int aLength){
Length = aLength;
}

bool Event::operator > (const Event &rhs){
return (Time > rhs.Time);
}

bool Event::operator == (const Event &rhs){
return (Time == rhs.Time && Type == rhs.Type);
}

ostream & operator<<(ostream & os, const Event & rhs){
os << "Type = " << rhs.getType();
os << " Time = " << rhs.getTime();
os << " Length = " << rhs.getLength() << " ";
return os;
}
41 changes: 41 additions & 0 deletions Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Event.h
*
* Class Definition: Stores a type (arrival/departure),
* arrival/departure time, and the durature of an event
*
* Created on: July 4th, 2017
* Author: Jacky Lee & Joey Zhu
*/

#pragma once
#include <iostream>
#include <ostream>

using namespace std;

class Event{
private:
char Type;
int Time;
int Length;

public:
// constructors and destructor
Event();
Event(char aType, int aTime);
Event(char aType, int aTime, int aLength);
~Event();
// getters
char getType() const;
int getTime() const;
int getLength() const;
// setters
void setType(char aType);
void setTime(int aTime);
void setLength(int aLength);
// operator overloads
bool operator > (const Event &rhs);
bool operator == (const Event &rhs);
friend ostream & operator<<(ostream & os, const Event& rhs);
};
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all: sApp

sApp: SimulationApp.cpp Queue.h PriorityQueue.h Node.h Event.o EmptyDataCollectionException.o
g++ -Wall -std=c++11 -o sApp SimulationApp.cpp Event.o EmptyDataCollectionException.o

Event.o: Event.cpp Event.h
g++ -c Event.cpp

EmptyDataCollectionException.o: EmptyDataCollectionException.cpp EmptyDataCollectionException.h
g++ -c EmptyDataCollectionException.cpp

clean:
rm -f sApp *.o
54 changes: 54 additions & 0 deletions Node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Node.h
*
* Class Definition: Node of a singly linked list
* in which the data is of "int" data type.
* Designed and implemented as a non-ADT.
*
* Created on: July 4th, 2017
* Author: Jacky Lee & Joey Zhu
*/

// #pragma once is shorthand for the 2 #include guards we've seen before:
// #ifndef _NODE and #define _NODE
// and it means: do not include me more than once.
#pragma once
#include <cstdio>

template <class ElementType>
class Node
{
public:
// Public attributes - Why are the attributes public?
ElementType data; // The data in the node
Node<ElementType> *next; // Pointer to next node

// Constructors (why no destructor?)
Node();
Node(ElementType theData);
Node(ElementType theData, Node* theNextNode);

}; // end Node

// implementation
template <class ElementType>
Node<ElementType>::Node()
{
next = NULL;
}

template <class ElementType>
Node<ElementType>::Node(ElementType theData)
{
data = theData;
next = NULL;
}

template <class ElementType>
Node<ElementType>::Node(ElementType theData, Node* theNextNode)
{
data = theData;
next = theNextNode;
}

// end Node.cpp
145 changes: 145 additions & 0 deletions PriorityQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* PriorityQueue.h
*
* Class Definition: PriorityQueue using array implementation
*
* Created on: July 4th, 2017
* Author: Jacky Lee & Joey Zhu
*/

#pragma once
#include <iostream>
#include <new>
#include "Event.h"
#include "EmptyDataCollectionException.h"

using namespace std;

template <class ElementType>
class PriorityQueue {

private:
static const int MAX_CAPACITY = 100;
ElementType array[100];
int front;
int back;
int capacity;

public:
// Let's put our constructor(s) and destructor (if any) ***here***.
PriorityQueue();

~PriorityQueue();

/******* Public Interface - START - *******/

// Description: Returns the number of elements in the Priority Queue.
// (This method eases testing.)
// Time Efficiency: O(1)
int getElementCount() const;

// Description: Returns "true" is this Priority Queue is empty, otherwise "false".
// Time Efficiency: O(1)
bool isEmpty() const;

// Description: Inserts newElement in sort order.
// It returns "true" if successful, otherwise "false".
// Precondition: This Priority Queue is sorted.
// Postcondition: Once newElement is inserted, this Priority Queue remains sorted.
// Time Efficiency: O(n)
bool enqueue(const ElementType& newElement);

// Description: Removes the element with the "highest" priority.
// It returns "true" if successful, otherwise "false".
// Precondition: This Priority Queue is not empty.
// Time Efficiency: O(1)
bool dequeue();

// Description: Returns (a copy of) the element with the "highest" priority.
// Precondition: This Priority Queue is not empty.
// Postcondition: This Priority Queue is unchanged.
// Exceptions: Thows EmptyDataCollectionException if this Priority Queue is empty.
// Time Efficiency: O(1)
ElementType peek() const throw(EmptyDataCollectionException);


/******* Public Interface - END - *******/

// Let's feel free to add other private helper methods to our Priority Queue class.

}; // end PriorityQueue

template <class ElementType>
PriorityQueue<ElementType>::PriorityQueue(){
front = -1;
back = -1;
capacity = MAX_CAPACITY;
}

template <class ElementType>
PriorityQueue<ElementType>::~PriorityQueue(){
// not needed
}

template <class ElementType>
int PriorityQueue<ElementType>::getElementCount() const{
if (front == -1){
return 0;
}
if (front == back){
return 1;
}
return (back-front+1);
}

template <class ElementType>
bool PriorityQueue<ElementType>::isEmpty() const{
return (front == -1);
}

template <class ElementType>
bool PriorityQueue<ElementType>::enqueue(const ElementType& newElement){
if ((back+1) % capacity == front){
return false;
}
if (isEmpty()){
front = 0;
back = 0;
array[front] = newElement;
}
else{
int i = front;
while (i <= back && (!(array[i] > newElement))){
i++;
}
for (int j = back+1; j > i; j--){
array[j] = array[j-1];
}
array[i] = newElement;
back = (back+1) % capacity;
}
return true;
}

template <class ElementType>
bool PriorityQueue<ElementType>::dequeue(){
if (isEmpty()){
return false;
}
if (front == back){
front = -1;
back = -1;
}
else{
front++;
}
return true;
}

template <class ElementType>
ElementType PriorityQueue<ElementType>::peek() const throw(EmptyDataCollectionException){
if (isEmpty()){
throw EmptyDataCollectionException("Queue is currently empty.");
}
return array[front];
}
Loading