Created
April 24, 2015 23:42
-
-
Save anonymous/d5456fe057b169840ffe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "linkedList.h" | |
#include <iostream> | |
#ifndef LINKEDLIST_CPP | |
#define LINKEDLIST_CPP | |
using namespace std; | |
//Destructor | |
template<class T> | |
linkedList<T>::~linkedList() | |
{ | |
node *temp=new node; | |
while(head->!=NULL) | |
{ | |
temp=head; | |
head=head->next; | |
delete temp; | |
} | |
} | |
//Copy Constructor | |
template <class T> | |
linkedList<T>::linkedList(const linkedList &list) | |
{ | |
node *temp =new node; | |
temp =list.head; | |
delete this; | |
while(temp->next!=NULL) | |
{ | |
orderedInsert(temp->data); | |
temp=temp->next; | |
} | |
} | |
//Assignment Operator | |
template <class T> | |
linkedList<T> &linkedList<T>::operator=(const linkedList &list) | |
{ | |
if(this != &list) | |
{ | |
current =head; | |
while(current->next!=NULL) | |
{ | |
head=head->next; | |
delete current; | |
current=head; | |
} | |
current=list.head; | |
while(current->!=NULL) | |
{ | |
orderedInsert(current->data); | |
current=current->next; | |
} | |
} | |
return *this; | |
} | |
template <class T> | |
bool linkedList<T>::empty() const | |
{ | |
current=head; | |
if(current==NULL) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
template <class T> | |
void linkedList<T>::clear() | |
{ | |
delete *this; | |
} | |
template <class T> | |
bool linkedList<T>::search(const T &value) | |
{ | |
current=head; | |
while(current->data!=value||current->!=NULL) | |
{ | |
current=current->next; | |
} | |
if(current->data==value) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
template <class T> | |
void linkedList<T>::orderedInsert(const T &value) | |
{ | |
current=head; | |
while(value>current->data) | |
{ | |
trailCurrent=current; | |
current=current->next; | |
} | |
trailCurrent->next=new node(value,current); | |
} | |
template <class T> | |
bool linkedList<T>::remove(const T &value) | |
{ | |
node *temp; | |
temp =head; | |
if(search(value)!=true) | |
{ | |
return false; | |
} | |
else | |
{ | |
trailCurrent->next=current->next; | |
temp=current; | |
current=current->next; | |
delete temp; | |
return true; | |
} | |
} | |
template <class T> | |
bool linkedList<T>::replace(const T &oldData,const T &newData) | |
{ | |
if(search(oldData)==false) | |
{ | |
return false; | |
} | |
else | |
{ | |
current=head; | |
while(current->data!=oldData) | |
{ | |
current=current->next; | |
} | |
current->data=newData; | |
} | |
return true; | |
} | |
template <class T> | |
void linkedList<T>::insert(const T &value) | |
{ | |
if(trailCurrent==NULL) | |
{ | |
trailCurrent=head; | |
head =new node(value,trailCurrent); | |
trailCurrent=head; | |
} | |
else if(trailCurrent->next->data!=current->data) | |
{ | |
trailCurrent=head; | |
while(trailCurrent->next->data!=current->data) | |
{ | |
trailCurrent=trailCurrent->next; | |
} | |
node *temp; | |
temp=trailCurrent; | |
temp=new node(value,current); | |
trailCurrent->next=temp; | |
} | |
return true; | |
} | |
template <class T> | |
bool linkedList<T>::retrieve(T &value)const | |
{ | |
if(current==NULL) | |
{ | |
return false; | |
} | |
else | |
{ | |
current->data =value; | |
return true; | |
} | |
} | |
template <class T> | |
void linkedList<T>::begin() | |
{ | |
current=head; | |
trailCurrent=NULL; | |
} | |
template <class T> | |
linkedList linkedList<T>::operator++() | |
{ | |
if(current!=NULL) | |
{ | |
current=current->next; | |
trailCurrent=trailCurrent->next; | |
return *this; | |
} | |
} | |
template<class T> | |
linkedList linkedList<T>::operator++(int i) | |
{ | |
if(current!=NULL) | |
{ | |
trailCurrent=current; | |
current=current->next; | |
} | |
return that; | |
} | |
template <class T> | |
ostream &operator<<(ostream &outStream,linkedList<T> list) | |
{ | |
T element; | |
this.current=this.head; | |
while(this.current->!=NULL) | |
{ | |
element=this.retrieve(element); | |
outStream<<"["<<element<<"]"<<endl; | |
this.current++; | |
} | |
return outStream; | |
} | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
#ifndef LINKEDLIST_H | |
#define LINKEDLIST_H | |
template <class T> | |
class linkedList | |
{ | |
public: | |
//default constructor | |
//Description: sets all class pointer to null | |
//Parameters: none | |
//Return: none | |
linkedList() {head = NULL; current = NULL; trailCurrent = NULL;} | |
//The Big 3 | |
//copy constructor | |
//Description: Copy constructor does a deep copy | |
//Parameters: list to copy from | |
//Return: none | |
linkedList(const linkedList &list); | |
//operator= | |
//Description: Assignment operator does a deep copy | |
//Parameter: list to copy form | |
//Return: the instance of the lit | |
linkedList& operator=(const linkedList &list); | |
//destructor | |
//Description: Destructor deletes all the nodes in the list | |
//Paramters: none | |
//Return : none | |
~linkedList(); | |
//empty | |
//Description: returns true if the list is empty, false otherwise | |
//Paramters: none | |
//Return: true if empty, false otherwise | |
bool empty() const; | |
//clear | |
//Description: clears out all the nodes in the list and sets head to NULL | |
//Paramters: none | |
//Return: none | |
void clear(); | |
//search | |
//Description: tries to locate value | |
// If found, returns true and current points to the value | |
// If not found, returns false and current cannot be trusted | |
//Parameters: value to seach for | |
//Return: true if found, false othewise | |
bool search(const T &value); | |
//orderedInsert | |
//Description: takes the value an inserts it in the linked list so that | |
// the values in the list are in ascending order | |
//Parameters: value to insert | |
//Return: none | |
void orderedInsert(const T&value); | |
//remove | |
//Description: removes the value from the list | |
// remove fails if there value is not found in the list | |
//Paramter: value to remove | |
//Return: true if removed, false otherwise | |
bool remove(const T &value); | |
//replace | |
//Description: replaces the oldData with the newData | |
//Parameters: T oldData - IN data to find | |
// T newData - IN data to put | |
//Return: True if the oldData is found and false otherwise | |
bool replace(const T &oldData, const T &newData); | |
//insert | |
//Description: inserts value before the before current and after trailCurrent | |
// If trailCurrent is NULL, it inserts at the head of the list | |
// trailCurrent is set to point to the new node | |
//Paramters: value to insert | |
//Return: none | |
void insert(const T &value); | |
//retrieve | |
//Description: retrieves the value at the current position | |
// value is returned through the parameter | |
//Paramterss: variable for value to retrieve | |
//Return: True if the position is valid and false otherwise | |
bool retrieve(T &value) const; | |
//The following routines are for traversing the list in an application | |
//begin | |
//Description: sets the current position to the beginning of the list | |
// It also sets the trailCurrent to NULL | |
//Paramters: none | |
//Return: none | |
void begin(); | |
//operator ++ | |
// Description: as long as the current position is not NULL, it | |
// moves the current position (and trailCurrent) to next item | |
// Two prototypes are given so the operator can be used both prefix and postfix. | |
//Parameters: none (int i as a dummy) | |
//Return: the list | |
linkedList operator++(); | |
linkedList operator++(int i); //dummy param | |
private: | |
class node | |
{ | |
public: | |
node() {next = NULL;} | |
node(const T &value, node *p= NULL) {data = value; next = p;} | |
T data; | |
node *next; | |
}; | |
//attributes | |
node *head; //points to the beginning of the list | |
node *current; //marks a node in the traversal | |
node *trailCurrent; //keeps track of what's before current | |
}; | |
//ostream operator for printing the list | |
template <class T> | |
ostream &operator<<(ostream &output,linkedList<T> list); | |
#include "linkedList.cpp" | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "linkedList.h" | |
#include "myDate.h" | |
#include <iostream> | |
using namespace std; | |
#include <fstream> | |
//FillList | |
//Description: opens a file and fills the list from the file | |
//Parameters: indexList | |
//Return: none | |
void FillList(linkedList<myDate> &list); | |
//DisplayList | |
//Description: displays the list to the monitor | |
//Parameters: indexList | |
//Return: none | |
void DisplayList(linkedList<myDate> list); | |
//AddToList | |
//Description: adds to the list by ordered insert | |
//Parameters: indexList,num | |
//Return: none | |
void AddToList(linkedList<myDate> &list); | |
//GetTodaysAppointments | |
//Description: Asks the user for today's date, and looks for a matching date from the list | |
//Parameters: indexList,num | |
//Return: none | |
void GetTodaysAppointments(linkedList<myDate> list); | |
//ChangeAppointment | |
//Description: Lets the user choose an appointment to change, and changes as such | |
//Parameters: indexList | |
//Return: None | |
void ChangeAppointment(linkedList<myDate> &list); | |
//Menu | |
int menu(); | |
int main() | |
{ | |
int choice; | |
int num; | |
linkedList<myDate> L; | |
while ((choice = menu()) != 6) | |
{ | |
switch (choice) | |
{ | |
case 1: FillList(L); break; | |
case 2: AddToList(L); break; | |
case 3: GetTodaysAppointments(L); break; | |
case 4: ChangeAppointment(L); break; | |
case 5: DisplayList(L); break; | |
} | |
} | |
return 0; | |
} | |
//menu | |
int menu() | |
{ | |
int ch; | |
cout << endl; | |
cout << "1. Fill from file" << endl; | |
cout << "2. Add to the list" << endl; | |
cout << "3. Get Todays Appointments" << endl; | |
cout << "4. Change an appointment" << endl; | |
cout << "5. Display list" << endl; | |
cout << "6. Quit"<<endl; | |
cout << "Choice: "; | |
cin >> ch; | |
return ch; | |
} | |
//FillList | |
//Description: opens a file and fills the list from the file | |
//Parameters: indexList | |
//Return: none | |
void FillList(linkedList<myDate> &list) | |
{ | |
myDate aDate; | |
myTime aTime; | |
ifstream f; | |
bool result =true; | |
string fname; | |
cout << "File: "; | |
cin >> fname; | |
f.open(fname.c_str()); | |
if (f.fail()) | |
{ | |
cout << "Failed to open" << endl; | |
return; | |
} | |
list.begin(); | |
f >> aDate; | |
while (result && !f.eof()) | |
{ | |
f>>aTime; | |
aDate.setTime(aTime); | |
list.orderedInsert(aDate); | |
f>>aDate; | |
} | |
f.close(); | |
} | |
//DisplayList | |
//Description: displays the list to the monitor | |
//Parameters: indexList | |
//Return: none | |
void DisplayList(linkedList<myDate> list) | |
{ | |
cout << endl; | |
cout << list; | |
} | |
//AddToList | |
//Description: adds to the list, by ordered insert | |
//Parameters: indexList, num | |
//Return: none | |
void AddToList(linkedList<myDate> &list) | |
{ | |
char answer; | |
myDate value; | |
myTime aTime; | |
int index; | |
bool result; | |
cout << "Date to add(Month/Day/Year): "; | |
cin >> value; | |
cout<<"Time to add(Hours:Mins): "; | |
cin>>aTime; | |
value.setTime(aTime); | |
list.orderedInsert(value); | |
} | |
//GetTodaysAppointment | |
//Description: Retrieves appointments based on "today" and compares to the list of appointments | |
//Parameters: indexList,num | |
//Return: none | |
void GetTodaysAppointments(linkedList<myDate> list) //Tests the Copy constructor | |
{ | |
int index=0; | |
myDate today; | |
myDate value; | |
cout<<"Please Enter the Month/Day/Year"<<endl; | |
cin>>today; | |
while(list.search(today)==true) | |
{ | |
list.retrieve(value); | |
if(today==value) | |
{ | |
cout<<"Todays Appointments are at : "<<value<<endl; | |
} | |
list.remove(today); | |
} | |
} | |
//ChangeAppointment | |
//Description: Asks the user for a date, if found it should remove that appointment, then request the new date and | |
//time and call the ordered Insert | |
//Parameters: indexList | |
//Return: none | |
void ChangeAppointment(linkedList<myDate> &list) | |
{ | |
myDate date; | |
myDate changeto; | |
myTime changetotime; | |
myDate value; | |
int index; | |
cout<<"Please Enter the appointment index you want to change"<<endl; | |
cin>>index; | |
while(index<0) | |
{ | |
cout<<"Please Enter the appointment index you want to change"<<endl; | |
cin>>index; | |
} | |
list.retrieve(date); | |
list.search(date); | |
if(index != -1) | |
{ | |
cout<<"What date do you want to change the appointment to?"<<endl; | |
cin>>changeto; | |
cout<<"What time do you want to change the appointment to? "<<endl; | |
cin>>changetotime; | |
changeto.setTime(changetotime); | |
list.replace(date,changeto); | |
} | |
else | |
cout<<"That appointment cannot be found"<<endl; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app: myTime.o myDate.o linkedList.o linkedListApp.o | |
g++ -o app myTime.o myDate.o linkedList.o linkedListApp.o | |
myTime.o: myTime.h myTime.cpp | |
g++ -c myTime.cpp | |
myDate.o: myTime.h myDate.h myDate.cpp | |
g++ -c myDate.cpp | |
linkedList.o: linkedList.h linkedList.cpp | |
g++ -c linkedList.cpp | |
linkedListApp.o: myDate.h linkedList.h linkedList.cpp linkedListApp.cpp | |
g++ -c linkedListApp.cpp | |
clean: | |
rm *.o app | |
touch * | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "myDate.h" | |
using namespace std; | |
//Converts int to string. | |
string to_string(int integer) | |
{ | |
string s; | |
stringstream out; | |
out << integer; | |
s = out.str(); | |
return s; | |
} | |
// Input and output handling | |
//Prints out the date and time in order | |
ostream &operator<<(ostream &output, const myDate &date) | |
{ | |
output <<to_string(date.getMonth()) << "/"<< to_string(date.getDay()) << "/" | |
<< to_string(date.getYear()) << " " | |
<< date.getTime() ; | |
return output; | |
} | |
//Reads in the date in order | |
istream &operator>>(istream &input, myDate &date) | |
{ | |
int day; | |
int month; | |
int year; | |
input >> month; | |
input.get(); | |
input >> day; | |
input.get(); | |
input >> year; | |
date.setDay(day); | |
date.setMonth(month); | |
date.setYear(year); | |
return input; | |
} | |
// Constructors | |
myDate::myDate() | |
{ | |
day = 1; | |
year = 2015; | |
month = 1; | |
} | |
// Getters and setters | |
void myDate::setMonth(int mnth) | |
{ | |
if(mnth < 1 || mnth > 12) | |
{ | |
//error here | |
// the operation does not have effect | |
} | |
else | |
month = mnth; | |
} | |
void myDate::setDay(int dy) | |
{ | |
if (dy < 0 || dy > daysInMonth() ) | |
{ | |
//ERROR HERE | |
} | |
day = dy; | |
} | |
void myDate::setYear(int yr) | |
{ | |
//accepts any year | |
year = yr; | |
} | |
void myDate::setTime(const myTime &tm) | |
{ | |
theTime = tm; | |
} | |
int myDate::getMonth() const | |
{ | |
return month; | |
} | |
int myDate::getDay() const | |
{ | |
return day; | |
} | |
int myDate::getYear() const | |
{ | |
return year; | |
} | |
myTime myDate::getTime() const | |
{ | |
return theTime; | |
} | |
// Operator overloading | |
bool myDate::operator < (const myDate &otherDate)const | |
{ | |
return compareDates(otherDate) < 0; | |
} | |
bool myDate::operator==(const myDate &otherDate) const | |
{ | |
return compareDates(otherDate) == 0; | |
} | |
bool myDate::operator>(const myDate &otherDate) const | |
{ | |
return compareDates(otherDate) > 0; | |
} | |
bool myDate::operator<=(const myDate &otherDate) const | |
{ | |
return compareDates(otherDate) < 1; | |
} | |
bool myDate::operator>=(const myDate &otherDate) const | |
{ | |
return compareDates(otherDate) > -1; | |
} | |
bool myDate::operator!=(const myDate &otherDate) const | |
{ | |
return compareDates(otherDate) != 0; | |
} | |
// Private helper functions | |
bool myDate::isLeapYear()const | |
{ | |
return (!(year%4) && year%100 ) || !(year%400); | |
} | |
int myDate::daysInMonth()const{ | |
switch(month) | |
{ | |
case 1: | |
case 3: | |
case 5: | |
case 7: | |
case 8: | |
case 10: | |
case 12: | |
return 31; | |
case 2: | |
return 28 + isLeapYear(); | |
default: | |
return 30; | |
} | |
} | |
//shows actual name of the month | |
string myDate::toLongDate() | |
{ | |
static string monthStr[] = {"January","February","March","April", "May"," June","July","August","September","October"," November","December"}; | |
string dateString = monthStr[month-1] + " " + to_string(day) ; | |
dateString += ", " + to_string(year); | |
return dateString; | |
} | |
//Adds the inputed days to move the appointments | |
myDate & myDate::operator+=(int days) | |
{ | |
if(days < 0)return *this; | |
int toFinishMonth; | |
while(true){ | |
toFinishMonth = daysInMonth() - day; | |
if (toFinishMonth >= days)break; | |
month = ( month )%12 + 1; | |
year += (month == 1); | |
days -= toFinishMonth + 1; | |
day = 1; | |
} | |
day += days; | |
return *this; | |
} | |
int myDate::compareDates(const myDate &secondDate) const | |
{ | |
int yearDiff = year - secondDate.getYear(); | |
int monthDiff = month - secondDate.getMonth(); | |
int dayDiff = day - secondDate.getDay(); | |
if(yearDiff) | |
return yearDiff; | |
else if(monthDiff) | |
return monthDiff; | |
else | |
return dayDiff; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include "myTime.h" | |
using namespace std; | |
#ifndef MYDATE_H | |
#define MYDATE_H | |
class myDate | |
{ | |
public: | |
//Default constructor | |
//Description: sets month to 1, day to 1, year to 2015 | |
//Parameters: none | |
myDate(); | |
//sets and gets perform as expected | |
void setMonth(int mnth); | |
void setDay(int dy); | |
void setYear(int yr); | |
void setTime(const myTime &tm); | |
int getMonth() const; | |
int getDay() const; | |
int getYear() const; | |
myTime getTime() const; | |
//relational operators: compare full date and time | |
//A later date/time is "greater" than an earlier one | |
bool operator==(const myDate &date) const; | |
bool operator<(const myDate &date) const; | |
bool operator>(const myDate &date) const; | |
bool operator<=(const myDate &date) const; | |
bool operator>=(const myDate &date) const; | |
bool operator!=(const myDate &date) const; | |
//Name: compareDates | |
//Description: It will compare two dates, not including the times | |
//Parameters: myDate secondDate - IN - the date to compare | |
//Return: -1 if the instance is ealier than secondDate | |
int compareDates(const myDate &secondDate) const; | |
//Name: operator+= | |
//Description: Moves the date a head days days | |
//Parameters: days, the number of days to advance the date | |
//Return: the instance of the object | |
myDate &operator+=(int days); | |
//Name: toLongDate | |
//Description: creates a string for the date using the actual month name | |
// for example 1/2/2012 would return January 2, 2012 | |
//Return: the long version of the date | |
string toLongDate(); | |
private: | |
int month, day, year; | |
myTime theTime; | |
//Name: isLeapYear | |
//Description: Determines if the year is a leap year | |
//A year is a leap year under the following rules: | |
//if not divisible by 4, it is not a leap year | |
//if divisible by 4 and not 100, it is a leap year | |
//if divisible by 4 and 100, it must be be divisible by | |
//400 to be a leap year | |
//Parameters: none | |
//Return: true if leap year, false otherwise | |
bool isLeapYear() const; | |
//Name: daysInMonth | |
//Description: uses the month and year to determine the total days | |
//Paraemters: none | |
//Return: the number of days in the month of the instance | |
//remember that February depends on if it is a leap year | |
int daysInMonth() const; | |
//Name:convertIntToString | |
//Description: Code included for your convenience: YOU DO NOT HAVE TO WRITE THIS FUNCTION | |
//This function will return the string version of the integer it is passed | |
//Parameters: int i - the interger to convert | |
//Return: the string corresponding to the integer i | |
string convertIntToString(int i) | |
{ | |
string s; | |
stringstream out; | |
out << i; | |
s = out.str(); | |
return s; | |
} | |
}; | |
//Name: operator<< | |
//Description: writes out the date in the form month/day/year hours:minutes | |
//Parameters: ostream output - IN/OUT the stream written to | |
// myDate date - IN - the date to write | |
//Return: ostream - the stream written to | |
ostream &operator<<(ostream &output, const myDate &date); | |
//Name: operator>> | |
//Description: Expects the user to enter a date in the form month/day/year | |
// reads ONLY the date part not the time | |
//Parameters: istream input - IN/OUT the stream read from | |
// myDate date - OUT the date to fill | |
//Return: istream - the stream read from | |
istream &operator>>(istream &input, myDate &date); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "myTime.h" | |
#include <iostream> | |
#include <iomanip> | |
using namespace std; | |
//Constructor | |
//Starts time at 0:01 | |
myTime::myTime() | |
{ | |
hours = 0; | |
minutes = 1; | |
} | |
//This constructor takes initial minutes and hours | |
myTime::myTime(int hrs, int min) | |
{ | |
hours = hrs; | |
minutes = min; | |
} | |
//gets and sets | |
//Sets provided for name, position and number only | |
void myTime::setMinutes(int mins) {minutes = mins;} | |
void myTime::setHours(int hrs) {hours = hrs;} | |
//gets provided for all data attributes | |
int myTime::getMinutes() const {return minutes;} | |
int myTime::getHours() const {return hours;} | |
//advances the time hrs hours | |
void myTime::addHours(int hrs) | |
{ | |
hours += hrs; | |
simplifyTime(); | |
} | |
//moves back the time hrs hours | |
void myTime::subtractHours(int hrs) | |
{ | |
hours -= hrs; | |
simplifyTime(); | |
} | |
//Displays the time in 12 hours form with am or pm | |
//The time of 3:45 would display as 3:45 am | |
//The time of 14:32 would display as 2:32 pm | |
void myTime::showClock() const | |
{ | |
if (hours == 0) | |
{ | |
cout << 12 << ":" << setw(2) << setfill('0') << minutes << " am"; | |
return; | |
} | |
if (hours < 13) | |
{ | |
if (hours < 12) | |
cout << hours << ":" << setw(2) << setfill('0') << minutes << " am"; | |
else | |
cout << hours << ":" << setw(2) << setfill('0') << minutes << " pm"; | |
} | |
else | |
{ | |
if (hours < 24) | |
{ | |
int hrs = hours -12; | |
cout << hrs << ":" << setw(2) << setfill('0') << minutes << " pm"; | |
} | |
} | |
} | |
//finds the minutes between the instance time and the nextTime | |
//If nextTime after instance, returns -1 | |
int myTime::timeDiff(const myTime &nextTime) const | |
{ | |
int mins; | |
if (*this > nextTime) | |
{ | |
if (minutes > nextTime.getMinutes()) | |
{ | |
mins = minutes - nextTime.getMinutes(); | |
mins += (hours - nextTime.getHours()) * 60; | |
} | |
else | |
{ | |
mins = (minutes + 60) - nextTime.getMinutes(); | |
mins += (hours -1 - nextTime.getHours()) * 60; | |
} | |
return mins; | |
} | |
else | |
return -1; | |
} | |
//All objects are compared by time of day | |
//First by the hours then by the minutes | |
bool myTime::operator==(const myTime &tm) const | |
{ | |
return (hours == tm.getHours() && minutes == tm.getMinutes()); | |
} | |
bool myTime::operator!=(const myTime &tm) const | |
{ | |
return (hours != tm.getHours() || minutes != tm.getMinutes()); | |
} | |
bool myTime::operator<(const myTime &tm) const | |
{ | |
if (hours < tm.getHours()) | |
return true; | |
else if (hours > tm.getHours()) | |
return false; | |
else if (minutes < tm.getMinutes()) | |
return true; | |
else | |
return false; | |
} | |
bool myTime::operator>(const myTime &tm) const | |
{ | |
if (hours > tm.getHours()) | |
return true; | |
else if (hours < tm.getHours()) | |
return false; | |
else if (minutes > tm.getMinutes()) | |
return true; | |
else | |
return false; | |
} | |
bool myTime::operator>=(const myTime &tm) const | |
{ | |
if (*this > tm || *this == tm) | |
return true; | |
else | |
return false; | |
} | |
bool myTime::operator<=(const myTime &tm) const | |
{ | |
if (*this < tm || *this == tm) | |
return true; | |
else | |
return false; | |
} | |
//simplifies the time to make sure the hours and minutes | |
//are within the 24 hour clock | |
void myTime::simplifyTime() | |
{ | |
if (minutes >= 60) | |
{ | |
minutes -= 60; | |
hours += 1; | |
} | |
if (hours > 23) | |
hours -= 24; | |
if (minutes <0) | |
{ | |
minutes += 60; | |
hours -= 1; | |
} | |
if (hours < 0) | |
hours += 24; | |
} | |
//moves the time forward min minutes | |
myTime &myTime::operator+=(int min) | |
{ | |
minutes += min; | |
simplifyTime(); | |
return *this; | |
} | |
//moves the time back min minutes | |
myTime &myTime::operator-=(int min) | |
{ | |
minutes -= min; | |
simplifyTime(); | |
return *this; | |
} | |
//overload of the stream output operator | |
//Displays time for the 24 hour clock in the form hours:minutes | |
ostream &operator<<(ostream &fileOut, const myTime &tm) | |
{ | |
fileOut << tm.getHours() << ":" << setw(2) << setfill('0') << tm.getMinutes(); | |
return fileOut; | |
} | |
//overload of the stream input operator | |
//Reads time from the input form hours:minutes | |
istream &operator>>(istream &fileIn, myTime &tm) | |
{ | |
int min, hr; | |
char colon; | |
fileIn >> hr >> colon >> min; | |
tm.setHours(hr); | |
tm.setMinutes(min); | |
return fileIn; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
#ifndef MYTIME_H | |
#define MYTIME_H | |
class myTime | |
{ | |
public: | |
//constructor | |
//myTime | |
//Initializes hours to 24 and minutes to 1 | |
myTime(); | |
//Initializes hours to hrs and minutes to mins | |
myTime(int hrs, int mins); | |
//gets and sets | |
//Sets provided for hours and minutes | |
void setMinutes(int mins); | |
void setHours(int hrs); | |
//gets provided for hours and minutes | |
int getMinutes() const; | |
int getHours() const; | |
//Name: addHours | |
//Description: advances the time hrs hours | |
//Parameters: int hrs - IN number of hours to advance | |
//Return: none | |
void addHours(int hrs); | |
//Name: subtractHours | |
//Description: moves back the time hrs hours | |
//Parameters: int hrs - IN hours to subtrace | |
//Return: none | |
void subtractHours(int hrs); | |
//Name: showClock | |
//Description: Displays the time in 12 hours form with am or pm | |
//13:10 would be 1:10 pm | |
//Paramters: none | |
//Return: none | |
void showClock() const; | |
//Name: timeDiff | |
//Description: finds the number of minutes from the instance time to the time passed in nextTime | |
//expects nextTime to be later | |
//if nextTime is after the time of the instance it returns -1 | |
//Parameters: myTime nextTime - IN the time to figure difference from | |
//Return: number of minutes between or -1 if order wrong | |
int timeDiff(const myTime &nextTime) const; | |
//All objects are compared by the time of day | |
//earlier in the day means "less than" | |
bool operator==(const myTime &tm) const; | |
bool operator!=(const myTime &tm) const; | |
bool operator<(const myTime &tm) const; | |
bool operator>(const myTime &tm) const; | |
bool operator>=(const myTime &tm) const; | |
bool operator<=(const myTime &tm) const; | |
//Name: operator+= | |
//Description: advances the time min minutes | |
//Parameters: int min - IN minutes to advance | |
//Return: the object itself | |
myTime &operator+=(int min); | |
//Name: operator -= | |
//Description: moves the time back min minutes | |
//Parameters: int min - IN the number minutes to subtract | |
//Return: the object itself | |
myTime &operator-=(int min); | |
private: | |
int hours, minutes; | |
//Name: simplifyTime | |
//Description: simplifies the time making sure the hours and minutes fit the 24 ho\ | |
ur clock | |
//Parameters: none | |
//Return: none | |
void simplifyTime(); | |
}; | |
//overload of the stream output operator | |
//Displays all data in nice, neat form. | |
//hours:minutes | |
ostream &operator<<(ostream &fileOut, const myTime &tm); | |
//overload of the stream input operator | |
//Reads time in the input form hours:minutes | |
istream &operator>>(istream &fileIn, myTime &tm); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment