C++实现Date日期类
定义一个Date类,包含三个属性年、月、日
实现了如下功能:
- 年月日的增加、减少:2017年10月1日加上100个月30天是2025年5月31日
- 输出某天是星期几:2017年10月1日是星期日
- 判断某一年是否是闰年:2020年是闰年
- 下一个工作日(周末)日期:2010年10月2日下一个周末是10月8日
class Date
{
public:
Date();
Date(int yy, Month mm, int dd);
int day() const { return m_day; }
int year() const { return m_year; }
Month month() const { return m_month; } void add_day(int dd);//增加或减少天数
void add_month(int mm);//增加或减少月份
void add_year(int yy);//增加或减少年份 private:
int m_year;
Month m_month;
int m_day;
};
//判断日期是否合法
bool is_date(int y, Month m, int d);
//判断是否为闰年
bool leapyear(int y);
//两个Date是否相等
bool operator==(const Date &a, const Date &b);
bool operator!=(const Date &a, const Date &b);
//Date输入输出
ostream &operator<<(ostream &os, const Date &d);
istream &operator>>(istream &is, Date &dd);
//今天是星期几
Day day_of_week(const Date &date);
ostream &operator<<(ostream &os, const Day &d);
//下一个周末
Date next_Sunday(const Date &d);
//下一个工作日
Date next_weekday(const Date &d);
具体的实现代码如下:
#include <iostream>
#include <vector>
#include <string> using std::istream;
using std::ostream;
using std::vector;
using std::ios_base;
using std::string; namespace Chrono
{
class InvalidDate
{
public:
std::string what() { return "InValid Date Occured"; };
}; enum class Month
{
jan = , //January
feb, //February
mar, //March
apr, //April
may, //May
jun, //June
jul, //July
aug, //August
sep, //September
oct, //October
nov, //November
dec //December
}; enum class Day
{
sun, //sunday
mon, //monday
tue, //tuesday
wed, //wednesday
thu, // thursday
fri, //friday
sat //saturday
}; class Date
{
public:
Date();
Date(int yy, Month mm, int dd);
int day() const { return m_day; }
int year() const { return m_year; }
Month month() const { return m_month; } void add_day(int dd);
void add_month(int mm);
void add_year(int yy); private:
int m_year;
Month m_month;
int m_day;
}; bool is_date(int y, Month m, int d);
bool leapyear(int y);
bool operator==(const Date &a, const Date &b);
bool operator!=(const Date &a, const Date &b);
ostream &operator<<(ostream &os, const Date &d);
istream &operator>>(istream &is, Date &dd);
Day day_of_week(const Date &date);
ostream &operator<<(ostream &os, const Day &d); Date next_Sunday(const Date &d);
Date next_weekday(const Date &d); ////////////////////////////////////////////////////////////////////////////////
//Implements /////
////////////////////////////////////////////////////////////////////////////////
Date::Date(int yy, Month mm, int dd) : m_year(yy), m_month(mm), m_day(dd)
{
if (!is_date(yy, mm, dd))
throw InvalidDate{};
} const Date &default_date()
{
static const Date d{, Month::jan, };
return d;
} Date::Date() : m_year(default_date().year()),
m_month(default_date().month()),
m_day(default_date().day()) {} void Date::add_day(int dd)
{
if (dd > )
{
for (; dd > ; --dd)
{
int temp_d = m_day + ;
switch (month())
{
case Month::feb:
if ((leapyear(m_year) && temp_d > ) || (!leapyear(m_year) && temp_d > ))
{
temp_d = ;
m_month = Month::mar;
}
break;
case Month::apr:
case Month::jun:
case Month::sep:
case Month::nov:
if (temp_d > )
{
temp_d = ;
m_month = Month((int)m_month + );
}
break;
case Month::dec:
if (temp_d > )
{
temp_d = ;
m_month = Month::jan;
m_year += ;
}
break;
default:
if (temp_d > )
{
temp_d = ;
m_month = Month((int)m_month + );
}
break;
}
m_day = temp_d;
}
}
else if (dd < )
{
for (; dd < ; ++dd)
{
int temp_d = day() - ;
if (temp_d <= )
{
switch (month())
{
case Month::jan:
m_month = Month::dec;
temp_d = ;
m_year -= ;
break;
case Month::mar:
m_month = Month::feb;
if (leapyear(m_year))
temp_d = ;
else
temp_d = ;
break;
case Month::feb:
case Month::apr:
case Month::jun:
case Month::oct:
case Month::sep:
case Month::nov:
temp_d = ;
m_month = Month((int)m_month - );
break;
default:
temp_d = ;
m_month = Month((int)m_month - );
break;
}
}
m_day = temp_d;
}
}
} void Date::add_month(int month)
{
int temp_y = month / + m_year;
int temp_m = month % + (int)m_day; if (temp_y <= )
{
temp_y--;
temp_m = temp_m + ;
}
else if (temp_m > )
{
temp_y++;
temp_m = temp_m - ;
}
m_year = temp_y;
m_month = Month(temp_m);
switch (m_month)
{
case Month::feb:
if (leapyear(m_year) && m_day > )
m_day = ;
else if (!leapyear(m_year) && m_day > )
m_day = ;
break;
case Month::apr:
case Month::jun:
case Month::sep:
case Month::nov:
if (m_day > )
m_day = ;
default:
break;
}
} void Date::add_year(int n)
{
if (month() == Month::feb && day() == && !leapyear(m_year + n))
m_day = ;
m_year += n;
} bool is_date(int y, Month m, int d)
{
if (d <= )
return false;
if (m < Month::jan || m > Month::dec)
return false;
int days_in_month;
switch (m)
{
case Month::feb:
days_in_month = leapyear(y) ? : ;
break;
case Month::apr:
case Month::jun:
case Month::sep:
case Month::nov:
days_in_month = ;
break;
default:
days_in_month = ;
break;
}
if (days_in_month < d)
return false;
return true;
} // https://en.wikipedia.org/wiki/Leap_year#Algorithm
bool leapyear(int y)
{
if (y % != )
return false;
else if (y % != )
return true;
else if (y % != )
return false;
return true;
}
bool operator==(const Date &a, const Date &b)
{
return a.year() == b.year() && a.month() == b.month() && a.day() == b.day();
} bool operator!=(const Date &a, const Date &b)
{
return !(a == b);
} ostream &operator<<(ostream &os, const Date &d)
{
return os << '(' << d.year()
<< ',' << (int)d.month()
<< ',' << d.day() << ')';
} ostream &operator<<(ostream &os, const Day &d)
{
vector<string> weekdays{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
os << weekdays[(int)d];
return os;
} // format (2017,5,23)
istream &operator>>(istream &is, Date &d)
{
int yy, mm, dd;
char ch1, ch2, ch3, ch4;
is >> ch1 >> yy >> ch2 >> mm >> ch3 >> dd >> ch4;
if (!is)
return is;
if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')')
is.clear(ios_base::failbit);
d = Date{yy, Month(mm), dd};
return is;
} // https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
Day day_of_week(const Date &date)
{
int y = date.year();
int m = (int)date.month();
int d = date.day();
y -= m < ;
int day_of_week = (y + y / - y / + y / + "-bed=pen+mad."[m] + d) % ;
return Day(day_of_week);
}
Date next_Sunday(const Date &d)
{
Date d1 = d;
while (day_of_week(d1) != Day::sun)
{
d1.add_day();
}
return d1;
} Date next_weekday(const Date &d)
{
Date d1 = d;
if (day_of_week(d1) == Day::sat)
d1.add_day();
else if (day_of_week(d1) == Day::fri)
d1.add_day();
else
d1.add_day();
return d1;
}
}
代码实现
C++实现Date日期类的更多相关文章
- javascript Date日期类
四.Date日期类 迁移时间:2017年5月27日18:43:02 Author:Marydon (一)对日期进行格式化(日期转字符串) 自定义Date日期类的format()格式化方法 方式一: ...
- 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类
Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...
- Date日期类,Canlendar日历类,Math类,Random随机数学类
Date日期类,SimpleDateFormat日期格式类 Date 表示特定的时间,精确到毫秒 常用方法 getTime() setTime() before() after() compareT ...
- java Date日期类和SimpleDateFormat日期类格式
~Date表示特定的时间,精确到毫秒~构造方法:public Date()//构造Date对象并初始化为当前系统的时间public Date(long date) //1970-1-1 0:0:0到指 ...
- Date日期类 Calendar日历类 完成可视化日历
package com.test; import java.text.DateFormat; import java.text.ParseException; import java.text.Sim ...
- 工具类 util.Date 日期类
/** * @description format the time * @author xf.radish * @param {String} format The format your want ...
- java.util.Date日期类通过java语句转换成Sql(这里测试用的是oracle)语句可直接插入(如:insert into)的日期类型
public void add(Emp emp) throws Exception{ QueryRunner runner = new QueryRunner(JdbcUtil.getDataSour ...
- 日期类 Date
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...
随机推荐
- VNCServer,SSH Secure Shell Client,window远程控制linux
1.VNC远程连接linux图形化桌面 2.SSH Secure Shell Client连接linux终端 3.设置FTP与linux传输文件 1.VNC远程连接linux图形化桌面 在centos ...
- java 集合框架(十)List
一.概述 List是一种有序集合,有时也被称为序列,可以有重复的元素.List集合相比Collection,除了直接继承的方法外,有以下拓展的操作方法 位置访问---可以基于元素索引来操作元素,比如g ...
- 兼容ie7以上的 placeholder属性
最近项目踩过的坑,不考虑ie的可以拐弯绕路走了. css3的新属性 占位符 placeholder用着多舒服 . 偏偏万恶的ie不支持,网上有几种方法是用焦点事件代替的,不过会失去原有的特性.一旦获取 ...
- GM8180启动过程调试
1. burnin下的boot.s 0: boot start 1 ; 1: Init SMC configuration OK ; 2: Ini ...
- 笔记︱信用风险模型(申请评分、行为评分)与数据准备(违约期限、WOE转化)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 巴塞尔协议定义了金融风险类型:市场风险.作业风 ...
- Linux显示按文件大小降序排列
Linux显示按文件大小降序排列 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ls -ls 总用量 56 12 -rw-r--r-- 1 youhaidon ...
- Java语法 示例
第二章: int:整型 double:双精度浮点型 char:字符型 String:字符串型语法:数据类型 变量名: 如:String name: 变量名=值: 如:name="张三&quo ...
- var dataObj=eval("("+data+")");//转换为json对象(解决在ajax返回json格式数据的时候明明正确的获取了返回值但是却就是进不去success方法的问题。格式错误)
一,原理 1.1,解析1 将字符串解析为JavaScript代码,比如:var a = "alert('a');";这里的a就只是一个字符串而已,输出的话也是alert(a);这句 ...
- C#图解教程 第二十二章 异常
异常 什么是异常try语句 处理异常 异常类catch 子句使用特定catch子句的示例catch子句段finally块为异常寻找处理程序更进一步搜索 一般法则搜索调用栈的示例 抛出异常不带异常对象的 ...
- jQuery UI Autocomplete Combobox 配 ASP.NET DropDownList
0.引言 1.起因 一开始使用Autocomplete做了一个自动补全的文本框,如上图.后来因业务需要希望能在这个文本框的边上做个下拉列表按钮,一按就展开所有支持 ...