#include<iostream>
using namespace std;
class Date
{
public:
int GetMonthDay(int year, int month)
{
static int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int day = days[month];
if((month == 2)&&(((year % 4 == 0)&&(year % 100 != 0))||(year%400 ==0)))
{
day+=1;
}
return day;
}
Date(int year = 2000, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)//拷贝构造函数
{
_year = d._year;
_month =d._month;
_day = d._day;
}
Date& operator+=(int day)//+=运算符重载
{
if(day < 0)
{
return *this -= -day;
}
_day += day;
while(_day > GetMonthDay(_year,_month))
{
_day -=GetMonthDay(_year,_month);
_month++;
if(_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date& operator-=(int day)//-=运算符重载
{
if(day < 0)
{
return *this += -day;
}
_day -= day;
while(_day < 0)
{
_month--;
if(_month == 0)
{
_year--;
_month = 12;
}
_day +=GetMonthDay(_year,_month);
}
return *this;
}
Date& operator=(const Date& d)//赋值运算符重载
{
if(this !=&d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
}
Date& operator++()//前置++运算符重载
{
*this+=1;
return *this;
}
Date operator++(int)//后置++运算符重载
{
Date ret(*this);
*this +=1;
return ret;
}
bool operator<(const Date& d)
{
if(_year < d._year)
{
return 1;
}
else if (_year == d._year)
{
if(_month < d._month)
{
return 1;
}
else if(_month == d._month)
{
if(_day < d._day)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
int operator-(const Date&d)//-运算符重载,用于计算日期差
{
Date max = *this;
Date min = d;
if(*this < d)
{
max = d;
min = *this;
}
int day =0;
while(min <max)
{
++(min);
++day;
}
return day;
}
int _year;
int _month;
int _day;
};
int main()
{
//Date d1(2024,9,5);
int i,j,k;
cout <<"请输入初始日期的年"<<'\n';
cin>>i;
cout <<"请输入初始日期的月"<<'\n';
cin>>j;
cout <<"请输入初始日期的日"<<'\n';
cin>>k;
Date d1(i,j,k);
cout <<"您输入的日期为:"<<'\t'<< d1._year << '-' << d1._month << '-' << d1._day <<endl;
int select;
cout <<"请输入需要进行的操作"<<'\n';
cout <<"1.日期加运算"<<'\n';
cout <<"2.日期减运算"<<'\n';
cout <<"3.计算日期差"<<'\n';
cin >>select;
if(select == 1)
{
int day1;
cout <<"请输入需要加的天数"<<endl;
cin >> day1;
d1+=day1;
cout << d1._year << '-' << d1._month << '-' << d1._day <<endl;
}
else if (select ==2)
{
int day2;
cout <<"请输入需要减的天数"<<endl;
cin >> day2;
d1-=day2;
cout << d1._year << '-' << d1._month << '-' << d1._day <<endl;
}
else if(select == 3)
{
int i1,j1,k1;
cout <<"请输入目标日期年"<<'\n';
cin>>i1;
cout <<"请输入目标日期月"<<'\n';
cin>>j1;
cout <<"请输入目标日期日"<<'\n';
cin>>k1;
Date d2(i1,j1,k1);
cout <<"您输入的日期为:"<<'\t'<< d2._year << '-' << d2._month << '-' << d2._day <<endl;
int ret = d1-d2;
cout <<"两个日期只差为:"<<' ' << ret << "天" <<endl;
}
else
{
cout << "您的输入有误"<<endl;
}
return 0;
}
![avatar](https://profile-avatar.csdnimg.cn/52466a5658fb4b448d288d20ffc74467_akoasm_.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
雪好像下整夜
- 粉丝: 156
- 资源: 2
最新资源
- COMSOL案例:多场耦合细观损伤模型在隧道衬砌结构中的应用,基于COMSOL的隧道衬砌结构多场耦合细观损伤模拟及损伤演化机制研究,COMSOL案例实现隧道衬砌结构多场耦合细观损伤 本案例以混凝土衬砌
- 尺寸精准测量与直线圆拟合技术-基于卡尺工具的智能应用研究,尺寸精准测量与直线、圆拟合技术-基于卡尺工具的应用与实现,尺寸测量,直线拟合,圆拟合,卡尺工具 ,核心关键词:尺寸测量; 直线拟合; 圆拟
- C语言系统调用详解:涵盖核心编程特性、实现机制及应用场景
- Visual Studio 2022 .NET Framework 4.0支持
- 共直流母线式风光储并网发电系统仿真模型:高效光伏Boost与风机MPPT控制,蓄电池储能双闭环,PQ控制并网电压电流THD小于5%的优异波形效果(2018b版),共直流母线式风光储并网发电系统仿真模型
- STM32G431高频注入无感FOC驱动技术详解:零速带载启动与堵转处理,三步高频注入零速启动策略及全套开发资源,STM32G431 HFI FOC方波高频注入无感驱动资料:零速带载启动与低速持续注入
- PHP面向对象编程(OOP)核心技术详解及应用场景
- 模拟分析PFC含纤维混凝土材料的单轴压缩破坏行为:数值模拟与实验验证,PFC模拟技术在纤维混凝土单轴压缩破坏分析中的应用研究,PFC含纤维混凝土材料单轴压缩破坏模拟 ,PFC模拟; 纤维混凝土; 单轴
- 嵌入式实验AB之间串口通信
- 基于51单片机红外遥控设计的车载娱乐系统LED显示应用与学习资料汇编,车载红外遥控LED显示系统设计资料:学习红外遥控、串口通信与数码管显示应用,51单片机红外遥控设计资料 一种车载娱乐产品,采用红外
- C++编程语言基础知识及应用案例详解
- 《EV3100电梯专用变频器源代码深度解析与优化实践》,EV3100电梯专用变频器源代码解析与实现细节探讨,EV3100电梯专用变频器源代码 ,EV3100电梯专用; 变频器; 源代码; 程序开发
- 台达DVP 16ES2与DT3温控器Modbus通讯程序开发(含温度设定、读取及控温启停功能详解),台达DVP 16ES2与DT3温控器通讯程序:Modbus协议下的温度控制与读取解决方案,台达DVP
- STM32G431高性能无感FOC驱动系统资料:方波高频注入加滑膜观测器,零速带载启动至中高速平滑过渡,全C语言代码带中文注释,方便移植与开发,STM32G431 HFI SMO FOC无感驱动资料:
- Screenshot_20250216_090613_com.huawei.browser.jpg
- 博弈论下的车辆换道模拟:定义参数、初始化状态,速度与换道决策受交通流影响 ,博弈论下的车辆换道模拟:定义参数、初始化状态,速度与换道决策受交通流影响 ,博弈论车辆道, 首先定义模拟所需的参数,包括车辆
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)