C++实现日期类(Date类)
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1) //构造
:_year(year)
, _month(month)
, _day(day)
{
if (!isInvalidDate(_year, _month, _day))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
Date operator+(int count)
{
Date tmp(*this);
tmp._day += count;
ToCorrect(tmp);
return tmp;
}
Date operator-(int count)
{
Date tmp(*this);
tmp._day -= count;
ToCorrect(tmp);
return tmp;
}
Date& operator++() //前置++
{
(*this)++;
return *this;
}
Date operator++(int) //后置++
{
Date tmp(*this);
(*this)+=1;
return tmp;
}
Date& operator--()
{
(*this)--;
return *this;
}
Date operator--(int)
{
Date tmp(*this);
(*this)--;
return tmp;
}
int operator-(const Date &d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this<d)
{
max = d;
min = *this;
flag = -1;
}
int count=0;
while (min != max)
{
++min;
count++;
}
return count*flag;
}
Date& operator+=(int day)
{
*this = *this + day;
return *this;
}
bool operator!=(const Date &d)
{
return !(*this == d);
}
bool operator<(const Date &d)
{
return !((*this>d)||(*this==d));
}
bool operator>=(const Date &d)
{
return !(*this<d);
}
bool operator>(const Date &d)
{
return (_year > d._year
|| (_year == d._year && _month > d._month)
|| (_year == d._year && _month == d._month && _day > d._day));
}
bool operator==(const Date &d)
{
return ((_year == d._year) && (_month == d._month) && (_day == d._day));
}
public:
bool IsLeapYear(int year)
{
if(year % 400 || (year % 4 && year % 100))
return true;
return false;
}
int YearsOfMonth(int year, int month)
{
int day;
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
day = days[month];
if (month == 2)
day += IsLeapYear(year);
return day;
}
Date ToCorrect(Date &d)
{
if (d._day>YearsOfMonth(d._year, d._month))
{
while (d._day > YearsOfMonth(d._year, d._month))
{
d._day -= YearsOfMonth(d._year,d._month);
if (d._month == 12)
{
d._year++;
d._month = 1;
}
else
{
++d._month;
}
}
}
else
{
while (d._day <= 0)
{
if (d._month == 1)
{
d._year--;
d._month = 12;
}
else
{
--d._month;
}
d._day += YearsOfMonth(d._year, d._month);
}
}
return d;
}
bool isInvalidDate(int year, int month, int day)
{
if ((year < 1) || (month<0 || month>12) || (day<0 || day>YearsOfMonth(year,month)))
return false;
return true;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
friend istream& operator>>(istream& is, Date &d);
friend ostream& operator<<(ostream& os, const Date &d);
private:
int _year;
int _month;
int _day;
};
istream& operator>>(istream& is, Date& d)
{
cout << "请输入一个日期" << endl;
is >> d._year >> d._month >> d._day;
return is;
}
ostream& operator<<(ostream& os, const Date &d)
{
cout << d._year << "-" <<d. _month << "-" << d._day << endl;
return os;
}
int main()
{
/*Date d1(2016,8,18);
//d1.Display();
//d1 = d1++;
cout << d1 << endl;*/
//Date d1(2015, 12, 3);
//(d1++).Display(); //d1.operator++(&d1, 0);
//(++d1).Display(); //d1.operator++(&d1);
Date d1(2015, 12, 3);
Date d2(2015, 11, 1);
cout << (d1 - d2) << endl;
//Date d1(2015, 12, 3);
//Date ret = d1 + 40; //operator+
//ret.Display();
/*Date d1(2015, 12, 3);
Date ret = d1 + 40;
d1 = ret;
ret = d1 - 40;
ret.Display();*/
/*Date ret;
Date d2(2015, 1, 1);
ret = d2 - 1;
ret.Display();*/
return 0;
}
C++实现日期类(Date类)的更多相关文章
- 日期操作类--Date类
Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...
- Java关于时间日期的Date类和Calendar类概述
1. System.currentTimeMillis()方法 可以获取当前时间距离1970年01月01日00时00分00秒的秒数,如果程序运行在北京时区,则获取的数据是当前时间距离1970 ...
- java日期时间Date类
java.util包提供了Date类来封装当前的日期和时间. Date类提供两个构造函数来实例化Date对象. 第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函数接收一个参 ...
- java常用类————Date类
Date类在Java.util包中. 一.功能介绍:创建Date对象,获取时间,格式化输出的时间. 二.对象创建:1.使用Date类无参数的构造方法创建的对象可以获取本地时间.例如: Date now ...
- Java学习关于时间操作的应用类--Date类、Calendar类及其子类
Date类 Date类封装了当期时间和日期.与Java1.0定义的原始版的Date类相比,Date类发生了本质的变化.在Java1.1发布时,原始版Date类定义的许多功能被移进Calendar类和D ...
- Java api 入门教程 之 JAVA的Date类与Calendar类
在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理. 一.这里简单介绍一下Date类的使 ...
- JAVA的Date类与Calendar类【转】
Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...
- Java的Date类与Calendar类
一:Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Da ...
- Date 类 02
Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...
- Java知多少(77)日期和时间类
Java 的日期和时间类位于 java.util 包中.利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间. Date 类 Date 类是 Java 中的日期时间类 ...
随机推荐
- 由浅入深了解Thrift之客户端连接池化
一.问题描述 在上一篇<由浅入深了解Thrift之服务模型和序列化机制>文章中,我们已经了解了thrift的基本架构和网络服务模型的优缺点.如今的互联网圈中,RPC服务化的思想如火如荼.我 ...
- SGU 113
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
- POJ 3335 Rotating Scoreboard(半平面交求多边形核)
题目链接 题意 : 给你一个多边形,问你在多边形内部是否存在这样的点,使得这个点能够看到任何在多边形边界上的点. 思路 : 半平面交求多边形内核. 半平面交资料 关于求多边形内核的算法 什么是多边形的 ...
- 转载——有感于三个50岁的美国程序员的生活状态与IT职业杂想
明天就是国庆节了,今天也不想干活干的太累了!写一篇以前去美国出差的杂想,对比于美国50多岁的程序员和大多数50多岁国内父母的生活状态有感而发. 前几年正好有一个项目的机会出差去了一次美国,地点是美国中 ...
- redhat6修改主机名
1.临时修改主机名 sudo hostname lyhost 2.永久修改主机名 vim /etc/sysconfig/network 修改里面的hostname字段即可,重启后生效.
- 540A: Combination Lock
题目链接:http://codeforces.com/problemset/problem/540/A 题意: 输入的两个长度一样的数,求对应位置的某位数到下一个数需要最小的步长,每次只能先前或先后走 ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-004JPA例子的代码
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...
- Centos环境下部署游戏服务器-SSH
在这个*nix大行其道的年代,SSH绝对值得你拥有,虽说它的语法真的很简单,可用途相当大.比如说你在登山,突然公司一个电话告诉你服务器出问题了,在登山的过程中,肯定不能背个几公斤的笔记本吧,这个时候只 ...
- Qt之界面出现、消失动画效果(简单好用)
在学习Qt的这2.3个月里,对Qt越发感兴趣,从刚开始的盲目.无所适从到现在的学习.研究.熟练.掌握的过程中,我学到了很多东西,也学会了如何通过自学让自己更加成熟.强大起来,如何更有效地提高自己学习. ...
- Mysql笔记——DDL
数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言.一个数据库模式包含该数据库中所有实体的描述定义. =========== ...