Bootstrap

C++声明一个表示时间的类CTime,可以精确表示年、月、日、小时、分、秒,请计算两个日期对象之间相隔的天数。

【问题描述】声明一个表示时间的类CTime,可以精确表示年、月、日、小时、分、秒,请计算两个日期对象之间相隔的天数。

要求:

1、包括私有成员年、月、日、小时、分、秒。

2、请使用构造函数实现的类的初始化工作,并判断日期和时间的有效性。年月日时分秒应该在正确的范围内。考虑闰年时候二月份的情况。时间的格式是xx:xx:xx,小时不是超过23,分钟和秒不能超过59。

  1)如果日期无效,则输出 “date error! ”  并将年、月、日、小时、分、秒置为0。

  2)如果时间无效,则输出 “time error! ”  并将年、月、日、小时、分、秒置为0。

  3)如果日期和时间都有效,则根据传递的参数初始化年、月、日、小时、分、秒。

  4)构造函数的三个参数:小时、分、秒 设计为默认形成,其默认值为0。

  5)  输出"构造函数被调用"

3、请设计一个拷贝构造函数,实现将参数的值全部传递给当前对象,同时输出“拷贝构造函数被调用”

4、请设计一个析构函数,同时输出“析构函数被调用”

5、设计一个成员函数 int dayDiff(CTime t) ,用于计算当前对象与形参t之间的相隔的天数,注意相隔天数为大于等于0的正整数。注意闰年的问题。

6、设计一个成员函数 showTime(),用于显示日期,显示格式为:2020/3/12 11:50:20

提示:除了上传要求的成员函数外,你可以自由添加需要的成员函数。

main函数已经给定,请补全其他代码。

【样例输入1】

2020 3 12 11 50 20

【样例输出1】

构造函数被调用

2020/3/12 11:50:20

构造函数被调用

拷贝构造函数被调用

析构函数被调用

这两天之间相隔了7376天

拷贝构造函数被调用

析构函数被调用

这两天之间相隔了7376天

析构函数被调用

析构函数被调用

【样例输入2】

2100 2 29 10 29 59
【样例输出2】

date error!

构造函数被调用

0/0/0 0:0:0

构造函数被调用

析构函数被调用

析构函数被调用

【样例输入3】

2000 2 29 15 79 40
【样例输出3】

time error!

构造函数被调用

0/0/0 0:0:0

构造函数被调用

析构函数被调用

析构函数被调用

【样例输入4】

2007 9 31 12 89 89
【样例输出4】

date and time error!

构造函数被调用

0/0/0 0:0:0

构造函数被调用

析构函数被调用

析构函数被调用
【C++代码】

#include<iostream>
using namespace std;
//日期合理 
int isdate(int year,int month,int day){
	if(((year%4==0) && (year%100!=0))||year%400==0){
		if(month==2&&day>29) return 0;
	};
	if(month>12)   return 0;
	
	if((month==2||month==4||month==6||month==9||month==11)&&day>30) return 0;
	else if ((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31) return 0;
	else return 1;
};
//时间合理
int istime(int hour,int minute,int second){
	if(hour>23||minute>59||second>59) return 0;
	else return 1;
};
//判断闰年
int IsLeapYear(int year)    
{  
    return (((year%4==0) && (year%100!=0))||year%400==0); 
}; 
//计算月份相差的时间
int fun(int a,int b){
	int num=0,i;
	for(i=b-1;i>=1;i--){
		if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) num+=31;
		if(i==4||i==6||i==9||i==11) num+=30;
		if(i==2) num=num+28+IsLeapYear(a);
}
	return num; 
};
//                              类
class CTime{
private:
	int y,n,d,h,m,s;
public:
	CTime(int y1,int n1,int d1,int h1,int m1,int s1);//构造函数
	CTime(CTime& p);//拷贝构造函数 
	~CTime(); 
	int isvalid();//日期和时间是否合理 
	void showTime();//输出日期 
	int dayDiff(CTime t);//计算日期差 
	  
};
//gouzaohanshu
CTime::CTime(int y1=0,int n1=0,int d1=0,int h1=0,int m1=0,int s1=0){
	if(isdate(y1,n1,d1)&&istime(h1,m1,s1)){
			y=y1;
			n=n1;		
			d=d1;
			h=h1;
			m=m1;
			s=s1;
		}
	else{
			if(isdate(y1,n1,d1)&&!istime(h1,m1,s1))		cout<<"time error!"<<endl;
			else if(!isdate(y1,n1,d1)&&istime(h1,m1,s1)) cout<<"date error!"<<endl;
			else	cout<<"date and time error!"<<endl;
			y=n=d=h=m=s=0;
		}
	cout<<"构造函数被调用"<<endl;
}
//拷贝构造函数
CTime::CTime(CTime& p){
	y=p.y;
	n=p.n;
	d=p.d;
	h=p.h;
	m=p.m;
	s=p.s;
	cout<<"拷贝构造函数被调用"<<endl; 
} 
//输出日期 
void CTime::showTime() {
	cout<<y<<"/"<<n<<"/"<<d<<" "<<h<<":"<<m<<":"<<s<<endl;
}
//析构函数 
CTime::~CTime() {
	cout<<"析构函数被调用"<<endl;
}
//日期时间均合法 
int CTime::isvalid(){
	if(n<=12&&h<=23&&m<=59&&s<=59){
		if(((y%4==0) && (y%100!=0))||y%400==0){
			if(((n==1||n==3||n==5||n==7||n==8||n==10||n==12)&&d<=31)||((n==2)&&d<=28)||((n==4||n==6||n==9||n==11)&&d<31))return 1;
			else return 0;
		} 
		else{
			if(((n==1||n==3||n==5||n==7||n==8||n==10||n==12)&&d<=31)||((y==2)&&d<=28)||((n==4||n==6||n==9||n==11)&&d<31))return 1;
			else return 0;
		}
	} 
	else return 0;
}
//计算日期差
int CTime::dayDiff(CTime t){
	int day=0,i;
	int a,a2,a3,b,b2,b3;
	if(y>t.y){
			a=y,a2=n,a3=d;
			b=t.y,b2=t.n,b3=t.d;
		}
	else	 {
			a=t.y,a2=t.n,a3=t.d;
			b=y,b2=n,b3=d;
		};
	if (a==b){                  //年相等
		day=a3-b3+fun(a,a2)-fun(b,b2);
		if(day<0) day=-day;
		return day;	
	}
	else{   
		for(i=b;i<a;i++) day=day+365+IsLeapYear(i);
		day=day+a3-b3+fun(a,a2)-fun(b,b2);
		if(day<0) day=-day;
		return day;
	}		
}
//主函数 
int  main() 
{ 
	int  year,  month,  day,  hour,  minute,  second; 
    cin >>year>>month>>day>>hour>>minute>>second; 
    CTime  t1(year,month,day,hour,minute,second); 
    t1.showTime(); 
    CTime  t2(2000,  1,  1);  //利用默认形参将时间初始化为00:00:00 
    if  (t1.isvalid())        //如果日期和时间合法,则计算天数 
    { 
        int days=0; 
        days=t1.dayDiff(t2); 
        cout<<"这两天之间相隔了"<<days<<"天"<<endl;
        days=t2.dayDiff(t1); 
        cout<<"这两天之间相隔了"<<days<<"天"<<endl; 
    } 
}

【测试结果】
在这里插入图片描述

;