首页 > 其他分享 >定义日期类Date

定义日期类Date

时间:2023-05-21 13:55:08浏览次数:24  
标签:cout int void month 日期 year Date day 定义

定义一个日期类Date,main()函数完成对其的测试。

Date类结构说明:

 
Date类的数据成员包括:
①私有数据成员:年year(int型),月month(int型),日day(int型)。
Date类成员函数包括:
①定义有参构造函数Date(int ,int ,int )和拷贝构造函数Date(Date &),其中有参构造函数参数默认值为1,输出信息“Constructor run”,拷贝构造函数输出信息“CopyConstructor run”
②定义析构函数,析构函数输出信息“Destructor run”
③公有函数成员:void  setYear(int )和int  getYear()分别设置和返回year
④公有函数成员:void  setMonth(int )和int  getMonth()分别设置和返回month
⑤公有函数成员:void  setDay(int )和int  getday()分别设置和返回day
⑥公有函数成员:void  tomorrow( )和void  yesterday( )实现日期加/减一天操作
⑦公有函数成员:void  printMonthCalendar( )打印当月日历,格式如下(每列宽度为3位,右对齐)(以2016年3月为例)
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 30 31
⑧公有函数成员:void  chineseFormat( )和void  americaformat( )分别显示中式日期(例:2014年4月21日)和美式日期(例:Api 21, 2014),其中美式格式中1~12月分别用Jan、Feb、Mar、Api、May、Jun、Jul、Aug、Sep、Oct、Nov、Dec表示
⑨私有函数成员:int  isLeapYear( )确定当前年份是否为闰年,闰年返回1,否则返回0;
⑩公有函数成员:int  weekDay( )返回该天是星期几,0~6分别表示Sun、Mon、Tue、Wed、Thu、Fri、Sat
 

注:在以上成员函数执行过程中,若输入的日期参数值超界,则按照最接近输入参数的有效日期值对待,例如:

假定输入的month>12,则按照12对待,若输入的month<1,则按照1对待;

假定当前month为12月,则若输入的day>31,则按照31对待,若输入的day<1,则按照1对待;

提示:cout.width(int)设置输出项的宽度

裁判测试程序样例:

 
#include<iostream>
using namespace std;
int const monthDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char* const 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"};

/*请在这里填写答案*/

int main(){
    int year,month,day;
    Date d1;
    Date d2(d1);
    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;
}
 

输入样例:

2019 3 27
2016 2 31
 

输出样例:

Constructor run
CopyConstructor run
2019年3月26日
Mar 1,2016
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 30 31
Destructor run
Destructor run



class Date
{
public:
Date(int x=1,int y=1,int z=1)
{
year=x;
month=y;
day=z;
cout<<"Constructor run"<<endl;
}
Date(Date &p)
{
year=p.year;
month=p.month;
day=p.day;
cout<<"CopyConstructor run"<<endl;
}
~Date()
{
cout<<"Destructor run"<<endl;
}
void setYear(int x)
{
year=x;
}
int getYear()
{
return year;
}
void setMonth(int m)
{
if(m<=12&&m>=1)
{
month=m;
}
else if(m>12)
{
month=12;
}
else
{
month=1;
}
}
int getMonth()
{
return month;
}
void setDay(int n)
{
if(n<=monthDay[month-1]&&n>=1)
{
day=n;
}
else if(n>monthDay[month-1])
{
day=monthDay[month-1];
}
else
{
day=1;
}
}
int getday()
{
return day;
}
void tomorrow()
{
day++;
if(day>monthDay[month-1])
{
month++;
day=1;
}
}
void yesterday()
{
day--;
if(day<1)
{
month--;
day=monthDay[month-1];
}
}
void printMonthCalendar()
{
int i,j,k;
int q=1;
j=k=weekDay();
cout<<"SunMonTueWedThuFriSat"<<endl;
while(k>=1&&k<=6)
{
cout<<" ";
k--;
}
if(j!=0)
{
for(i=0;i<28;i++)
{
if(i%7==7-j)
cout<<endl;
cout.width(3);cout<<q++;
}
if(month==14&&isLeapYear(year+1)==1)
{
if(i%7==7-j)
{cout<<endl;}
cout.width(3);cout<<q++;
}
if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)
{
for( ;i<31;i++)
{
if(i%7==7-j)
cout<<endl;
cout.width(3);cout<<q++;
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==7-j)
cout<<endl;
cout.width(3);cout<<q++;
}
}
cout<<endl;
}
else
{
for(i=0;i<28;i++)
{
cout.width(3);cout<<q++;
if(i%7==6&&i!=27)
cout<<endl;
}
if(month==14&&isLeapYear(year+1)==1)
{
cout<<endl;
cout.width(3);cout<<q++;
}

if(month==13||month==3||month==5||month==7||month==8||month==10||month==12)
{
for( ;i<31;i++)
{
if(i%7==0)
cout<<endl;
cout.width(3);cout<<q++;
}
}
if(month==4||month==6||month==9||month==11)
{
for( ;i<30;i++)
{
if(i%7==0)
cout<<endl;
cout.width(3);cout<<q++;
}
}
}
}
void chineseFormat()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
void americaformat()
{
cout<<monthName[month-1]<<" "<<day<<","<<year<<endl;
}
int weekDay( )
{
int i;
if(month==1)
{
year--;
month=13;
}
if(month==2)
{
year--;
month=14;
}
i=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
switch(i)
{
case 0:return 1;break;
case 1:return 2;break;
case 2:return 3;break;
case 3:return 4;break;
case 4:return 5;break;
case 5:return 6;break;
case 6:return 0;break;
}
}
private:
int isLeapYear(int year)
{
if(((year%4)==0&&(year%100)!=0)||(year%400)==0)
{
return 1;
}
else{
return 0;
}
}
private:
int year;
int month;
int day;
};

标签:cout,int,void,month,日期,year,Date,day,定义
From: https://www.cnblogs.com/zljzy/p/17418528.html

相关文章

  • updatexml报错
      ......
  • 配置wordpress:自定义简码(wordpress 6.2)
    一,添加php代码的例子:1,代码://说明:得到当前时间functionlhdGetNowTime(){ $now=date("Y-m-dH:i:s");return$now;}//注册成简码,名字myNowadd_shortcode('myNow','lhdGetNowTime');//说明:获取完整URLfunctioncurPageURL(){ $pageURL='h......
  • 字符串转LocalDateTime
    /***yyyy-MM-ddHH:mm:ss转LocalDateTime*@paramexpectStartTime*@return*/publicstaticLocalDateTimestrToLocalDateTime(StringexpectStartTime){returnLocalDateTime.parse(expectStartTime,DateTimeFormatter.ofPattern("yyyy-MM-ddHH:......
  • 数据列表管理-底部的自定义代码参考
    <!--做数据的流转状态操作001--><divclass="div_bottom_control_location_area"id="div_rejected_or_approved"><buttononclick="submit_or_reject_for_review(0)"type="button"class="am-btnam-btn-dangerbtn_......
  • 数据列表管理-顶部查询条件的自定义代码参考
     银行:<selectid="sl_bank"class="fsbpmserachcontrolsearch_select"myts="sl"></select>状态:<selectid="sl_status_filter"class="fsbpmserachcontrolsearch_select"myts="sl"><......
  • C语言定义和声明
    1、定义:创建一个对象,为这个对象分配一块内存并给他去上一个名字,一个变量只能被定义一次。2、声明:告诉编译器,这个名字已经被匹配到一块内存上了,变量或对象是在别的地方定义的,声明可以出现很多次,声明是为了扩大变量的适用范围。......
  • DNS相关命令ping、host、nslookup、dig、nsupdate学习
    另外再标注一篇文章:http://zhumeng8337797.blog.163.com/blog/static/10076891420112108424555/1、ping  很好奇为什么返回的是www.a.shifen.com,whois一下:发现shifen.com也是百度的。  这个在知乎上也有相关回答:十分系统(www.a.shifen.com)是干什么的?和百度有什么关系? 2、host ......
  • el-table自定义表头
    实现效果<!--*@Descripttion:el-table自定义表头*@version:*@Author:zhangfan*@email:[email protected]*@Date:2020-05-1409:08:01*@LastEditors:zhangfan*@LastEditTime:2020-06-2918:53:17--><template><divclass="topCon......
  • el-tree实现自定义节点内容
    <!--*@Descripttion:el-tree实现自定义节点内容*@version:*@Author:zhangfan*@email:[email protected]*@Date:2020-07-0309:10:28*@LastEditors:zhangfan*@LastEditTime:2020-07-1611:21:20--><template><divclass="treeBo......
  • ExtJs GridPanel 自定义汇总
    {text:'订单金额',dataIndex:'amount',renderer:function(value){returnExt.util.Format.number(value,'0.00');},summaryType:function(records){varamount=0;varlength=records.length;for(var......