Code:
#include <iostream>
#include <iomanip> // for setfill and setw
#include <cstring> // for strings
using namespace std;
class Date // typically stored in header file date1.h
{
public:
Date( const char* , int , int ); // constructor for January 15, YYYY
void printJulian() const; // print date in DDD YYYY format
void printMDY() const; // print date in MM/DD/YY
void printLong() const; // print date something like June 14, 2006
~Date(); // provided to confirm destruction order
private:
int month;
int day;
int year;
int julian;
int checkDay( int ) const; // function tests proper day
int getMonthNumber( const char* ) const; // to compare January to 1
}; // end class Date; end of date1.h header file
const char* monthTable[13] = {"invalid","January","February","March","April","May","June","July","August","September","October","November","December"
}; // global array
Date::Date(const char* _month, int _day, int _year) // untested
{
year = _year; // should validate yr
month = getMonthNumber(_month);
day = checkDay( _day ); // validate the day
}
void Date::printJulian() const // prints as DDD YYYY (ordinal/gregorian)
{
int calculator = 0;
if (month == 1)
calculator = 0;
else if (month == 2)
calculator = 31;
else if (month == 3)
calculator = 59;
else if (month == 4)
calculator = 90;
else if (month == 5)
calculator = 120;
else if (month == 6)
calculator = 151;
else if (month == 7)
calculator = 181;
else if (month == 8)
calculator = 212;
else if (month == 9)
calculator = 243;
else if (month == 10)
calculator = 273;
else if (month == 11)
calculator = 304;
else if (month == 12)
calculator = 334;
int gregorian;
gregorian = (day + calculator);
cout << setfill('0') << setw(3) << gregorian << "/" << year;
} // end function printJulian
void Date::printMDY() const // prints as MM/DD/YY
{
cout << setfill('0') << setw(2) << month << '/' << setw(2) << day << '/' << setw(2) << year;
} // end function print3
void Date::printLong() const // prints, for example, March 12, 1988 - written format
{
const char* monthArray[13] = {"null","January","February","March","April","May","June","July",
"August","September","October","November","December"};
cout << monthArray[month] << " " << day << ", " << year;
} // end function print4
// output Date object to show when its destructor is called
Date::~Date()
{
} // end ~Date destructor
// utility function to confirm proper day value based on
// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
static const int daysPerMonth[ 13 ] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
// February 29 check for leap year
if ( month == 2 && testDay == 29 &&
( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
} // end function checkDay; end of date1.cpp
int Date::getMonthNumber(const char* _month) const
{
int key = 0;
for (int i = 1; i< 13;i++){
if (!strcmp(_month,monthTable[i]))
{
key = i;
break;
}
}
return key;
}
int main()
{
Date g( January, 12, 1994 ); // fourth date, written
cout << "\nFourth date entered is: ";
g.printLong();
cout << "\nFourth date in DDD/YYYY (Gregorian) format: ";
g.printJulian();
cout << "\nFourth date in MM/DD/YY format: ";
g.printMDY();
cout << "\nFourth date in written format: ";
g.printLong();
cout << endl;
return 0;
} // end main