这是三个文件的内容
Dtime.h:#ifndef DTIME_H
#define DTIME_H
#include <iostream>
using std::istream;
using std::ostream;
namespace DTimeSavitch
{
class DigitalTime
{
public:
DigitalTime(int theHour, int theMinute);
DigitalTime();
int getHour() const;
int getMinute() const;
void advance(int minutesAdded);
void advance(int houtsAdded, int minutesAdded);
friend bool operator==(const DigitalTime &time1, const DigitalTime &time2);
friend istream &operator>>(istream &ins, DigitalTime &theObject);
friend ostream &operator<<(ostream &outs, const DigitalTime &theObject);
private:
int hour;
int minute;
};
}
#endif
Dtime.cpp:#include <iostream>
#include <cctype>
#include <cstdlib>
using std::cin;
using std::cout;
using std::istream;
using std::ostream;
#include "Dtime.h"
namespace
{
int digitToInt(char c)
{
return (int(c) - int('0'));
}
void readMinute(int &theMinute)
{
char c1, c2;
cin >> c1 >> c2;
if (!(isdigit(c1) && isdigit(c2)))
{
cout << "Error: illegal input to reaadMinute\n";
exit(1);
}
theMinute = digitToInt(c1) * 10 + digitToInt(c2);
if (theMinute < 0 || theMinute > 59)
{
cout << "Error: illegal input to readMinute\n";
exit(1);
}
}
void readHour(int &theHour)
{
char c1, c2;
cin >> c1 >> c2;
if (!(isdigit(c1) && (isdigit(c2) || c2 == ':')))
{
cout << "Error: illegal input to readHour\n";
exit(1);
}
if (isdigit(c1) && c2 == ':')
{
theHour = digitToInt(c1);
}
else
{
theHour = digitToInt(c1) * 10 + digitToInt(c2);
cin >> c2;
if (c2 != ':')
{
cout << "Error: illegal input to readHour\n";
exit(1);
}
}
if (theHour == 24)
theHour = 0;
if (theHour < 0 || theHour > 23)
{
cout << "Error: illegal input to readHour\n";
exit(1);
}
}
}
namespace DTimeSavitch
{
istream &operator>>(istream &ins, DigitalTime &theObject)
{
readHour(theObject.hour);
readMinute(theObject.minute);
return ins;
}
ostream &operator<<(ostream &outs, const DigitalTime &theObject)
{
outs << theObject.hour << ':';
if (theObject.minute < 10)
outs << '0';
outs << theObject.minute;
return outs;
}
bool operator==(const DigitalTime &time1, const DigitalTime &time2)
{
return (time1.hour == time2.hour && time1.minute == time2.minute);
}
DigitalTime::DigitalTime(int theHour, int theMinute)
{
if (theHour < 0 || theHour > 24 || theMinute < 0 || theMinute > 59)
{
cout << "Illegal argument to DigitalTime constructor.";
exit(1);
}
else
{
hour = theHour;
minute = theMinute;
}
if (hour == 24)
hour = 0;
}
DigitalTime::DigitalTime()
{
hour = 0;
minute = 0;
}
int DigitalTime::getHour() const
{
return hour;
}
int DigitalTime::getMinute() const
{
return minute;
}
void DigitalTime::advance(int minutesAdded)
{
int grossMinutes = minute + minutesAdded;
minute = grossMinutes % 60;
int hourAdjustment = grossMinutes / 60;
hour = (hour + hourAdjustment) % 24;
}
void DigitalTime::advance(int hoursAdded, int minutesAdded)
{
hour = (hour + hoursAdded) % 24;
advance(minutesAdded);
}
}
main.cpp:#include <iostream>
#include "Dtime.h"
void readHour(int &theHour);
int main()
{
using std::cin;
using std::cout;
using std::endl;
using DTimeSavitch::DigitalTime;
int theHour;
readHour(theHour);
DigitalTime clock(theHour, 0), oldClock;
oldClock = clock;
clock.advance(15);
if (clock == oldClock)
cout << "Sometheing is wrong.";
cout << "You entered " << oldClock << endl;
cout << "15 minutes later the time will be " << clock << endl;
clock.advance(2, 15);
cout << "2 hours and 15 minutes after that\n"
<< "the time will be " << clock << endl;
return 0;
}
void readHour(int &theHour)
{
using std::cin;
using std::cout;
cout << "Let's play a time game.\n"
<< "Let's pretend the hour has just changed.\n"
<< "You may write midnight as either 0 or 24,\n"
<< "but, I will always write it as 0.\n"
<< "Enter the hour as a number(0 to 24)";
cin >> theHour;
}
|