壁画网站建设,北京到石家庄,鞍山网站制作推广,wordpress删除作者信息文章目录 日期类的设计日期计算相关OJ题HJ73 计算日期到天数转换KY111 日期差值KY222 打印日期KY258 日期累加 在软件开发中#xff0c;处理日期是一项常见的任务。为了方便地操作日期#xff0c;我们可以使用C编程语言来创建一个简单的日期类。在本文中#xff0c;我们将介… 文章目录 日期类的设计日期计算相关OJ题HJ73 计算日期到天数转换KY111 日期差值KY222 打印日期KY258 日期累加  在软件开发中处理日期是一项常见的任务。为了方便地操作日期我们可以使用C编程语言来创建一个简单的日期类。在本文中我们将介绍如何使用C实现一个基本的日期类包括日期的加减、大小比较等功能。 日期类的设计 
下面是日期类的基本实现代码 
#pragma once
#includeiostream
using namespace std;class Date {
public:// 获取某年某月的天数int GetMonthDay(const int year, const int month);// 构造函数Date(int year  1900, int month  1, int day  1);// 拷贝构造函数Date(const Date d);// 析构函数~Date();// 打印日期void print()const;// 赋值运算符重载Date operator(const Date d);// 运算符重载Date operator(const int day);// 运算符重载Date operator(const int day);// -运算符重载Date operator-(int day);// -运算符重载Date operator-(int day);// 计算两个日期之间的天数差int operator-(const Date d) const;// 前置运算符重载Date operator();// 后置运算符重载Date operator(int);// --前置运算符重载Date operator--();// --后置运算符重载Date operator--(int);// 大于运算符重载bool operator(const Date d) const;// 等于运算符重载bool operator(const Date d) const;// 大于等于运算符重载bool operator  (const Date d) const;// 小于运算符重载bool operator  (const Date d) const;// 小于等于运算符重载bool operator  (const Date d) const;// 不等于运算符重载bool operator ! (const Date d) const;// 地址运算符重载const Date* operator  () const;// 输出流运算符重载friend ostream operator  (ostream out, const Date d);// 输入流运算符重载friend istream operator  (istream in, Date d);private:int _year;  // 年int _month;  // 月int _day;  // 日
};// 获取某年某月的天数
int Date::GetMonthDay(const int year, const int month) {int monthDay[13]  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (2  month  ((year % 4  0  year % 100 ! 0) || (year % 400  0))) {return 29;}return monthDay[month];
}// 构造函数
Date::Date(int year, int month, int day) {if ((year  1) || (month  1) || (month  12) || (day  1) || (day  GetMonthDay(year, month))) {cout  非法日期  endl;}_year  year;_month  month;_day  day;
}// 拷贝构造函数
Date::Date(const Date d) {_year  d._year;_month  d._month;_day  d._day;
}// 析构函数
Date::~Date() {_year  1900;_month  1;_day  1;
}// 打印日期
void Date::print()const {cout  _year  /  _month  /  _day  endl;
}// 赋值运算符重载
Date Date::operator(const Date d) {if (this ! d) {_day  d._day;_month  d._month;_year  d._year;}return *this;
}// 运算符重载
Date Date::operator(const int day) {_day  day;while (_day  GetMonthDay(_year, _month)) {_day - GetMonthDay(_year, _month);_month;if (_month  13) {_year;_month  1;}}return *this;
}// 运算符重载
Date Date::operator(const int day) {Date tmp(*this);tmp  day;return tmp;
}// -运算符重载
Date Date::operator-(int day) {_day - day;while (_day  0) {--_month;if (_month  0) {--_year;_month  12;}_day  GetMonthDay(_year, _month);}return *this;
}// -运算符重载
Date Date::operator-(int day) {Date tmp(*this);tmp - day;return tmp;
}// 计算两个日期之间的天数差
int Date::operator-(const Date d) const {Date BigDate  *this;Date SmallDate  d;if (SmallDate  BigDate) {BigDate  d;SmallDate  *this;}int count  0;while (SmallDate ! BigDate) {SmallDate;count;}return count;
}// 前置运算符重载
Date Date::operator() {*this  1;return *this;
}// 后置运算符重载
Date Date::operator(int) {Date tmp(*this);*this  1;return tmp;
}// --前置运算符重载
Date Date::operator--() {*this - 1;return *this;
}// --后置运算符重载
Date Date::operator--(int) {Date tmp(*this);*this - 1;return tmp;
}// 大于运算符重载
bool Date::operator(const Date d) const {if (_year  d._year || (_year  d._year  _month  d._month) || (_year  d._year  _month  d._month  _day  d._day)) {return true;}return false;
}// 等于运算符重载
bool Date::operator(const Date d) const {return _year  d._year  _month  d._month  _day  d._day;
}// 大于等于运算符重载
bool Date::operator  (const Date d) const {return (*this  d) || (*this  d);
}// 小于运算符重载
bool Date::operator  (const Date d) const {return !(*this  d);
}// 小于等于运算符重载
bool Date::operator  (const Date d) const {return !(*this  d);
}// 不等于运算符重载
bool Date::operator ! (const Date d) const {return !(*this  d);
}// 地址运算符重载
const Date* Date::operator  () const {return this;
}// 输出流运算符重载
ostream operator  (ostream out, const Date d) {out  d._year  /  d._month  /  d._day;return out;
}// 输入流运算符重载
istream operator  (istream in, Date d) {in  d._year;in  d._month;in  d._day;return in;
}上面的代码实现了日期类的基本功能包括构造函数、加减运算符重载、比较运算符重载、输入输出流运算符重载等。 下面是主函数用于测试代码功能 
int main() {// 创建日期对象并打印Date d1(2023, 11, 13);cout  日期1;d1.print();// 拷贝构造函数测试Date d2(d1);cout  日期2拷贝构造;d2.print();// 赋值运算符重载测试Date d3  d1;cout  日期3赋值运算符重载;d3.print();//  运算符重载测试d1  10;cout  日期1运算符重载后;d1.print();//  运算符重载测试Date d4  d2  5;cout  日期4运算符重载;d4.print();// - 运算符重载测试d2 - 3;cout  日期2-运算符重载后;d2.print();// - 运算符重载测试Date d5  d3 - 7;cout  日期5-运算符重载;d5.print();// - 运算符重载测试int diff  d5 - d4;cout  日期4和日期5之间的天数差  diff  endl;//  前置运算符重载测试d1;cout  日期1前置运算符重载后;d1.print();//  后置运算符重载测试Date d6  d2;cout  日期6后置运算符重载;d6.print();cout  日期2后置运算符重载后;d2.print();// -- 前置运算符重载测试--d3;cout  日期3--前置运算符重载后;d3.print();// -- 后置运算符重载测试Date d7  d4--;cout  日期7--后置运算符重载;d7.print();cout  日期4--后置运算符重载后;d4.print();// 大于运算符重载测试cout  日期5大于日期6吗  (d5  d6 ? 是 : 否)  endl;// 等于运算符重载测试cout  日期1等于日期2吗  (d1  d2 ? 是 : 否)  endl;// 不等于运算符重载测试cout  日期3不等于日期4吗  (d3 ! d4 ? 是 : 否)  endl;// 输出流运算符重载测试cout  日期1的输出流运算符重载  d1  endl;// 输入流运算符重载测试Date d8;cout  请输入一个日期年 月 日;cin  d8;cout  您输入的日期为  d8  endl;return 0;
}C实现一个简单的日期类包括日期的加减、大小比较等功能。日期类的实现可以帮助我们更方便地处理日期提高代码的可读性和可维护性。 
日期计算相关OJ题 
HJ73 计算日期到天数转换 
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId37tqId21296rp1ru/activity/ojqru/ta/huawei/question-ranking  
#include iostream
using namespace std;int main() {int month[13]  {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};int y, m, d;cin  y  m  d;int day  d;if (2  m  ((y % 4  0  y % 100 ! 0) || (y % 400  0))){day  1;}day  month[m];cout  day  endl;
}KY111 日期差值 
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId62tqId29468rp1ru/activity/ojqru/ta/sju-kaoyan/question-ranking  
#include iostream
#include string
using namespace std;// 判断是否为闰年
bool isLeapYear(int year) 
{return (year % 4  0  year % 100 ! 0) || (year % 400  0);
}// 获取某年某月的天数
int getDaysOfMonth(int year, int month) 
{int daysOfMonth[]  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month  2  isLeapYear(year)) {return 29;}return daysOfMonth[month];
}// 计算日期距离公元0年的天数
int getDaysFromZero(int year, int month, int day) 
{int days  0;for (int y  1; y  year; y) {days  isLeapYear(y) ? 366 : 365;}for (int m  1; m  month; m) {days  getDaysOfMonth(year, m);}days  day;return days;
}// 计算两个日期之间的天数差值
int getDaysDiff(const string date1, const string date2) 
{int year1, month1, day1, year2, month2, day2;sscanf(date1.c_str(), %4d%2d%2d, year1, month1, day1);sscanf(date2.c_str(), %4d%2d%2d, year2, month2, day2);int days1  getDaysFromZero(year1, month1, day1);int days2  getDaysFromZero(year2, month2, day2);return abs(days2 - days1)  1;
}int main() 
{string date1, date2;while (cin  date1  date2) {int daysDiff  getDaysDiff(date1, date2);cout  daysDiff  endl;}return 0;
}KY222 打印日期 
https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId69tqId29669rp1ru/activity/ojqru/ta/hust-kaoyan/question-ranking  
#include iostream
using namespace std;bool isLeapYear(int year)
{return (year % 4  0  year % 100 ! 0) || (year % 400  0);
}int getMonthDay(int year, int month)
{int daysOfMonth[]  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month  2  isLeapYear(year)) {return 29;}return daysOfMonth[month];
}int main() {int year, day;while (cin  year  day) { int month  1;while (day  getMonthDay(year, month)){day - getMonthDay(year, month);month;}printf(%4d-%02d-%02d\n,  year, month, day);}
}KY258 日期累加 
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId40tqId31013rp1ru/activity/ojqru/ta/kaoyan/question-ranking  
#include iostream
using namespace std;bool isLeapYear(int year)
{return (year % 4  0  year % 100 ! 0) || (year % 400  0);
}int getMonthDay(int year, int month)
{int daysOfMonth[]  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month  2  isLeapYear(year)) {return 29;}return daysOfMonth[month];
}
int main() {int t, year, month, day, add;cin  t;while (t--) { // 注意 while 处理多个 casecin  year  month  day  add;day  add;while (day  getMonthDay(year, month)){day - getMonthDay(year, month);month;if (13  month){month  1;year;}}printf(%4d-%02d-%02d\n,  year, month, day);}
}
 文章转载自: http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.blxlf.cn.gov.cn.blxlf.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn