CSC 313 Code, Fall 2007
Notre Dame University–Louaize · Data structures using C++ · 23 pages
Showing the first 10 of 23 pages. Download the PDF to see all of it.
More CSC 313 files
- Code · Fall 2007FileView
- Exam 1 with Solution · Spring 2008Exam · with solutionView
- Final · Spring 2008Final examView
- Final with Solution · Spring 2008Final exam · with solutionView
- Midterm · Spring 2008ExamView
- Homework 1 · Fall 2007HomeworkView
- Homework 2 · Fall 2007HomeworkView
- Exam 2 with Solution · Fall 2005Exam · with solutionView
Show the text of this file
CHAPTER 3
DRIVER.CPP
/*== Driver ==============================================================
Driver program to test Time library.
========================================================================*/
#include <iostream>
using namespace std;
#include "Time.h"
int main()
{
Time mealTime,
goToWorkTime;
set(mealTime, 5, 30, 'P');
cout << "We'll be eating at ";
display(mealTime, cout);
cout << endl;
set(goToWorkTime, 5, 30, 'P'); // Try other values also: 'A' -> 'P'
cout << "You leave for work at ";
display(goToWorkTime, cout);
cout << endl;
if (lessThan(mealTime, goToWorkTime))
cout << "If you hurry, you can eat first.\n";
else
cout << "Sorry you can't eat with us.\n";
advance(goToWorkTime, 0, 30); // Try other values also: 0 -> 12)
cout << "Your boss called. You go in later at ";
display(goToWorkTime, cout);
cout << endl;
if (lessThan(mealTime, goToWorkTime))
cout << "If you hurry, you can eat first.\n";
else
cout << "Sorry you can't eat with us.\n";
cout << endl;
}
FIGURE 3-10.CPP
//-- Demonstration #3 of new failure -- Use nothrow version of new
//-- and check whether it returns a null address.
#include <iostream>
#include <new>, nothrow
using namespace std;
int main()
{
const int NUM_ARRAYS = 10;
cout << "How large should the arrays of doubles be? ";
int capacity;
cin >> capacity;
double * arrayPtr[NUM_ARRAYS];
int i;
for (i = 0; i < NUM_ARRAYS; i++)
{
arrayPtr[i] = new(nothrow) double [capacity];
if (arrayPtr[i] == 0) // null address
{
cout << "Allocation failed for i = " << i << endl;
exit(1);
}
cout << "Allocated " << capacity
<< " doubles for i = " << i << endl;
}
cout << "All " << NUM_ARRAYS << " arrays of "
<< capacity << " doubles were allocated successfully." << endl;
}
FIGURE 3-5.CPP
/*--------------------------------------------------------------------------
Demonstration of what can happen when array indices get out of bounds.
Input: Three arrays of integers
Output: The three arrays before and after modification using
out-of-range indices.
-------------------------------------------------------------------------*/
#include <iostream>
using namespace std;
const int CAPACITY = 4;
typedef int IntArray[CAPACITY];
void read(IntArray theArray, int capacity, int numValues);
void display(IntArray theArray, int numValues);
int main()
{
IntArray a, b, c;
cout << "Enter " << CAPACITY << " integer values for:\n";
cout << "Array a: ";
read(a, CAPACITY, CAPACITY);
cout << "Array b: ";
read(b, CAPACITY, CAPACITY);
cout << "Array c: ";
read(c, CAPACITY, CAPACITY);
cout << "\n------ Part I of the demonstration -----\n\n"
"The arrays are:\n";
cout << "a: ";
display(a, CAPACITY);
cout << "b: ";
display(b, CAPACITY);
cout << "c: ";
display(c, CAPACITY);
//--- Now change array elements in b, but using
//--- some out-of-range indices.
int below = -3,
above = 6;
b[below] = -999;
b[above] = 999;
cout << "\n------ Part II of the demonstration -----\n\n"
"The arrays after out-of-range errors are:\n";
cout << "a: ";
display(a, CAPACITY);
cout << "b: ";
display(b, CAPACITY);
cout << "c: ";
display(c, CAPACITY);
cout << endl;
}
//--- Insert here the definition of read() from Figure 3.4
//--- and the definition of display() from Figure 3.3
#include <cassert>
void read(IntArray theArray, int capacity, int numValues)
/*-------------------------------------------------------------------------
Input values into an array of integers from the keyboard.
Preconditions: 0 <= numValues <= capacity, which is the capacity of
theArray.
Postcondition: numValues integers entered from the keyboard have been
stored in the first numValues positions of theArray.
-------------------------------------------------------------------------*/
{
assert (numValues >= 0 && numValues <= capacity);
for (int i = 0; i < numValues; i++)
cin >> theArray[i];
}
void display(int theArray[], int numValues)
/*-------------------------------------------------------------------------
Display values in an array of integers.
Precondition: 0 <= numValues < capacity of theArray.
Postcondition: The first numValues integers stored in theArray have
been output to cout.
-------------------------------------------------------------------------*/
{
for (int i = 0; i < numValues; i++)
cout << theArray[i] << " ";
cout << endl;
}
FIGURE 3-8.CPP
//-- Demonstration #1 of new failure -- Uncaught bad_alloc exception
#include <iostream>
#include <new> // new, bad_alloc
using namespace std;
int main()
{
const int NUM_ARRAYS = 10;
cout << "How large should the arrays of doubles be? ";
int capacity;
cin >> capacity;
double * arrayPtr[NUM_ARRAYS];
int i;
for (i = 0; i < NUM_ARRAYS; i++)
{
arrayPtr[i] = new double [capacity];
cout << "Allocated " << capacity
<< " doubles for i = " << i << endl;
}
cout << "All " << NUM_ARRAYS << " arrays of "
<< capacity << " doubles were allocated successfully." << endl;
}
FIGURE 3-9.CPP
//-- Demonstration #2 of new failure -- Use try-catch mechanixm
//-- to handle bad_alloc exception.
#include <iostream>
#include <new>
using namespace std;
int main()
{
const int NUM_ARRAYS = 10;
cout << "How large should the arrays of doubles be? ";
int capacity;
cin >> capacity;
double * arrayPtr[NUM_ARRAYS];
int i;
try
{
for (i = 0; i < NUM_ARRAYS; i++)
{
arrayPtr[i] = new double [capacity];
cout << "Allocated " << capacity
<< " doubles for i = " << i << endl;
}
}
catch (bad_alloc ex)
{
cout << "\nException: " << ex.what()
<< " -- for i = " << i << endl;
exit(1);
}
cout << "All " << NUM_ARRAYS << " arrays of "
<< capacity << " doubles were allocated successfully." << endl;
}
TIME.CPP
/*== Time.cpp =============================================================
Definitions of the functions that implement Time operations whose
prototypes are in Time.h.
========================================================================*/
#include <iostream>
using namespace std;
#include "Time.h"
/** Utility functions **/
int toMilitary(unsigned hours, unsigned minutes, char AMPM);
void toStandard(unsigned military,
unsigned & hours, unsigned & minutes, char & AMPM);
//--- Definition of set() -------------------------------------------------
void set(Time & t, unsigned hours, unsigned minutes, char AMPM)
{
if (hours >= 1 && hours <= 12 &&
minutes >= 0 && minutes <= 59 &&
(AMPM == 'A' || AMPM == 'P'))
{
t.hour = hours;
t.minute = minutes;
t.AMorPM = AMPM;
t.milTime = toMilitary(hours, minutes, AMPM);
}
else
cerr << "*** Can't set time with these values ***\n";
// t remains unchanged
}
//--- Definition of display() -----------------------------------------
void display(const Time & t, ostream & out)
{
out << t.hour << ':'
<< (t.minute < 10 ? "0" : "") << t.minute
<< ' ' << t.AMorPM << ".M. ("
<< t.milTime << " mil. time)";
}
//--- Definition of advance() ---------------------------------------------
void advance(Time & t, unsigned hours, unsigned minutes)
{
// Advance using military time
t.milTime += 100 * hours + minutes;
unsigned milHours = t.milTime / 100,
milMins = t.milTime % 100;
// Adjust to proper format
milHours += milMins / 60;
milMins %= 60;
milHours %= 24;
t.milTime = 100 * milHours + milMins;
// Now set standard time
toStandard(t.milTime, t.hour, t.minute, t.AMorPM);
}
//--- Definition of lessThan() --------------------------------------------
bool lessThan(const Time & t1, const Time & t2)
{
return (t1.milTime < t2.milTime);
}
//----- DEFINITIONS OF UTILITY FUNCTIONS -------
int toMilitary (unsigned hours, unsigned minutes, char AMPM)
/*-------------------------------------------------------------------------
Convert standard time to military time.
Receive: hours, minutes, AMPM
Return: The military time equivalent
-------------------------------------------------------------------------*/
{
if (hours == 12)
hours = 0;
return hours * 100 + minutes + (AMPM == 'P' ? 1200 : 0);
}
void toStandard(unsigned military,
unsigned & hours, unsigned & minutes, char & AMPM)
/*-------------------------------------------------------------------------
Convert military time to standard time.
Receive: military, a time in military format
Pass back: hours, minutes, AMPM -- equivalent standard time
-------------------------------------------------------------------------*/
{
hours = (military / 100) % 12;
if (hours == 0)
hours = 12;
minutes = military % 100;
AMPM = (military / 100) < 12 ? 'A' : 'P';
}
TIME.H
/*== Time.h ===============================================================
This header file defines the data type Time for processing time.
Basic operations are:
set: To set the time
display: To display the time
advance: To advance the time by a certain amount
lessThan: To determine if one time is less than another
=========================================================================*/
#include <iostream>
struct Time
{
unsigned hour,
minute;
char AMorPM; // 'A' or 'P'
unsigned milTime; // military time equivalent
};
void set(Time & t, unsigned hours, unsigned minutes, char AMPM);
/*-------------------------------------------------------------------------
Set the time to a specified value.
Receive: Time object t
hours, the number of hours in standard time
minutes, the number of minutes in standard time
AMPM ('A' if AM, 'P' if PM)
Pass back: The modified Time t with data members set to the
specified values
-------------------------------------------------------------------------*/
void display(const Time & t, ostream & out);
/*-------------------------------------------------------------------------
Display time t in standard and military format using output stream out.
Receive: Time t and ostream out
Output: The time t to out
Pass back: The modified ostream out with a representation of t
inserted into it
------------------------------------------------------------------------*/
void advance(Time & t, unsigned hours, unsigned minutes);
/*-------------------------------------------------------------------------
Increment a time by a specified value.
Receive: Time object t
hours, the number of hours to add
minutes, the number of minutes to add
Pass back: The modified Time t with data members incremented by the
specified values
-------------------------------------------------------------------------*/
bool lessThan(const Time & t1, const Time & t2);
/*-------------------------------------------------------------------------
/* Determines if one time is less than another time.
*
* Receive: Times t1 and t2
* Return: True if t1 < t2, false otherwise.
-------------------------------------------------------------------------*/
CHAPTER 4
FIGURE 4.2-4
DRIVER.CPP
//--- Test driver for class Time
#include <iostream>
using namespace std;
#include "Time.h"
int main()
{
Time mealTime;
mealTime.set(5, 30, 'P');
cout << "We'll be eating at ";
mealTime.display(cout);
cout << endl;
cout << "\nNow trying to set time with illegal hours (13)" << endl;
mealTime.set(13, 0, 'A');
cout << "Now trying to set time with illegal minutes (60)" << endl;
mealTime.set(5, 60, 'A');
cout << "Now trying to set time with illegal AM/PM ('X')" << endl;
mealTime.set(5, 30, 'X');
}
TIME.CPP
/*-- Time.cpp------------------------------------------------------------
Definitions of the function members of the Time class declared
in Time.h and definitions of utility functions that convert
between military and standard time.
-------------------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include "Time.h"
/*** Utility Functions -- Prototypes ***/
int toMilitary(unsigned hours, unsigned minutes, char am_pm);
//----- Definition of set function -----
void Time::set(unsigned hours, unsigned minutes, char am_pm)
{
// Check class invariant
if (hours >= 1 && hours <= 12 &&
minutes >= 0 && minutes <= 59 &&
(am_pm == 'A' || am_pm == 'P'))
{
myHours = hours;
myMinutes = minutes;
myAMorPM = am_pm;
myMilTime = toMilitary(hours, minutes, am_pm);
}
else
cerr << "*** Can't set time with these values ***\n";
// Object's data members remain unchanged
}
//----- Definition of display function -----
void Time::display(ostream & out) const
{
out << myHours << ':'
<< (myMinutes < 10 ? "0" : "") << myMinutes
<< ' ' << myAMorPM << ".M. ("
<< myMilTime << " mil. time)";
}
/*** Utility Functions -- Definitions ***/
int toMilitary(unsigned hours, unsigned minutes, char am_pm)
/*-------------------------------------------------------------------------
Convert standard time to military time.
Precondition: hours, minutes, am_pm satisfy the class invariant.
Postcondition: Military time equivalent is returned.
-------------------------------------------------------------------------*/
{
if (hours == 12)
hours = 0;
return hours * 100 + minutes + (am_pm == 'P' ? 1200 : 0);
}
TIME.H
/*-- Time.h ---------------------------------------------------------------
This header file defines the data type Time for processing time.
Basic operations are:
set: To set the time
display: To display the time
-------------------------------------------------------------------------*/
#include <iostream>
class Time
{
public:
/******** Function Members ********/
void set(unsigned hours, unsigned minutes, char am_pm);
/*----------------------------------------------------------------------
Set the data members of a Time object to specified values.
Preconditions: 1 <= hours <= 12, 0 <= minutes <= 59,
and am_pm is either 'A' or 'P'.
Postcondition: Data members myHours, myMinutes, and myAMorPM
are set to hours, minutes, and am_pm, respectively, and
myMilTime to the equivalent military time
----------------------------------------------------------------------*/
void display(ostream & out) const;
/*----------------------------------------------------------------------
Display time in standard and military format using output stream out.
Precondition: The ostream out is open.
Postcondition: The time represented by this Time object has been
inserted into ostream out.
----------------------------------------------------------------------*/
private:
/********** Data Members **********/
unsigned myHours,
myMinutes;
char myAMorPM; // 'A' or 'P'
unsigned myMilTime; // military time equivalent
}; // end of class declaration
FIGURE 4.5-10
DRIVER.CPP
// Test driver for Time class
#include "Time.h"
#include <iostream>
using namespace std;
// Now check including file Time.h again
#include "Time.h"
int main()
{
Time mealTime,
bedTime;
for (int i = 1; i <= 3; i++)
{
Time napTime(2, 0, 'P');
cout << "\nNaptime is " << napTime << endl;
cout << "Enter meal time and bedtime (hh:mm xM): ";
cin >> mealTime >> bedTime;
cout << "We'll be eating at " << mealTime << endl;
cout << "We'll hit the hay at " << bedTime << endl;
cout << "\nmealTime < napTime? "
<< boolalpha << (mealTime < napTime) << endl;
cout << "mealTime > napTime? "
<< (mealTime > napTime) << endl;
cout << "mealTime == napTime? "
<< (mealTime == napTime) << endl;
cout << "mealTime <= napTime? "
<< (mealTime <= napTime) << endl;
cout << "mealTime >= napTime? "
<< (mealTime >= napTime) << endl;
cout << "mealTime !=napTime? "
<< (mealTime != napTime) << endl;
napTime.set(mealTime.getHours(), mealTime.getMinutes(),
mealTime.getAMPM());
napTime.advance(1, 15);
cout << "\nWe'll be eating at " << mealTime
<< "\nWe'll be napping 1 1/4 hours later at " << napTime << endl;
}
}
TIME.CPP
/*-- Time.cpp------------------------------------------------------------
This file implements the Time member functions.
-------------------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include "Time.h"
/*** Utility Functions -- Prototypes ***/
int toMilitary(unsigned hours, unsigned minutes, char am_pm);
void toStandard(unsigned military,
unsigned & hours, unsigned & minutes, char & AMPM);
//----- Definition of default constructor
Time::Time()
: myHours(12), myMinutes(0), myAMorPM('A'), myMilTime(0)
{
}
//----- Definition of explicit-value constructor -----
Time::Time(unsigned initHours, unsigned initMinutes, char initAMPM)
{
// Check class invariant
if (initHours >= 1 && initHours <= 12 &&
initMinutes >= 0 && initMinutes <= 59 &&
(initAMPM == 'A' || initAMPM == 'P'))
{
myHours = initHours;
myMinutes = initMinutes;
myAMorPM = initAMPM;
myMilTime = toMilitary(initHours, initMinutes, initAMPM);
}
else
cerr << "*** Invalid initial values ***\n";
}
//----- Definition of getMinutes()
unsigned Time::getMinutes() const
{
return myMinutes;
}
//----- Definition of getHours()
unsigned Time::getHours() const
{
return myHours;
}
//----- Definition of getAMPM()
unsigned Time::getAMPM() const
{
return myAMorPM;
}
//----- Definition of getMilTime()
unsigned Time::getMilTime() const
{
return myMilTime;
}
//----- Definition of display function -----
void Time::display(ostream & out) const
{
out << myHours << ':'
<< (myMinutes < 10 ? "0" : "") << myMinutes
<< ' ' << myAMorPM << ".M. ("
<< myMilTime << " mil. time)";
}
//----- Definition of read function -----
void Time::read(istream & in)
{
unsigned hours, // Local variables to hold input values from in so
minutes; // they can be checked against the class invariant
char am_pm, // before putting them in the data members
ch; // To gobble up : and the 'M' in input
in >> hours >> ch >> minutes >> am_pm >> ch;
// Check the class invariant
if (hours >= 1 && hours <= 12 &&
minutes >= 0 && minutes <= 59 &&
(am_pm == 'A' || am_pm == 'P'))
{
myHours = hours;
myMinutes = minutes;
myAMorPM = am_pm;
myMilTime = toMilitary(hours, minutes, am_pm);
}
else
cerr << "*** Invalid input for Time object ***\n";
}
//----- Definition of operator<<()
ostream & operator<<(ostream & out, const Time & t)
{
t.display(out);
return out;
}
//----- Definition of operator>>()
istream & operator>>(istream & in, Time & t)
{
t.read(in);
return in;
}
//----- Definition of set function -----
void Time::set(unsigned hours, unsigned minutes, char am_pm)
{
// Check class invariant
if (hours >= 1 && hours <= 12 &&
minutes >= 0 && minutes <= 59 &&
(am_pm == 'A' || am_pm == 'P'))
{
myHours = hours;
myMinutes = minutes;
myAMorPM = am_pm;
myMilTime = toMilitary(hours, minutes, am_pm);
}
else
cerr << "*** Can't set time with these values ***\n";
// Object's data members remain unchanged
}
//----- Definition of advance function-----
void Time::advance(unsigned hours, unsigned minutes)
{
// Increment the myMilTime member
myMilTime += 100 * hours + minutes;
// Adjust to proper format
unsigned milHours = myMilTime / 100,
milMins = myMilTime % 100;
milHours += milMins / 60;
milMins %= 60;
milHours %= 24;
myMilTime = 100 * milHours + milMins;
// Now set the standard time data members
toStandard(myMilTime, myHours, myMinutes, myAMorPM);
}
/***** Relational operators *****/
bool operator<(const Time & t1, const Time & t2)
{
return t1.getMilTime() < t2.getMilTime();
}
bool operator>(const Time & t1, const Time & t2)
{
return t1.getMilTime() > t2.getMilTime();
}
bool operator==(const Time & t1, const Time & t2)
{
return t1.getMilTime() == t2.getMilTime();
}
bool operator<=(const Time & t1, const Time & t2)
{
return t1.getMilTime() <= t2.getMilTime();
}
bool operator>=(const Time & t1, const Time & t2)
{
return t1.getMilTime() >= t2.getMilTime();
}
//----- Definition of operator!=() -----
bool operator!=(const Time & t1, const Time & t2)
{
return t1.getMilTime() != t2.getMilTime();
}
/*** Utility Functions -- Definitions ***/
int toMilitary(unsigned hours, unsigned minutes, char am_pm)
/*-------------------------------------------------------------------------
Convert standard time to military time.
Precondition: hours, minutes, am_pm satisfy the class invariant.
Postcondition: Military time equivalent is returned.
-------------------------------------------------------------------------*/
{
if (hours == 12)
hours = 0;
return hours * 100 + minutes + (am_pm == 'P' ? 1200 : 0);
}
void toStandard(unsigned military,
unsigned & hours, unsigned & minutes, char & AMPM)
/*-------------------------------------------------------------------------
Convert military time to standard time.
Receive: military, a time in military format
Return: hours, minutes, AMPM -- equivalent standard time
-------------------------------------------------------------------------*/
{
hours = (military / 100) % 12;
if (hours == 0)
hours = 12;
minutes = military % 100;
AMPM = (military / 100) < 12 ? 'A' : 'P';
}
TIME.H
/*-- Time.h ---------------------------------------------------------------
This header file defines the data type Time for processing time.
Basic operations are:
Default constructor -- Initializes object to 12:00 AM
Explicit-value constructor
read: To read a time
display: To display the time
>>, <<: Input and Output operators
accessors: getHours, getMinutes, getAMPM, getMilTime
set: To set the time
advance: To advance the time by a certain amount
relops: <, >, ==, <=, >=, !=
-------------------------------------------------------------------------*/
#include <iostream>
using namespace std;
#ifndef TIME
#define TIME
class Time
{
public:
/******** Function Members ********/
/***** Class constructors *****/
Time();
/*----------------------------------------------------------------------
Construct a class object (default).
Precondition: None.
Postcondition: Time object is initialized to 12:00 A.M.;
that is, myHours, myMinutes, and myAMorPM are initialized
to 12, 0, 'A', respectively, and myMilTime to 0.
-----------------------------------------------------------------------*/
Time(unsigned initHours, unsigned initMinutes, char initAMPM);
/*----------------------------------------------------------------------
Construct a class object (explicit values).
Precondition: initHours, initMinutes, and initAMPM are initial
values for the data members; they must preserve the class
invariant.
Postcondition: Data members myHours, myMinutes, and myAMorPM
have been initialized to initHours, initMinutes, and initAMPM,
respectively, and myMilTime to the corresponding military time.
-----------------------------------------------------------------------*/
/***** Accessors *****/
unsigned getHours() const;
/*----------------------------------------------------------------------
Retrieve the value stored in the myHours data member.
Precondition: None.
Postcondition: Value stored in myHours is returned.
-----------------------------------------------------------------------*/
unsigned getMinutes() const;
/*----------------------------------------------------------------------
Retrieve the value stored in the myMinutes data member.
Precondition: None.
Postcondition: Value stored in myMinutes is returned.
-----------------------------------------------------------------------*/
unsigned getAMPM() const;
/*----------------------------------------------------------------------
Retrieve the value stored in the myAMorPM data member.
Precondition: None.
Postcondition: Value stored in myAMorPM is returned.
-----------------------------------------------------------------------*/
unsigned getMilTime() const;
/*----------------------------------------------------------------------
Retrieve the value stored in the myMilTime data member.
Precondition: None.
Postcondition: Value stored in myMilTime is returned.
-----------------------------------------------------------------------*/
/***** Input/Output *****/
void display(ostream & out) const;
/*----------------------------------------------------------------------
Display time in standard and military format using output stream out.
Precondition: The ostream out is open.
Postcondition: The time represented by this Time object has been
inserted into ostream out.
----------------------------------------------------------------------*/
void read(istream & in);
/*----------------------------------------------------------------------
Read a time value from input stream in.
Precondition: The istream in is open; input from in has the form
hh:mm xM; values hh, mm, and X satisfy the class invariant.
Postcondition: Input values have been removed from in and stored
in the data members.
----------------------------------------------------------------------*/
/***** Set operation *****/
void set(unsigned hours, unsigned minutes, char am_pm);
/*----------------------------------------------------------------------
Set the data members of a Time object to specified values.
Preconditions: 1 <= hours <= 12, 0 <= minutes <= 59,
and am_pm is either 'A' or 'P'.
Postconditon: Data members myHours, myMinutes, and myAMorPM
are set to hours, minutes, and am_pm, respectively, and
myMilTime to the equivalent military time
----------------------------------------------------------------------*/
/***** Increment operation *****/
void advance(unsigned hours, unsigned minutes);
/*----------------------------------------------------------------------
Increment a Time object by a specified value.
Precondition: hours is the number of hours to add and minutes
is the number of minutes to add.
Postcondition: The time represented by this Time object had been
incremented by this number of hours and minutes.
----------------------------------------------------------------------*/
private:
/********** Data Members **********/
unsigned myHours,
myMinutes;
char myAMorPM; // 'A' or 'P'
unsigned myMilTime; // military time equivalent
}; // end of class declaration
//----- << and >> operators
ostream & operator<<(ostream & out, const Time & t);
/*------------------------------------------------------------------------
Overloaded ouput operator
Precondition: The ostream out is open.
Postcondition: The time represented by this Time object has been
inserted into ostream out (via display()); reference to out
is returned.
------------------------------------------------------------------------*/
istream & operator>>(istream & in, Time & t);
/*------------------------------------------------------------------------
Overloaded input operator
Precondition: The istream in is open; input from in has the form
hh:mm xM; values hh, mm, and X satisfy the class invariant.
Postcondition: Values have been extracted from in (via read()) and
stored in this Time object's data members; reference to in is
returned.
--------------------------------------------------------------------------*/
/***** Relational operators *****/
bool operator<(const Time & t1, const Time & t2);
/*----------------------------------------------------------------------
Determine if one Time object is less than (i.e., earlier than)
another Time object.
Precondition: None.
Postcondition: true is returned if t1 is less than t2 and
false otherwise.
---------------------------------------------------------------------*/
bool operator>(const Time & t1, const Time & t2);
/*----------------------------------------------------------------------
Determine if one Time object is greater than (i.e., later than)
another Time object.
Precondition: None.
Postcondition: true is returned if t1 is greater than t2 and
false otherwise.
----------------------------------------------------------------------*/
bool operator==(const Time & t1, const Time & t2);
/*----------------------------------------------------------------------
Determine if one Time object is equal to another Time object.
Precondition: None.
Postcondition: true is returned if t1 is equal to t2 and
false otherwise.
----------------------------------------------------------------------*/
bool operator<=(const Time & t1, const Time & t2);
/*----------------------------------------------------------------------
Determine if one Time object is less than or equal to
(i.e., earlier than or the same as) another Time object.
Precondition: None.
Postcondition: true is returned if t1 is less than or equal
to t2 and false otherwise.
----------------------------------------------------------------------*/
bool operator>=(const Time & t1, const Time & t2);
/*----------------------------------------------------------------------
Determine if one Time object is greater than or equal to
(i.e., later than or the same as) another Time object.
Precondition: None.
Postcondition: true is returned if t1 is greater than or equal
to t2 and false otherwise.
----------------------------------------------------------------------*/
//----- Definition of operator!=() -----
bool operator!=(const Time & t1, const Time & t2);
/*----------------------------------------------------------------------
Determine if one Time object is not equal to another Time object.
Precondition: None.
Postcondition: true is returned if t1 is not equal to t2 and
false otherwise.
----------------------------------------------------------------------*/
#endif
CHAPTER 5
FIGURE 5.1
DATA 5-1 TXT
88 66 99 100
77 66 44 55
95 85
FIGURE 5-1.CPP
/*--------------------------------------------------------------------------
Read numeric data stored in a file, compute the minimum, maximum, and
average of the numbers, and write these statistics to an output file.
Input(keyboard): names of the input and output files
Input(file): a sequence of numeric values
Output(file): a count of the values, the minimum value,
the maximum value, and the average value
--------------------------------------------------------------------------*/
#include <iostream> // cin, cout
#include <fstream> // ifstream, ofstream
#include <string> // string, getline()
#include <cassert> // assert()
#include <cfloat> // DBL_MIN and DBL_MAX
using namespace std;
int main()
{
cout << "This program computes the number, maximum, minimum, and\n"
"average of an input list of numbers in one file,\n"
"and places its results in another file.\n\n";
// ----------- Input Section -----------------------------------
cout << "Enter the name of the input file: ";
string inputFileName;
getline(cin, inputFileName); // get name of input file
// open an input stream
ifstream fin; // to the input file,
fin.open(inputFileName.data()); // establish a connection,
assert( fin.is_open() ); // and check for success
int count = 0; // number of values
double reading, // value being processed
maximum = DBL_MIN, // largest seen so far
minimum = DBL_MAX, // smallest seen so far
sum = 0.0; // running total
for (;;) // loop:
{
fin >> reading; // r