data structure 2
Lab2 CSC 240 Files/DateType.cpp
Lab2 CSC 240 Files/DateType.cpp
// File DateType.cpp contains the implementation of class DateType
#include
"DateType.h"
#include
<
fstream
>
#include
<
iostream
>
using
namespace
std
;
// Nmber of days in each month
static
int
daysInMonth
[]
=
{
0
,
31
,
28
,
31
,
30
,
31
,
30
,
31
,
31
,
30
,
31
,
30
,
31
};
// Nmaes of the months
static
string conversionTable
[]
=
{
"Error"
,
"January"
,
"February"
,
"March"
,
"April"
,
"May"
,
"June"
,
"July"
,
"August"
,
"September"
,
"October"
,
"November"
,
"December"
};
void
DateType
::
Initialize
(
int
newMonth
,
int
newDay
,
int
newYear
)
// Post: If newMonth, newDay and newYear represent a valid date,
// year is set to newYear;
// month is set to newMonth;
// day is set to newDay;
// otherwise a string exception is thrown, stating the
// first incorrect parameter.
{
if
(
newMonth
<
1
||
newMonth
>
12
)
throw
string
(
"Month is invalid"
);
else
if
(
newDay
<
1
||
newDay
>
daysInMonth
[
newMonth
])
throw
string
(
"Day is invalid"
);
else
if
(
newYear
<
1583
)
throw
string
(
"Year is invalid"
);
year
=
newYear
;
month
=
newMonth
;
day
=
newDay
;
}
int
DateType
::
GetMonth
()
const
// Accessor function for data member month.
{
return
month
;
}
string
DateType
::
GetMonthAsString
()
const
// Returns data member as a string
{
return
conversionTable
[
month
];
}
int
DateType
::
GetYear
()
const
// Accessor function for data member year.
{
return
year
;
}
int
DateType
::
GetDay
()
const
// Accessor function for data member day.
{
return
day
;
}
RelationType
DateType
::
ComparedTo
(
DateType
aDate
)
const
// Pre: Self and aDate have been initialized.
// Post: Function value = LESS, if self comes before aDate.
// = EQUAL, if self is the same as aDate.
// = GREATER, if self comes after aDate.
{
if
(
year
<
aDate
.
year
)
return
LESS
;
else
if
(
year
>
aDate
.
year
)
return
GREATER
;
else
if
(
month
<
aDate
.
month
)
return
LESS
;
else
if
(
month
>
aDate
.
month
)
return
GREATER
;
else
if
(
day
<
aDate
.
day
)
return
LESS
;
else
if
(
day
>
aDate
.
day
)
return
GREATER
;
else
return
EQUAL
;
}
DateType
DateType
::
Adjust
(
int
daysAway
)
const
// Pre: Self has been initialized
// Post: Function value = newDate daysAway from self
{
int
newDay
=
day
+
daysAway
;
int
newMonth
=
month
;
int
newYear
=
year
;
bool
finished
=
false
;
int
daysInThisMonth
;
DateType
returnDate
;
while
(
!
finished
)
{
daysInThisMonth
=
daysInMonth
[
newMonth
];
if
(
newMonth
==
2
)
if
(((
newYear
%
4
==
0
)
&&
!
(
newYear
%
100
==
0
))
||
(
newYear
%
400
==
0
))
daysInThisMonth
++
;
if
(
newDay
<=
daysInThisMonth
)
finished
=
true
;
else
{
newDay
=
newDay
-
daysInThisMonth
;
newMonth
=
(
newMonth
%
12
)
+
1
;
if
(
newMonth
==
1
)
newYear
++
;
}
}
returnDate
.
Initialize
(
newMonth
,
newDay
,
newYear
);
return
returnDate
;
}
Lab2 CSC 240 Files/DateType.h
#ifndef DATETYPE_H #define DATETYPE_H #include <string> #include <fstream> using namespace std; // Declare a class to represent the Date ADT // This is file DateType.h. enum RelationType {LESS, EQUAL, GREATER}; // Compares self with someDate. class DateType { public: void Initialize(int newMonth, int newDay, int newYear); int GetMonth() const; // returns year int GetYear() const; // returns month int GetDay() const; // returns day string GetMonthAsString() const; // returns month as a string DateType Adjust(int daysAway) const; RelationType ComparedTo(DateType someDate) const; private: int year; int month; int day; }; #endif
Lab2 CSC 240 Files/PersonType.cpp
Lab2 CSC 240 Files/PersonType.cpp
/*
* PersonType.cpp
*
* Created on: Jan 28, 2019
* Author: igt88
*/
#include
"PersonType.h"
#include
<
iostream
>
using
namespace
std
;
void
PersonType
::
Initialize
(
string n
,
DateType
dob
){
name
=
n
;
birthdate
=
dob
;
}
string
PersonType
::
NameIs
(){
return
name
;
}
DateType
PersonType
::
BirthdateIs
(){
return
birthdate
;
}
RelationType
PersonType
::
ComparedTo
(
PersonType
&
somePerson
){
return
birthdate
.
ComparedTo
(
somePerson
.
birthdate
);
}
void
PersonType
::
Print
(){
cout
<<
"Name: "
<<
name
<<
endl
;
}
Lab2 CSC 240 Files/PersonType.h
#ifndef PERSONTYPE_H #define PERSONTYPE_H #include <string> using namespace std; #include "DateType.h" class PersonType { public: void Initialize(string, DateType); string NameIs(); RelationType ComparedTo(PersonType& somePerson); DateType BirthdateIs(); void Print(); private: string name; DateType birthdate; }; #endif
Lab2 CSC 240 Files/StudentType.cpp
#include "StudentType.h" #include <iostream> using namespace std; void StudentType::Initialize (string newName, DateType newBirthdate, int newStatus) { status = newStatus; PersonType::Initialize(newName, newBirthdate); } int StudentType::GetStatus() const { return status; } RelationType StudentType::ComparedTo(StudentType& someStudent){ //Compare two StudentType objects based on their status here. } DateType StudentType::BirthdateIs(){ return PersonType::BirthdateIs(); //notice the use of the scope for PersonType here. } void StudentType::Print(){ PersonType::Print(); //use the Print function of PersonType here. cout <<"Status: " << status << endl; }
Lab2 CSC 240 Files/StudentType.h
#ifndef STUDENTTYPE_H #define STUDENTTYPE_H #include "PersonType.h" enum StudentStatus {NON_ATTENDING, ENROLLED, GRADUATED}; //notice the enumeration here for status class StudentType : public PersonType { public: int GetStatus() const; void Initialize(string, DateType, int); RelationType ComparedTo(StudentType& someStudent); DateType BirthdateIs(); void Print(); private: int status; }; typedef StudentType ItemType; //notice this is used to allow ItemType to be identified as a StudentType //The only way around this line of code above would be to use a template, //but the author doesn't get into it until Chapter 6... #endif
Lab2 CSC 240 Files/StudentTypeDriver.cpp
Lab2 CSC 240 Files/StudentTypeDriver.cpp
/*
* StudentTypeDriver.cpp
*
* Created on: Jan 28, 2019
* Author: igt88
*/
#include
"unsorted.h"
#include
<
iostream
>
using
namespace
std
;
int
main
(){
StudentType
student1
,
student2
,
student3
;
DateType
student1DOB
,
student2DOB
,
student3DOB
;
student1DOB
.
Initialize
(
7
,
15
,
1978
);
student2DOB
.
Initialize
(
6
,
23
,
1980
);
student3DOB
.
Initialize
(
3
,
4
,
1945
);
student1
.
Initialize
(
"Ivan"
,
student1DOB
,
ENROLLED
);
student2
.
Initialize
(
"Jim"
,
student2DOB
,
GRADUATED
);
student3
.
Initialize
(
"Billy"
,
student3DOB
,
NON_ATTENDING
);
UnsortedType
classList
;
classList
.
PutItem
(
student1
);
classList
.
PutItem
(
student2
);
//NON_ATTENDING = 0, ENROLLED = 1, GRADUATED = 2
cout
<<
"Compare student1 to student1: "
<<
student1
.
ComparedTo
(
student1
)
<<
endl
;
//expect 1 for EQUAL
cout
<<
"Compare student1 to student2: "
<<
student1
.
ComparedTo
(
student2
)
<<
endl
;
//expect 0 for LESS/Not equal
cout
<<
"Print list 0:\n"
;
classList
.
ResetList
();
//reset the current position to NULL
classList
.
Print
();
cout
<<
"Print list 1:\n"
;
classList
.
DeleteItem
(
student1
);
classList
.
ResetList
();
//this must be reset before printing the list everytime
classList
.
Print
();
cout
<<
"Print list 2:\n"
;
classList
.
PutItem
(
student3
);
classList
.
ResetList
();
//this must be reset before printing the list everytime
classList
.
Print
();
cout
<<
"Print list 3:\n"
;
classList
.
DeleteItem
(
student2
);
classList
.
DeleteItem
(
student3
);
classList
.
ResetList
();
classList
.
Print
();
}
Lab2 CSC 240 Files/unsorted.cpp
Lab2 CSC 240 Files/unsorted.cpp
// This file contains the linked implementation of class
// UnsortedType.
#include
"unsorted.h"
#include
<
iostream
>
using
namespace
std
;
struct
NodeType
{
ItemType
info
;
NodeType
*
next
;
};
UnsortedType
::
UnsortedType
()
// Class constructor
{
length
=
0
;
listData
=
NULL
;
}
bool
UnsortedType
::
IsFull
()
const
// Returns true if there is no room for another ItemType
// on the free store; false otherwise.
{
NodeType
*
location
;
try
{
location
=
new
NodeType
;
delete
location
;
return
false
;
}
catch
(
std
::
bad_alloc exception
)
{
return
true
;
}
}
int
UnsortedType
::
GetLength
()
const
// Post: Number of items in the list is returned.
{
return
length
;
}
void
UnsortedType
::
MakeEmpty
()
// Post: List is empty; all items have been deallocated.
{
NodeType
*
tempPtr
;
while
(
listData
!=
NULL
)
{
tempPtr
=
listData
;
listData
=
listData
->
next
;
delete
tempPtr
;
}
length
=
0
;
}
void
UnsortedType
::
PutItem
(
ItemType
item
)
// item is in the list; length has been incremented.
{
NodeType
*
location
;
// Declare a pointer to a node
location
=
new
NodeType
;
// Get a new node
location
->
info
=
item
;
// Store the item in the node
location
->
next
=
listData
;
// Store address of first node
// in next field of new node
listData
=
location
;
// Store address of new node into
// external pointer
length
++
;
// Increment length of the list
}
ItemType
UnsortedType
::
GetItem
(
ItemType
&
item
,
bool
&
found
)
// Pre: Key member(s) of item is initialized.
// Post: If found, item's key matches an element's key in the
// list and a copy of that element has been stored in item;
// otherwise, item is unchanged.
{
bool
moreToSearch
;
NodeType
*
location
;
location
=
listData
;
found
=
false
;
moreToSearch
=
(
location
!=
NULL
);
while
(
moreToSearch
&&
!
found
)
{
switch
(
item
.
ComparedTo
(
location
->
info
))
{
case
LESS
:
case
GREATER
:
location
=
location
->
next
;
moreToSearch
=
(
location
!=
NULL
);
break
;
case
EQUAL
:
found
=
true
;
item
=
location
->
info
;
break
;
}
}
return
item
;
}
void
UnsortedType
::
DeleteItem
(
ItemType
item
)
// Pre: item's key has been initialized.
// An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
NodeType
*
location
=
listData
;
NodeType
*
tempLocation
;
// Locate node to be deleted.
if
(
item
.
ComparedTo
(
listData
->
info
)
==
EQUAL
)
{
tempLocation
=
location
;
listData
=
listData
->
next
;
// Delete first node.
}
else
{
while
(
item
.
ComparedTo
((
location
->
next
)
->
info
)
!=
EQUAL
)
location
=
location
->
next
;
// Delete node at location->next
tempLocation
=
location
->
next
;
location
->
next
=
(
location
->
next
)
->
next
;
}
delete
tempLocation
;
length
--
;
}
void
UnsortedType
::
ResetList
()
// Post: Current position has been initialized.
{
currentPos
=
NULL
;
}
ItemType
UnsortedType
::
GetNextItem
()
// Post: A copy of the next item in the list is returned.
// When the end of the list is reached, currentPos
// is reset to begin again.
{
ItemType
item
;
if
(
currentPos
==
NULL
)
currentPos
=
listData
;
else
currentPos
=
currentPos
->
next
;
item
=
currentPos
->
info
;
return
item
;
}
UnsortedType
::~
UnsortedType
()
// Post: List is empty; all items have been deallocated.
{
NodeType
*
tempPtr
;
while
(
listData
!=
NULL
)
{
tempPtr
=
listData
;
listData
=
listData
->
next
;
delete
tempPtr
;
}
}
void
UnsortedType
::
Print
(){
//Complete this.
}
Lab2 CSC 240 Files/unsorted.h
#ifndef UNSORTED_H #define UNSORTED_H // File ItemType.h must be provided by the user of this class. // ItemType.h must contain the following definitions: // MAX_ITEMS: the maximum number of items on the list // ItemType: the definition of the objects on the list // RelationType: {LESS, GREATER, EQUAL} // Member function ComparedTo(ItemType item) which returns // LESS, if self "comes before" item // GREATER, if self "comes after" item // EQUAL, if self and item are the same struct NodeType; #include "StudentType.h" class UnsortedType { public: UnsortedType(); // Constructor ~UnsortedType(); // Destructor void MakeEmpty(); // Function: Returns the list to the empty state. // Post: List is empty. bool IsFull() const; // Function: Determines whether list is full. // Pre: List has been initialized. // Post: Function value = (list is full) int GetLength() const; // Function: Determines the number of elements in list. // Pre: List has been initialized. // Post: Function value = number of elements in list ItemType GetItem(ItemType& item, bool& found); // Function: Retrieves list element whose key matches item's key (if // present). // Pre: List has been initialized. // Key member of item is initialized. // Post: If there is an element someItem whose key matches // item's key, then found = true and someItem is returned; // otherwise found = false and item is returned. // List is unchanged. void PutItem(ItemType item); // Function: Adds item to list. // Pre: List has been initialized. // List is not full. // item is not in list. // Post: item is in list. void DeleteItem(ItemType item); // Function: Deletes the element whose key matches item's key. // Pre: List has been initialized. // Key member of item is initialized. // One and only one element in list has a key matching item's key. // Post: No element in list has a key matching item's key. void ResetList(); // Function: Initializes current position for an iteration through the list. // Pre: List has been initialized. // Post: Current position is prior to list. ItemType GetNextItem(); // Function: Gets the next element in list. // Pre: List has been initialized and has not been changed since last call. // Current position is defined. // Element at current position is not last in list. // // Post: Current position is updated to next position. // item is a copy of element at current position. void Print(); //Complete this... private: NodeType* listData; int length; NodeType* currentPos; }; #endif