#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date {
friend ostream& operator<<(ostream& _cout, const Date& d);
public:
//构造
Date(int year = 1900, int month = 1, int day = 1)
{
if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > GetDay(year, month))
{
cout << "非法日期,已设置为默认值:1900-1-1" << endl;
_year = 1900;
_month = 1;
_day = 1;
}
_year = year;
_month = month;
_day = day;
}
//拷贝构造
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//获取天数以便后面进位
int GetDay(int year, int month)
{
//有12个月
static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month - 1];
if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
day++;
}
}
return day;
}
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date& operator+=(int days)
{
if (days < 0)
return *this -= -days;
_day += days;
//然后判断当前天数是否大于当月天数
while(_day > GetDay(_year, _month))
{
//减去当月天数
_day -= GetDay(_year, _month);
//月份进位
++_month;
//判断月份是否合理
if (_month == 13)
{
//如果月份超限,年份进位,月份变为一月
_month = 1;
++_year;
}
}
return *this;
}
Date& operator-=(int days)
{
if (days < 0)
return *this += -days;
_day -= days;
while (_day <= 0)
{
--_month;
if (_month <= 0){
_month = 12;
--_year;
}
_day += GetDay(_year, _month - 1);
}
//cout << _year << _month << _day << endl;
return *this;
}
Date operator+(int days)
{
Date ret(*this);
ret += days;
return ret;
}
Date operator-(int days)
{
Date ret(*this);
ret -= days;
return ret;
}
int operator-(const Date& d)
{
Date d1(*this);
Date d2(d);
int day = 0;
if (d1 > d2)
{
while (d1 > d2)
{
--d1;
++day;
}
return day;
}
else
{
while (d1 < d2)
{
++d1;
++day;
}
return -day;
}
}
//++date
Date& operator++()
{
return *this += 1;
}
//date++
Date operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
//--date
Date& operator--()
{
return *this -= 1;
}
//date--
Date operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
bool operator>(const Date& d)const
{
return _year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day == d._day;
}
bool operator>=(const Date& d)const
{
return (*this > d) || (*this == d);
}
bool operator<(const Date& d)const
{
return _year < d._year ||
_year == d._year && _month < d._month ||
_year == d._year && _month == d._month && _day < d._day;
}
bool operator<=(const Date& d)const
{
return (*this < d) || (*this == d);
}
bool operator==(const Date& d)const
{
return (_year == d._year)
&& (_month == d._month)
&& (_day == d._day);
}
bool operator!=(const Date& d)const
{
return !(*this == d);
}
/*void operator<<(ostream& _cout)
{
_cout << _year << "-" << _month << "-" << _day;
}*/
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
int main()
{
Date d(2018, 9, 9);
d -= 30;
cout << d << endl;
//d.operator<<(cout);
//d << cout;
system("pause");
return 0;
}
相关推荐
【C++】万年历的实现
/****************************************************************************************** Date.hpp: Copyright (c) Bit Software, Inc.(2013), All rights reserved. Purpose: 声明并实现一个万年历类【腾讯面试题】
C++编写万年历,公元后日历程序,考虑了1582年前后以及该年的特殊情况。
公元后较为完善的万年历创建程序
C++实现万年历(附代码)
本文给大家介绍如何使用C++语言解决万年历问题
C++万年历课程设计
C++课程设计——万年历 功能要求: (1)输入一个年份,输出是在屏幕上显示该年的日历。假定输入的年份在1940-2040年之间。 (2)输入年月,输出该月的日历。 (3)输入年月日,输出距今天还有多少天,星期几,是否是公历节日。 一.设计目的 1:要求学生能够熟练掌握C++语言的基本知识和技能。 2:基本掌握面向过程程序设计的基本思路和方法。 3:能够利用所学的基本知识和技能,解决简单的面向过程程序设计问题。 4:培养自己独立分析问题、解决问题、查阅资料以及自学能力,综合运用所学知识,解决实际问题,提高程
实现万年历的C++编程
用户可以通过输入不同的年份和月份来运行程序,程序将根据输入的年份和月份输出相应的月历。函数首先输出年份和月份的标题,然后输出星期的标题,接着根据给定的年份和月份计算该月的天数和第一天是星期几。以下是一个用C++编写的万年历程序,它可以根据用户输入的年份和月份显示一个完整的月历。程序中使用了基本的日期计算和条件判断来确定每个月的天数和每个月的第一天是星期几。函数用于计算某个月份的天数,对于2月份的天数需要根据是否是闰年进行判断,其他月份的天数比较固定。函数用于判断某一年是否是闰年,它根据闰年的定义进行判断。
基于C++开发的(控制台)万年历系统
利用C++语言的程序将日常生活中最经常接触到的日历查询编成一个简单的万年历查询系统。该系统具有多项功能,包括当前时间显示,年历查询,月历查询,日历查询功能等。该系统还具有菜单项,方便用户使用相关功能。
[C/C++/初学者]万年历(输入年份和月份,输出对应的日历表——数组)
紧接着就是空白问题了,即如果我们所求的该月的第一天为星期五,则前面星期日到星期四的数据为空,用k来保存这个数据,并通过循环输出。最后就可以输出我们需要的日期啦,值得注意的是,由于我们是以星期日为始,所以每当日期来到星期六时,我们就需要对其进行换行。即,我们求2022年12月的日历表,那么就计算出了从1年1月1日到2022年11月的全部天数之和。这样一来,我们就得到了指定年份之前的总天数,那么接下来就要计算指定月份之前的总天数了。利用循环,将指定月份前的天数与之前的数据相加,就得到指定月份之前的全部天数。
ptrade量化策略之ST股弱转强,回测年化115%
本策略的核心是精选ST股票,国九条过滤,防止直接暴雷退市,只做ST股票的弱转强
C++ 万年历 查找年、月、日、星期数以及节日等信息
C++ 万年历 显示公历的万年历,输入的年份在1800-2100年之间。功能要求如下: (1)输入一个年份,输出是在屏幕上显示该年的日历。 (2)输入年、月,输出该月的日历。 (3)输入年月日,输出距今天还有多少天,星期几,是否是为公历节日。 部分算法 程序主要分为四个部分:菜单,年历,月历,具体某天的日历信息。 年历 void nianli() //输出一整年的年历 { cout << "请输入年份:"; runnian(); system("cls");
大疆上云API-1.10.0版本-设备位置数据实时处理与WebSocket推送实战解析
本文深入解析了大疆上云API 1.10.0版本中设备位置数据的实时处理与推送全链路。通过剖析源码,详细阐述了从MQTT接收OSD数据、Spring Integration路由分发、业务逻辑处理到WebSocket实时推送至前端地图的核心流程,并提供了实战优化与避坑指南,帮助开发者构建高可靠的无人机实时定位系统。
C#实战:通过ZPL指令与斑马打印机交互及状态监控全解析
本文全面解析了在C#中通过ZPL指令与斑马打印机进行交互及状态监控的实战方法。文章详细介绍了TCP/IP网络打印和Windows Raw打印两种核心连接方式,并深入讲解了ZPL指令的构建。重点阐述了通过SGD命令和斑马官方SDK实现实时打印状态监控的策略,为工业自动化场景下的可靠打印提供了完整解决方案。
【C++】实现万年历
1.此万年历功能 1>日期加减天数 2>日期与日期之间的差值 3>输入年月显示当月日历 2.代码实现 #include #include using namespace std; class Date { public: Date(int year = 1990, int month = 1, int day = 1) //构造函数 :_year(year), _month(m
ROS基础(一)——命令行代码(以小乌龟程序为例)
ROS学习(一)——命令行代码(以小乌龟程序为例)1 基础命令行语句1.1 roscore1.2 rqt_graph(显示系统计算图)1.3 rosnode(显示所有节点相关信息)1.4 rostopic(和话题相关)2 服务类型指令rosservice同时存在两只海龟3 指令记录 1 基础命令行语句 从大二四旋翼无人机编队后就没再接触过ROS了,最近由于科研任务需要重新捡起来,打算系统学一下!学习过程参考古月居老师的视频,有兴趣的同学可以去[BliBli]学习。 学习平台:Ubuntu 18.04 硬件
C++万年历
用C++写了个简易的万年历。 具体功能如下:1.打印指定年(用户输入)所有月份的年历 2.打印指定年指定月(用户输入)的月份 3.打印指定日期(用户输入)的星期数 4.可重复输入 贴上源码: #include #include
rk3566-11.0新增device分区
1.增加块设备 2.修改根目录节点权限 3.开机启动自动挂载 增加块设备 1.修改parameter.txt文件,新增块设备大小及命名 device/rockchip/rk356x/rk3566_r/parameter.txt FIRMWARE_VER: 11.0 MACHINE_MODEL: rk3566_r MACHINE_ID: 007 MANUFACTURER: rockchip MAGIC: 0x5041524B ATAG: 0x00200800 MACHINE
lidar点云比赛-Southlidar软件使用入门
lidar点云比赛-Southlidar软件使用入门 广西高校重点实验室培训基地 系统控制与信息处理重点实验室 本篇博客来自河池学院: 智控无人机小组 写作时间:2020.09.29 一、软件资源及安装 1.下载地址: https://pan.baidu.com/s/1jN1pynmAZOqj5mMx774pVg 提取码:gkvv (资源来自南方测绘) 2.安装 进入链接,找到软件文件夹, 内含文件即为安装包 3.下载成功后按提示安装即可 4.安装成功后桌面自动创建一个快捷方式 二、界面布
【课设】C++实现万年历
一、万年历要求的功能 二、代码块 #include #include using namespace std; class date//定义一个日期类 { public: date(int y=1,int m=1,int d=1):year(y),month(m),day(d){}//构造函数 void setyear (int y){year=y;} //改变年份的接
yakit 通过codec 解决RSA加密
靶场就不过多叙述,具体的可以看yakit系列的前几篇文章。yakit 热加载进行 RSA 加密如何显示想要的payloads。
C++实现简易的万年历
C++实现简易的万年历 代码如下: /* *文件名称:万年历.cpp *作 者:chenghan *完成日期:2019/1/10 *版 本 号:1.0 *问题描述:制作一个简单的万年历 */ #include&amp;lt;iostream&amp;gt; #include &amp;lt;string&amp;gt; using namespace std; //判断一年是否为闰年,是返回true 否返回
vLLM 显存泄漏与 OOM 深度排查:从日志到火焰图
系统性地排查 vLLM 运行中的显存泄漏和 OOM 问题,包括 CUDA 内存快照、PyTorch 内存分析器、火焰图工具。
efm32 入门.pdf
本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。
[C++学习]万年历
//万年历程序#include #include //判断某年是否是闰年bool isLeapYear(int year){ if((year%4==0&&year%100!=0)||(year%400==0)) return true; else return false;}//根据年和月计算天数int daysOfMonth(int year,int month){ int result;
Statistics and Machine Learning Toolbox
Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具
【c++】万年历
上部分为日期之间的简单运算,下部分为日期类写的万年历 int month_days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; #include #include #include using namespace std; class Date { public: Date(int year=0,int month=0,int day=0) {
霍尼韦尔SoftMaster使用说明
霍尼韦尔SoftMaster使用说明
Camera2Demo
在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作
C++C#输出万年历C#含农历
农历在C++项目内调用DLL:在我的资源里面提供这个DLL文件下载。 void 输出万年历(int 年, int 月, int 日) { int r = 1, z = r, nn = 年, yy = 月; nn = 年; yy = 月; while (nn)if (--yy)z += 返回月的天数(nn, yy); else --nn, yy = 13; nn = 返回月的天数(年, ...
实现C++的万年历类
实现C++的万年历类 一直想做这个东西,从vc知识库搜索到一个,可是代码算法有问题,许多的结果都是错的。其他的代码都是js的,没办法,自己动手。代码虽短,心意一片。从此后你想为自己的软件增添点特色,放心的用吧,可以任意修改,传播。#ifndef _CALENDAR_H#define _CALENDAR_H#include struct myDATE{ int
c++编写万年历
<br />#include <iostream><br />#include <iomanip>using namespace std;<br />class Data<br />{ private: int year; // int monthdays[N];<br />public: void getyear(); //提示用户输入想要查询的年份<br />int ioleapyear(); //判断该年是不是闰年<br />int weekday();//判断该年的元旦是星期几 // void
1892



被折叠的 条评论
为什么被折叠?



