实现 日期 的运算---C++

简介:

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

#include<assert.h>

using namespace std;

class Date

{

public:

void display()

{

cout << _year << "-" << _month << "-" << _day << endl;

}

bool IsLeapyear(int year)

{

return ((year / 400 == 0) || (year / 4 == 0 && year / 100 != 0));

}

int GetMonthDay(int year, int month)

{

if (month > 12 || month < 1)

{

printf("无此月\n");

return -1;

}

else

{

int dayarr[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if (IsLeapyear(year) && month == 2)

{

return 29;

}

else

{

return dayarr[month - 1];

}

}

}

Date& operator+(const int day)

{

if (day < 0)

{

return *this - (-day);

}

else

{

_day += day;

while (_day > GetMonthDay(_year, _month))

{

_day -= GetMonthDay(_year, _month);

if (_month == 12)

{

_year += 1;

_month = 1;

}

else

{

_month += 1;

}

}

}

return *this;

}

Date& operator-(int day)

{

if (day < 0)

{

return *this + (-day);

}

else

{

_day -= day;

while (_day <= 0)//注意   等于    的时候

{

if (_month == 1)

{

_year -= 1;

_month = 12;

}

else

{

_month -= 1;

}

_day += GetMonthDay(_year, _month);

}

}

return *this;

}

Date operator++(int day)//后置加加

{

Date tem = *this;

*this = *this + 1;

return tem;

}

Date& operator++()//前置加加

{

*this = *this + 1;

return *this;

}

Date& operator--(int day)//后置减减

{

Date tem = *this;

*this = *this - 1;

return tem;

}

Date& operator--()//前置减减

{

*this = *this - 1;

return *this;

}

Date& operator+=(const int day)

{

*this = *this + day;

return *this;

}

Date& operator-=(const int day)

{

*this = *this - day;

return *this;

}

bool operator>(const Date& cur)

{

if (_year > cur._year)

{

return 1;

}

else if (_year == cur._year)

{

if (_month > cur._month)

{

return 1;

}

else if (_month == cur._month)

{

if (_day > cur._day)

{

return 1;

}

}

}

return 0;

}

bool operator==(const Date& cur)

{

if (_year == cur._year

&& _month == cur._month

&& _day == cur._day)

{

return 1;

}

else

{

return 0;

}

}

bool operator<(const Date& cur)

{

if (!(*this > cur || *this == cur))

{

return 1;

}

else

{

return 0;

}

}

bool operator>=(const Date& cur)

{

if (*this > cur || *this == cur)

{

return 1;

}

else

{

return 0;

}

}

bool operator<=(const Date& cur)

{

if (*this < cur || *this == cur)

{

return 1;

}

else

{

return 0;

}

}

int operator-(const Date& cur)

{

int count = 0;

Date max;

Date min;

if (*this > cur)

{

max = *this;

min = cur;

}

else if (*this < cur)

{

max = cur;

min = *this;

}

else

{

return 0;

}

while (1)

{

if (min == max)

{

return count;

}

else

{

min = min + 1;

count++;

}

}

}

public:

Date(int year = 1990, int month = 1, int day = 1)

:_year(year)

, _month(month)

, _day(day)

{}

Date(const Date& a)

:_year(a._year)

,_month(a._month)

, _day(a._day)

{}

~Date()

{}

Date& operator=(const Date& cur)

{

_year = cur._year;

_month = cur._month;

_day = cur._day;

return *this;

}

private:

int _year;

int _month;

int _day;

};

//operator+ operator-

//operator++ operator--

//operator+= operator-=

void test1()

{

Date cur(2015, 11, 11);

//(cur + 18).display();

//(cur + 19).display();

//(cur + 20).display();

//(cur + (-30)).display();

//(cur - 10).display();

//(cur - 11).display();

//(cur - 12).display();

//(cur - (-18)).display();

}

//operator>  <  ==  >=  <=

void test2()

{

Date cur1(2015, 11, 13);

Date cur2(2015, 11, 12);

cout << (cur1 > cur2) << endl;

cout << (cur1 == cur2) << endl;

}

//operator-(const Date& cur)

void test3()

{

Date date1(2015, 11, 11);

Date date2(2015, 11, 1);

Date date3(2015, 11, 22);

cout << date1 - date2 << endl;

cout << date1 - date3 << endl;

}

int main()

{

//test1();

//test2();

test3();

system("pause");

return 0;

}










本文转自 ye小灰灰  51CTO博客,原文链接:http://blog.51cto.com/10704527/1719279,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
编译器 C++
C++初阶--类与对象--const成员和日期类的实现
C++初阶--类与对象--const成员和日期类的实现
|
1月前
|
存储 C++
【C++初阶】第三站:类和对象(中) -- 日期计算器
【C++初阶】第三站:类和对象(中) -- 日期计算器
|
1月前
|
存储 C语言 C++
c++日期和时间
c++日期和时间
19 0
|
28天前
|
算法 C语言 C++
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)(一)
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)
112 1
|
28天前
|
缓存 算法 安全
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)(三)
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)
36 1
|
28天前
|
安全 调度 C++
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)(二)
C++ std::chrono库使用指南 (实现C++ 获取日期,时间戳,计时等功能)
54 0
|
1月前
|
编译器 C++
『C++成长记』日期类的实现
『C++成长记』日期类的实现
|
1月前
|
C++
C++运算符号重载详解
C++运算符号重载详解
|
2月前
|
C++
【c++】日期类的实现-Class Date
【c++】日期类的实现-Class Date
【c++】日期类的实现-Class Date
|
2月前
|
存储 人机交互 C++
C++实现简易的集合运算
C++实现简易的集合运算