Home > Back-end >  Define a class Date Date, the main () function to finish the test
Define a class Date Date, the main () function to finish the test

Time:03-29

Define a class Date Date, the main () function to finish the test,

The Date class structure elucidation:
The Date class data members include:
Private data members: (1) year year (int), month to month (int), day day (int),
The Date class member functions include:
1) define the parameter constructor Date (int, int, int) and copy constructor Date (Date & amp;) , including the Constructor parameters default value is 1, the output information "Constructor run", the copy Constructor output information "CopyConstructor run"
(2) define the Destructor, the Destructor output information "Destructor run"
(3) members of the public function: void setYear (int) and int getYear () returns and set the year
(4) members of the public function: void setMonth (int) and int getMonth () returns and set the month
(5) members of the public function: void setDay (int) and int getday () returns and set the day
Those members of the public function: the void tomorrow () and void yesterday () implementation date plus/minus one day operation
7 members of the public function: void printMonthCalendar () to print the month calendar, format is as follows (each column width to three, right-aligned) (in March 2016, for example)
SunMonTueWedThuFriSat
1, 2, 3, 4, 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20, 21, 22, 23 24 25 26
27 28, 29 and 30, 31
End members of the public function: void chineseFormat () and void americaformat () respectively show Chinese date (example: on April 21, 2014) and American date (example: Api 21, 2014), the American format 1 ~ 12 month respectively with Jan, Feb, Mar, Api, May, Jun, Jul, Aug, Sep and Oct, Nov, Dec said
Pet-name ruby private member function: int isLeapYear () to determine whether the current year is a leap year, leap year to return to 1, otherwise it returns 0;
Attending members of the public function: int weekDay () returns the day of the week, respectively 0 to 6 Sun, Mon, Tue, Wed and Thu Fri, Sat,
Note: the above in the execution of a member function, if the date of the input parameter value in excess of the world, is in accordance with the effective date value towards the closest to the input parameters, such as:
Assume that the input month> If 12, 12, according to the input of month<1, according to the 1 treatment;
Assume that the current month to 12 month, if the input day> 31, according to 31, if the input day<1, according to the 1 treatment;
Tip: cout. Width (int) set the width of the output item

The referee sample testing program:
#include
using namespace std;
31,28,31,30,31,30,31,31,30,31,30,31 int const monthDay [12]={};
Const char * weekName [7]={" Sun ", "Mon, Tue, Wed," Thu ", "Fri", "Sat"};
Char * const monthName [12]={" Jan ", "Feb", "Mar", "Api", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov," "Dec"};

Please fill in the answer here */*/

Int main () {
Int year, month, day.
The Date d1;
The Date (d1, d2);
Cin> Year> Month> Day;
D1. SetYear (year);
D1. SetMonth (month);
D1. SetDay (day);
D1. Yesterday ();
D1. ChineseFormat ();
Cin> Year> Month> Day;
D2. SetYear (year);
D2. SetMonth (month);
D2. SetDay (day);
D2. Tomorrow ();
D2. Americaformat ();
D2. PrintMonthCalendar ();
return 0;
}
Input the sample:
2019 3 27
2016 2 31
The output sample:
The Constructor run
CopyConstructor run
On March 26, 2019
Mar 6 1201
SunMonTueWedThuFriSat
1, 2, 3, 4, 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20, 21, 22, 23 24 25 26
27 28, 29 and 30, 31
Destructor run
Destructor run

CodePudding user response:

I write good, output

CodePudding user response:

 
//head
# define DATE_H
#include
#include
#include
The class Date
{
Private:
Int year;
Int the month;
Int day;
Int isLeapYear (int _year)
{
If ((year % 4==0 & amp; & Year % 100!=0) | | year %==0 400)
return 1;
The else
return 0;
}
Public:
Date () : the year (1990), the month (1), day (1) {STD: : cout & lt; <"Constructor run \ n"; }
The Date (int _y, int _m, int _d) : the year (_y), month (_m), day (_d) {STD: : cout & lt; <"Constructor run \ n"; }
The Date (const Date & amp; D) : the year (d.y ear), month (d.m onth), day (d.d ay) {STD: : cout & lt; <"CopyConstructor run \ n"; }
Date& Operator=(const Date&);
To Date () {STD: : cout & lt; <"Destructor run \ n"; }
Void setYear (int y) {year=y; }
Int getYear () {return year; }
Void setMonth (int m) {month=m; }
Int getMonth () {return the month; }
Void setDay (int) d {day=d; }
Int getDay () {return day; }
Int nyear (int _y);
Int nmonth (int _y, int _m);
Int getDays (int y, int m);
Int weekDay ();
Void printMonthCalendar ();
Void chineseFormat ();
Void amerciaFormat ();
Bool ifMonthOverload (int y, int m, int d);
Bool ifDayOverload (int y, int m, int d);
Void tomorrow ();
Void yesterday ();
};
Date& Date: : operator=(const Date & amp; Rdate)
{
Year=rdate. Year;
The month=rdate. The month;
Day=rdate. Day;
Return * this;
}
The inline int the Date: : nyear (int _y)
{
If (isLeapYear (_y))
The return of 366;
The else
The return of 365;
}
The inline int the Date: : nmonth (int _y, int _m)
{
If (_m==1 | | _m==8 | | _m==5 | | _m==7 | | _m==8 | | _m==10 | | _m==12)
The return of 31;
Else if (nyear (_y)==366 & amp; & _m==2)
The return of 29;
Else if (nyear (_y)==365 & amp; & _m==2)
The return of 28;
The else
Return 30;
}
The inline int the Date: : getDays (int y, int m)
{
int sum=0;
For (int I=1990; I & lt; Year; I++)
The sum +=nyear (I);
If (m & gt; 1)
For (int I=1; I & lt; m; I++)
The sum +=nmonth (y, I);
Return the sum.
}
The inline void Date: : printMonthCalendar ()
{
Int sum=getDays (year, month);
Int front_space=sum (+ 1) % 7;
Int days=nmonth (year, month);
STD: : cout & lt; <"SunMonTueWedThuFriSat" & lt; For (int I=0; I & lt; Front_space; I++)
STD: : cout & lt;
  • Related