2012年2月20日 星期一

Exercise #4


Exercise #4

Create a date class consisting of:
  • 3 private unsigned int data members:  month, day, and year.
  • 5 constructors, as described below
  • a destructor, as described below
  • a print() function
  • an increment() function

Use the following global variables:

const char* const Months[12] = {"January","February","March","April","May", "June","July","August","September","October","November","December"};
const unsigned CurrentYear = 2009;
// DaysPerMonth - non-const so that changes can be made for leap year
unsigned DaysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

Constructors:
  • The default constructor should look something like this:
  • // The default constructor returns the current system date
    date::date() {
    time_t timer = time(0);
    tm* NOW = localtime(&timer);
    month = NOW->tm_mon+1;  // NOW->tm_mon is current month#-1
    day = NOW->tm_mday;
    year = NOW->tm_year + 1900;
    }
  • The time_t and tm types and the time() and localtime() functions are defined in the ANSI C header file, ctime. Review the documentation for these types and functions.
  • The second constructor should have 3 unsigned arguments, the third argument is default = CurrentYear. The 3 arguments should be assigned to the month, day, and year members respectively. Observe the assumption listed below concerning 2-digit years. This constructor should be able to take a 2-digit or a 4-digit year.
  • The third constructor should take a char* argument of the form "mmddyy" or "mm/dd/yy". The argument should be parsed and respective values assigned to the month, day, and year members. Observe the assumption listed below concerning 2-digit years.
  • The fourth constructor should take a single unsigned int argument, the day of the year for the current year. For example, date(25) is January 25, 2009 and date(364) is December 30, 2009.
  • The fifth constructor is the copy constructor.

The destructor should call the print() function to display the date that is being destructed.

The print() function, with a default int argument, should display dates with a format mm/dd/yy or Month d, yyyy or ddMONyy, depending on the value of the argument. Note the uppercase in the 3rd format.  Note: mm/dd/yy is the default format.

The increment() function should add 1 day to any date.  This function must work for leap years.  The rules for leap years are:
  • A leap year occurs in every year that can be divided evenly by four, except the years that mark the even hundreds, such as 1500.
  • The only century years that are leap years are those that can be divided evenly by 400, such as 1600 and 2000.

Assumptions:
  • For the constructors, any 2-digit reference to a year is to be interpreted as follows:  1) assume any two-digit year reference < 50 refers to years between 2000 and 2049.  2) assume any two-digit year reference >= 50 refers to years between 1950 and 1999.  For example, 02/04/94 means February 4, 1994, and 02/04/49 means February 4, 2049.
  • Assume date values for month, day, year, and day of the year are correct. You do not have to perform error checking on these values. 

Use this main() to test your program and redirect your output to a file.  Turn in the output file with your program listing.

int main()
{
  int i;
  date D1;
  date D2(9,19,00);
  date D3(7,15,49);
  date D4(1,10);
  date D5("010148");
  date D6("10/01/59");
  date D7(2,26,1936);
  date D8(2,26,2000);
  date D9(2,26,1900);
  date D10(2,26,1999);
  date D11(25);
  date D12(364);
  date D13(D10);
  D1.print(); D1.print(1); D1.print(2);
  D2.print(); D2.print(1); D2.print(2);
  D3.print(); D3.print(1); D3.print(2);
  D4.print(); D4.print(1); D4.print(2);
  D5.print(); D5.print(1); D5.print(2);
  D6.print(); D6.print(1); D6.print(2);
  D7.print(); D7.print(1); D7.print(2);
  D8.print(); D8.print(1); D8.print(2);
  D9.print(); D9.print(1); D9.print(2);
  D10.print(); D10.print(1); D10.print(2);
  D11.print(); D11.print(1); D11.print(2);
  D12.print(); D12.print(1); D12.print(2);
  D13.print(); D13.print(1); D13.print(2);
  cout << "5 days after "; D7.print(1);
  for (i = 1; i <= 5; i++) D7.increment(); D7.print(1);
  cout << "5 days after "; D8.print(1);
  for (i = 1; i <= 5; i++) D8.increment(); D8.print(1);
  cout << "5 days after "; D9.print(1);
  for (i = 1; i <= 5; i++) D9.increment(); D9.print(1);
  cout << "5 days after "; D10.print(1);
  for (i = 1; i <= 5; i++) D10.increment(); D10.print(1);
  cout << "35 days after "; D1.print(1);
  for (i = 1; i <= 35; i++) D1.increment(); D1.print(1);
  return 0;
}

******  Output  ******

05/01/09                    - today’s date
May 1, 2009
01MAY09
09/19/00
September 19, 2000
19SEP00
07/15/49
July 15, 2049
15JUL49
01/10/02
January 10, 2002
10JAN02
01/01/48
January 1, 2048
01JAN48
10/01/59
October 1, 1959
01OCT59
02/26/36
February 26, 1936
26FEB36
02/26/00
February 26, 2000
26FEB00
02/26/00
February 26, 1900
26FEB00
02/26/99
February 26, 1999
26FEB99
01/25/02
January 25, 2002
25JAN02
12/30/02
December 30, 2002
30DEC02
02/26/99
February 26, 1999
26FEB99
5 days after February 26, 1936
March 2, 1936                <- you figure it out
5 days after February 26, 2000
March ?, 2000                <- you figure it out
5 days after February 26, 1900
March ?, 1900                <- you figure it out
5 days after February 26, 1999
March ?, 1999                <- you figure it out
35 days after May 1, 2009
? ?, 2009
date destructed: 02/26/99
date destructed: 12/30/02
date destructed: 01/25/02
date destructed: 03/03/99

沒有留言:

張貼留言