• No results found

Here are the classes that are changed.

In document Answers to Selected Exercises (Page 133-149)

b Note that the calling code below does not print an error message The error message has already been printed by OpenForInput , as advertised by its postcondition.

4. Here are the classes that are changed.

//**************************************************************** // SPECIFICATION FILE (AptTime.h)

// This file gives the specification of an AptTime ADT with // action responsibilities and knowledge reponsibilities

//**************************************************************** #ifndef APT_TIME #define APT_TIME #include "relType.h" class AptTime { public:

void Set( /* in */ int hours, /* in */ int minutes ); // Precondition:

// 0 <= hours <= 23 && 0 <= minutes <= 59 // Postcondition:

// hrs == hours && mins == minutes void Write() const;

// Postcondition:

// Time has been output in the form HH:MM int Hours() const;

// Postcondition:

// Return value is time

Exceptions

Input Values

Expected Output

Observed

Output

DuplicateTime DuplicateDay EntryListIsFull UndefinedString UndefinedDay

InsertDay 1 3 2004 2

InsertApt 1 3 2004 10:30

InsertApt 1 3 2004 10:30

InsertDay 1 3 2004

InsertApt 1 3 2004 9:30

InsertApt 1 3 2004

Insertpt

InsertApt 1 4 2004

DeleteApt 1 4 2004

IsFree 1 4 2004

PrintDay 1 4 2004

Entry with same time is already in the list."

Day with same date is already defined."

Entry list is full for this date."

"Input string was not valid." "Read menu and try again." "An operation has been defined on an undefined day"

int Minutes() const; // Postcondition:

// Return value is minutes

RelationType ComparedTo( /* in */ AptTime otherTime ) const; // Precondition:

// This and input parameter contains valid times // Postcondition:

// Return value is

// BEFORE, if this time comes before otherTime // SAME, if this time and otherTime are the same // AFTER, if this time comes after otherTime AptTime( /* in */ int initHrs,

/* in */ int initMins); // Precondition:

// 0 <= initHrs <= 23 AND 0 <= initMins <= 59 // Postcondition:

// Class object is constructed

// AND Time is set according to the incoming parameters AptTime();

// Postcondition:

// hours and minutes have been set to 0 void ReadTime();

// Postcondition:

// hours and minutes have been prompted for, read, // and set private: int hrs; int mins; }; #endif //****************************************************************** // IMPLEMENTATION FILE (AptTime.cpp)

// This file implements the AptTime member functions

//****************************************************************** #include "aptTimeEx.h"

using namespace std;

// Private members of class: // int hrs;

// int mins;

//****************************************************************** void AptTime::Set( /* in */ int hours,

/* in */ int minutes ) // Precondition:

// 0 <= hours <= 23 && 0 <= minutes <= 59 //

// Postcondition:

// hrs == hours && mins == minutes {

hrs = hours; mins = minutes; }

//******************************************************************

void AptTime::Write() const // Postcondition:

// AptTime has been output in the form HH:MM { if (hrs < 10) cout << '0'; cout << hrs << ':'; if (mins < 10) cout << '0'; cout << mins; } //****************************************************************** // Precondition:

// This and input parameter contains valid times // Postcondition:

// Return value is

// BEFORE, if this time comes before otherTime // SAME, if this time and otherTime are the same // AFTER, if this time comes after otherTime

{

if (hrs == otherAptTime.hrs && mins == otherAptTime.mins) return SAME;

else

if (hrs < otherAptTime.hrs ||

hrs == otherAptTime.hrs && mins < otherAptTime.mins) return BEFORE;

}

//****************************************************************** int AptTime::Hours() const

// Postcondition: // Return value is hrs { return hrs; } //****************************************************************** int AptTime::Minutes() const

// Postcondition:

// Return value is mins { return mins; } //****************************************************************** void AptTime::ReadTime() // Postcondition:

// hours, minutes, and seconds have been prompted for, read, // and stored into hrs and mins

{

cout << "Enter hours (<= 23): " << endl; cin >> hrs;

cout << "Enter minutes (<= 59): " << endl; cin >> mins; } //****************************************************************** AptTime::AptTime() // Default constructor // Postcondition

{

hrs = 0; mins = 0; }

//****************************************************************** AptTime::AptTime( /* in */ int hours,

/* in */ int minutes ) // Precondition:

// 0 <= hours <= 23 && 0 <= minutes <= 59 // && 0 <= seconds <= 59

// Postcondition:

// hrs == hours && mins == minutes {

hrs = hours; mins = minutes; }

//***************************************************************** // SPECIFICATION FILE (Entry.h)

// This file contains the specification of the Entry ADT, // which has two contained classes, Name and AptTime

//***************************************************************** #ifndef ENTRY #define ENTRY #include "AptTimeEx.h" #include "Name.h" #include <string> #include "relType.h" using namespace std; class Entry { public:

string NameStr() const;

// Returns a string made up of first name, blank, last name // Postcondition:

// Return value is first name of Name object, blank, and // last name of Name object

string TimeStr() const;

// Postcondition:

// Return value is hours from Time object, colon, and // minutes of Time object

RelationType ComparedTo(Entry entry) const; // Compares time with entry.time

// Postcondition:

// BEFORE, if this time comes before entry.time // SAME, if this time and entry.time are the same // AFTER, if this time is after entry.time

Entry();

// Default constructor // Postcondition:

// Entry object has been constructed

Entry( /* in */ string firstName, // First name /* in */ string middleName, // Middle name /* in */ string lastName, // Last name /* in */ int initHours, // Hours /* in */ int initMinutes ); // Minutes // Parameterized constructor

// Postcondition:

// Entry object has been constructed with firstName, // middleName, and lastName as arguments to Name // parameterized constructor; Time object has // initHours and initMinutes as arguments // to Time parameterized constructor void ReadEntry();

// Postcondition:

// Name and time have been prompted for and read private: Name name; AptTime time; }; #endif //***************************************************************** // IMPLEMENTATION FILE (Entry.cpp)

// This file contains the specification of the Entry ADT, // which has two contained classes, Name and Time

#include "EntryEx.h" #include <string> #include <fstream> #include <iostream> using namespace std;

string Entry::NameStr() const

// Returns a string made up of first name, blank, last name // Postcondition:

// Return value is first name of Name object, blank, and // last name of Name object

{

return (name.FirstName() + ' ' + name.LastName()); }

string Entry::TimeStr() const

// Returns a string made up of hour, colon, minutes // Post condition:

// Return value is minutes from Time object, colon, and // seconds of Time object

{

ifstream tempIn; ofstream tempOut;

tempOut.open("convert");

tempOut << time.Hours() << ":" << time.Minutes() << endl; tempOut.close(); tempIn.open("convert"); string temp; tempIn >> temp; tempIn.close(); return temp; } void Entry::ReadEntry() // Postcondition:

{

name.ReadName(); time.ReadTime(); }

RelationType Entry::ComparedTo(Entry entry) const // Compares time with entry.time

// Postcondition:

// BEFORE, if this time comes before entry.time // SAME, if this time and entry.time are the same // AFTER, if this time is after entry.time

{ return time.ComparedTo(entry.time); } Entry::Entry() // Default constructor // Postcondition:

// Entry object has been constructed {

}

Entry::Entry( /* in */ string firstName, /* in */ string middleName, /* in */ string lastName, /* in */ int initHours, /* in */ int initMinutes) // Constructor initializers

: name(firstName, middleName, lastName), time(initHours, initMinutes)

// Parameterized constructor // Postcondition:

// Entry object has been constructed with firstName, // middleName, and lastName as arguments to Name // parameterized constructor; initHours, initMinutes, // and initSeconds as arguments to Time parameterized // constructor

{ }

// SPECIFICATION FILE (SortedList2.h)

// This file gives the specification of a sorted list abstract data // type. The list items are maintained in ascending order of

// value

template<class ItemType> // Forward declaration template<class ItemType> class SortedList2 { public:

bool IsEmpty() const; // Postcondition:

// Return value is true if list is empty; // false otherwise

bool IsFull() const; // Postcondition:

// Return value is true if list is full; // false otherwise

void Insert( /* in */ ItemType item ); // Precondition:

// item is assigned // Postcondition: // item is in list

// && List components are in ascending order void Delete( /* in */ ItemType item );

// Precondition:

// item is somewhere in list // Postcondition:

// First occurrence of item is no longer in list // && List components are in ascending order

void Reset();

// Postcondition:

// Iteration is initialized ItemType GetNextItem();

// Precondition:

// Postcondition:

// Returns component at the current position // in the SortedList

int Length();

// Postcondition:

// Return value is length of list SortedList2();

// Constructor // Postcondition:

// Empty list is created SortedList2(int maxLength);

// Constructor // Postcondition:

// Empty list is created

SortedList2( const SortedList2& otherList ); // Copy-constructor

// Postcondition:

// List is created as a duplicate of otherList ~SortedList2();

// Destructor // Postcondition:

// List is destroyed bool IsPresent(ItemType item);

// Searches the list for item, reporting whether it was found // Postcondition:

// Return value is true, if item is the list; // false, otherwise private: NodeType<ItemType>* head; NodeType<ItemType>* currentPos; int length; int maxSize; };

#include "SortedList2.cpp"

//****************************************************************** // IMPLEMENTATION FILE (SortedList.cpp)

// This file implements the SortedList2 class member functions // List representation: a linked list of dynamic nodes

//****************************************************************** #include <iostream>

#include <cstddef> // For NULL #include "relType.h" using namespace std; template<class ItemType> struct NodeType { ItemType item; NodeType<ItemType>* link; };

// Private members of class:

// NodeType* head; External pointer to linked list //****************************************************************** template<class ItemType>

SortedList2<ItemType>::SortedList2() // Constructor

// Postcondition:

// head == NULL AND maxSize set to 1000 { head = NULL; length = 0; maxSize = 1000; } //****************************************************************** template<class ItemType> SortedList2<ItemType>::SortedList2(int maxLength)

// Constructor // Postcondition: // head == NULL { head = NULL; length = 0; maxSize = maxLength; } //****************************************************************** template<class ItemType>

SortedList2<ItemType>::SortedList2( const SortedList2<ItemType>& otherList )

// Copy-constructor // Postcondition:

// IF otherList.head == NULL (i.e., the other list is empty) // head == NULL

// ELSE

// head points to a new linked list that is a copy of // the linked list pointed to by otherList.head {

TNodeType<ItemType>* fromPtr; // Pointer into list being copied TNodeType<ItemType>* toPtr; // Pointer into list being built if (otherList.head == NULL)

{

head = NULL; return; }

// Copy first node

fromPtr = otherList.head; head = new TNodeType<ItemType>; head->item = fromPtr->item; // Copy remaining nodes toPtr = head;

while (fromPtr != NULL) {

toPtr->link = new TNodeType<TNodeType>; toPtr = toPtr->link; toPtr->item = fromPtr->item; fromPtr = fromPtr->link; } toPtr->link = NULL; length = otherList.length; currentPos = otherList.currentPos; } //****************************************************************** template<class ItemType> SortedList2<ItemType>::~SortedList2() // Destructor // Postcondition:

// All linked list nodes have been deallocated from free store {

TNodeType<ItemType>* temp; // Temporary variable while ( !IsEmpty() ) { temp = head; head = head->link; delete temp; } } //****************************************************************** template<class ItemType>

bool SortedList2<ItemType>::IsEmpty() const // Postcondition:

// Return value is true if head == NULL; false otherwise {

return (head == NULL); }

//**************************************************************** template<class ItemType>

bool SortedList2<ItemType>::IsFull() const // Postcondition:

// Return value is false {

return length == maxSize; }

//**************************************************************** template<class ItemType>

void SortedList2<ItemType>::Insert( /* in */ ItemType item ) // Precondition:

// item members of list nodes are in ascending order // && item is assigned

// Postcondition:

// New node containing item is in its proper place // in linked list

// && item members of list nodes are in ascending order {

TNodeType<ItemType>* currentPtr; // Moving pointer TNodeType<ItemType>* prevPtr; // Trailing pointer TNodeType<ItemType>* newNodePtr; // Pointer to new node // Set up node to be inserted

newNodePtr = new TNodeType<ItemType>; newNodePtr->item = item;

// Find previous insertion point prevPtr = NULL;

currentPtr = head;

while (currentPtr != NULL &&

currentPtr->item.ComparedTo(item) == BEFORE) {

prevPtr = currentPtr;

currentPtr = currentPtr->link; }

// Insert new node newNodePtr->link = currentPtr; if (prevPtr == NULL) head = newNodePtr; else prevPtr->link = newNodePtr; length++; } //****************************************************************** template<class ItemType>

void SortedList2<ItemType>::Delete( /* in */ ItemType item ) // Precondition:

// item members of list nodes are in ascending order // Postcondition:

// Node containing first occurrence of item is no longer in // linked list

// && item members of list nodes are in ascending order {

TNodeType<ItemType>* currentPtr; TNodeType<ItemType>* prevPtr; currentPtr = head;

prevPtr = NULL;

while (currentPtr != NULL

&& !(currentPtr->item.ComparedTo(item) == SAME)) { prevPtr = currentPtr; currentPtr = currentPtr->link; } if (currentPtr != NULL) { prevPtr->link = currentPtr->link; length--; } }

template<class ItemType> void SortedList2<ItemType>::Reset() // Postcondition: // Iteration is initialized { currentPos = NULL; } template<class ItemType> ItemType SortedList2<ItemType>::GetNextItem() // Precondition:

// No transformers have been invoked since last call // Postcondition:

// Returns item at the current position in the SortedList and // resets current to next position or first position if // last item is returned

{ ItemType item; if (currentPos == NULL) currentPos = head; item = currentPos->item; currentPos = currentPos->link; return item; } template<class ItemType> int SortedList2<ItemType>::Length() // Postcondition:

// Return value is length {

return length; }

template<class ItemType>

bool SortedList2<ItemType>::IsPresent(ItemType item)

// Searches the list for item, reporting whether it was found // Postcondition:

// Return value is true if item is the list; // false otherwise

{

TNodeType<ItemType>* currentPtr; // Moving pointer // Search for item

currentPtr = head;

while (currentPtr != NULL

&& currentPtr->item.ComparedTo(item) != SAME) currentPtr = currentPtr->link;

return !(currentPtr == NULL); }

In document Answers to Selected Exercises (Page 133-149)