日期类——日期计算器
来源:互联网 发布:淘宝买轮毂 编辑:程序博客网 时间:2024/06/05 11:04
"date.h"
<strong><span style="font-size:18px;">#pragma onceclass Date{public:Date(int year = 1990,int month = 1,int day = 1):_year(year),_month(month),_day(day){if (IsInvalidData() == false){_year = 1990;_month = 1;_day = 1;}}Date(const Date& d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}Date& operator=(const Date& d){if (this != &d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}Date& operator+(const int day){_day += day;while (_day > DayOfMonth()){_month++;if (_month > 12){_year++;_month = 1;}_day = _day - DayOfMonth();}return *this;}Date& operator-(const int day){_day -= day;while(_day < 1){_month--;if (_month < 1){_year--;_month = 12;}_day = DayOfMonth() - (-_day);}return *this;}Date& operator++()//前置++{_day++;if (_day > DayOfMonth()){_month++;if (_month > 12){_year++;_month = 1;}_day = 1;}return *this;}Date operator++(int)//后置++{Date tmp = *this;_day++;if (_day > DayOfMonth()){_month++;if (_month > 12){_year++;_month = 1;}_day = 1;}return tmp;}Date& operator--()//前置--{_day--;if (_day < 1){_month--;if (_month < 1){_year--;_month = 12;}_day = DayOfMonth();}return *this;}Date operator--(int)//后置--{Date tmp = *this;_day--;if (_day < 1){_month--;if (_month < 1){_year--;_month = 12;}_day = DayOfMonth();}return tmp;}int operator-(const Date& d){if (this > &d){Date tmp = d;int count = 0;while (tmp != *this){++tmp;++count;}return count;}else{Date tmp = *this;int count = 0;while(tmp != d){++tmp;count++;}return count;}}bool operator==(const Date& d){return (_year==d._year) && (_month == d._month) && (_day == d._day);}bool operator!=(const Date& d){return !(operator==(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 !(operator>(d));}void Display(){cout<<this->_year<<"_"<<this->_month<<"_"<<this->_day<<endl;}protected:bool IsInvalidData(){if (_year < 1990 || _month < 1 || _month > 12 || _day < 1 || _day > DayOfMonth()){return false;} else{return true;}}int DayOfMonth(){int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};if (IsLeepYear(_year) == true){arr[2] = 29;}return arr[_month];}bool IsLeepYear(int year){if (((year % 4 == 0)&&(year % 100 == 0)) || (year % 400 == 0)){return true;}else{return false;}}private:int _year;int _month;int _day;};</span></strong>
"test.cpp"
<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>using namespace std;#include "date.h"void Test1()//operator= operator+{Date d(2016,7,8);d.Display();Date d1;d1.Display();d1 = d;//operator=d1.Display();//d = d + 10; //operator+//d.Display();d = d + 100; //operator+d.Display();}void Test2()//operator- operator++{Date d(2016,7,8);d.Display();//d = d-10; // operator-//d.Display();d = d-100; // operator-d.Display();++d;//operator++d.Display();++d;//operator++d.Display();Date d1(2012,12,31);d1.Display();++d1;//operator++d1.Display();}void Test3()//operator--{Date d(2016,7,11);d.Display();--d;//operator--d.Display();Date d1(2000,1,1);d1.Display();--d1;d1.Display();}void Test4()//operator== operator!= operator> operator<{Date d(2001,1,1);Date d1(2001,2,1);Date d2(2001,1,1);cout<<(d==d2)<<endl;//operator==cout<<(d==d1)<<endl;//operator==cout<<(d!=d2)<<endl;//operator!=cout<<(d!=d1)<<endl;//operator!=cout<<(d>d1)<<endl;//operator>cout<<(d1>d2)<<endl;//operator>cout<<(d<d1)<<endl;//operator<cout<<(d1<d2)<<endl;//operator<}void Test5()//operator-{Date d(2016,7,11);Date d1(2014,8,18);int ret = d-d1;cout<<ret<<endl;int tmp = d1-d;cout<<tmp<<endl;}int main(){//Test1();//Test2();//Test3();//Test4();Test5();system("pause");return 0;}</span></strong>
0 0
- 日期类——日期计算器
- 日期类——日期计算器
- 日期类->日期计算器
- 【C++】日期类+日期万年历+日期计算器
- C/C++ — 实现日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 实现日期类(日期计算器)
- MFC计算器项目——日期计算模块
- 利用C++日期类实现简单的日期计算器
- 类实现一个简单的日期计算器
- 日期计算器的实现
- C++ 日期计算器
- 11.ThinkPHP 3.1.2 连贯操作
- js中this的应用场景
- iOS 获取本地的坐标
- 阿里weex研究iOS(一)playground内容空白问题
- android 资源文件命名规则 drawable mipmap一样的
- 日期类——日期计算器
- 暑期第一弹<搜索> D - Fliptile(黑白棋翻转经典题型 DFS)
- samba配置记录_Ubuntu+Win7+Samba实现文件共享
- Intent介绍及Intent在Activity中的使用方法
- 在亚信实习的日子
- 替换xml/html中的属性值 参考的
- jQuery中attr和prop方法的区别
- iOS多线程--下(GCD)
- Hive集群安装配置