C++借助运算符重载实现CDate类

这篇博客展示了如何在C++中创建一个CDate类,并实现日期的加法和减法运算符重载。通过友元函数,实现了日期与整数相加减以及两个日期之间的差值计算,考虑了闰年的特殊情况。代码中还包含了一个简单的主函数来演示这些运算。
摘要由CSDN通过智能技术生成

1.创建CDate类

class CDate{
	public:
		CDate(int y=2002,int m=4,int d=7,int c=0){
			year=y;
			month=m;
			day=d;
			cnt=c;
		}
		void print(){
			cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
		} //打印函数
		operator int(){
			return cnt;
		} //类型转换
		friend CDate operator+(CDate ob,int x); //友元运算符重载,计算日期加上天数
		friend CDate operator-(CDate ob,int x); //友元运算符重载,计算日期减去天数
		friend CDate operator-(CDate obj1,CDate obj2); //计算两个日期之间的时间间隔
	private:
		int year,month,day,cnt;
};

2.+运算符重载

CDate operator+(CDate ob,int x){
			CDate temp;
			int year_st=ob.year;
			int month_st=ob.month;
			int day_st=ob.day;
			for(int i=1;i<=x;i++){
				day_st++;
				if(month_st==2){
					int leap=(year_st%4==0&&year_st%100!=0)||(year_st%400==0);
					if(day_st>monthss[2]+leap){
						month_st++;
						day_st=1;
					}
				}else{
					if(day_st>monthss[month_st]){
						month_st++;
						day_st=1;
					}
				}
				if(month_st>=13){
					month_st=1;
					year_st++;
					day_st=1;
				}
			}
			temp.year=year_st;
			temp.month=month_st;
			temp.day=day_st;
			return temp;
}

3.-运算符重载

CDate operator-(CDate ob,int x){
	CDate temp;
	int year_st=ob.year;
	int month_st=ob.month;
	int day_st=ob.day;
	for(int i=0;i<x;i++){
				day_st--;
				if(day_st<=0){
					if(month_st==3){
						int leap=(year_st%4==0&&year_st%100!=0)||(year_st%400==0);
						day_st=monthss[2]+leap;
						month_st--;
					}else{
						if(month_st<=1) month_st=13,year_st--;
						month_st--;
						day_st=monthss[month_st];
					}
				}
			}
			temp.year=year_st;
			temp.month=month_st;
			temp.day=day_st;
			return temp;
}

4.-运算符重载2

CDate operator-(CDate obj2,CDate obj1){
	CDate temp;
	int year_st=obj1.year;
	int month_st=obj1.month;
	int day_st=obj1.day;
	int year_end=obj2.year;
	int month_end=obj2.month;
	int day_end=obj2.day;
	int cnt=temp.cnt;
	while(1){
				day_st++;
				cnt++;
				if(month_st==2){
					int leap=(year_st%4==0&&year_st%100!=0)||(year_st%400==0);
					if(day_st>monthss[2]+leap){
						month_st++;
						day_st=1;
					}
				}else{
					if(day_st>monthss[month_st]){
						month_st++;
						day_st=1;
					}
				}
				if(month_st>=13){
					month_st=1;
					year_st++;
					day_st=1;
				}
		if(year_st==year_end&&month_st==month_end&&day_st==day_end) break;
	}
	temp.cnt=cnt;
	temp.year=year_st;
	temp.month=month_st;
	temp.day=day_st;
	return temp;
}

5.完整代码

#include<iostream>
using namespace std;
int monthss[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
class CDate{
	public:
		CDate(int y=2002,int m=4,int d=7,int c=0){
			year=y;
			month=m;
			day=d;
			cnt=c;
		}
		void print(){
			cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
		}
		operator int(){
			return cnt;
		}
		friend CDate operator+(CDate ob,int x);
		friend CDate operator-(CDate ob,int x);
		friend CDate operator-(CDate obj1,CDate obj2);
	private:
		int year,month,day,cnt;
};
CDate operator+(CDate ob,int x){
			CDate temp;
			int year_st=ob.year;
			int month_st=ob.month;
			int day_st=ob.day;
			for(int i=1;i<=x;i++){
				day_st++;
				if(month_st==2){
					int leap=(year_st%4==0&&year_st%100!=0)||(year_st%400==0);
					if(day_st>monthss[2]+leap){
						month_st++;
						day_st=1;
					}
				}else{
					if(day_st>monthss[month_st]){
						month_st++;
						day_st=1;
					}
				}
				if(month_st>=13){
					month_st=1;
					year_st++;
					day_st=1;
				}
			}
			temp.year=year_st;
			temp.month=month_st;
			temp.day=day_st;
			return temp;
}
CDate operator-(CDate ob,int x){
	CDate temp;
	int year_st=ob.year;
	int month_st=ob.month;
	int day_st=ob.day;
	for(int i=0;i<x;i++){
				day_st--;
				if(day_st<=0){
					if(month_st==3){
						int leap=(year_st%4==0&&year_st%100!=0)||(year_st%400==0);
						day_st=monthss[2]+leap;
						month_st--;
					}else{
						if(month_st<=1) month_st=13,year_st--;
						month_st--;
						day_st=monthss[month_st];
					}
				}
			}
			temp.year=year_st;
			temp.month=month_st;
			temp.day=day_st;
			return temp;
}
CDate operator-(CDate obj2,CDate obj1){
	CDate temp;
	int year_st=obj1.year;
	int month_st=obj1.month;
	int day_st=obj1.day;
	int year_end=obj2.year;
	int month_end=obj2.month;
	int day_end=obj2.day;
	int cnt=temp.cnt;
	while(1){
				day_st++;
				cnt++;
				if(month_st==2){
					int leap=(year_st%4==0&&year_st%100!=0)||(year_st%400==0);
					if(day_st>monthss[2]+leap){
						month_st++;
						day_st=1;
					}
				}else{
					if(day_st>monthss[month_st]){
						month_st++;
						day_st=1;
					}
				}
				if(month_st>=13){
					month_st=1;
					year_st++;
					day_st=1;
				}
		if(year_st==year_end&&month_st==month_end&&day_st==day_end) break;
	}
	temp.cnt=cnt;
	temp.year=year_st;
	temp.month=month_st;
	temp.day=day_st;
	return temp;
}
int main(){
	CDate bir(2002,4,7);
	CDate bir2(2022,4,20);
	//bir=bir+10;
	//bir2=bir2-10;
	cout<<"起始日期:"<<endl;
	bir.print();
	cout<<"终止日期:"<<endl; 
	bir2.print();
	cout<<"时间间隔:"<<endl;
	cout<<(bir2-bir);
	return 0;
}
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值