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

C++借助运算符重载实现CDate类,第1张

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
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;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/713242.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-24
下一篇2022-04-24

发表评论

登录后才能评论

评论列表(0条)

    保存