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);
cout<<"起始日期:"<<endl;
bir.print();
cout<<"终止日期:"<<endl;
bir2.print();
cout<<"时间间隔:"<<endl;
cout<<(bir2-bir);
return 0;
}