简单日期类操作(c++)
来源:互联网 发布:大数据可以研究什么 编辑:程序博客网 时间:2024/06/06 15:38
简单的日期类操作,如一个日期加上指定天数后的日期,日期减去日期有多少天,求指定的一天是星期几,和一些有关类的函数。
头文件:
#ifndef _DATE_H__#define _DATE_H__#include<iostream>using namespace std;class Date{public: Date(int year = 1900, int month = 1, int day = 1); Date(const Date& d); ~Date(); Date& operator=(Date& d);public: bool operator==(Date& d); bool operator!= (Date& d); bool operator>(Date& d); bool operator>=(Date& d); bool operator<(Date& d); bool operator<=(Date& d); ostream& operator<<(ostream&os, Date&d);public: Date& operator++(); Date operator++(int); Date& operator--(); Date operator--(int); //日期+天数 Date operator+(int day); //为加上指定天数的日期 Date& operator+=(int day); //日期-天数 Date operator-(int day); Date& operator-=(int day); //日期-日期 int operator-(const Date& d); //给定一个日期求这一天是星期几 int IsWeekDay(Date&d);public: bool IsLeapYear(int year); int GetMonthDay(int year, int month); void display();private: int _year; int _month; int _day;};#endif//_DATE_H__
实现函数
//构造函数Date::Date(int year, int month, int day){ if (year > 0 && month > 0 && month<13 && day>0 && day <= GetMonthDay(year,month)) { _year = year; _month = month; _day = day; } else { cout << "输入错误" << endl; exit(EXIT_FAILURE); }}//拷贝构造函数Date::Date(const Date& d){ _year = d._year; _month = d._month; _day = d._day;}//析构函数Date::~Date(){ //这里的析构函数不用做任何操作}//赋值操作运算符重载Date& Date::operator=(Date& d){ if (this != &d) { _year = d._year; _month = d._month; _day = d._day; } return* this;}//操作运算符的重载bool Date::operator==(Date& d){ if ((_year == d._year) && (_month == d._month) && (_day == d._day)) { return true; } return false;}bool Date::operator!= (Date& d){ return (!(operator==(d)));}bool Date:: operator>(Date& d){ if (_year > d._year) { return true; } else if (_year == d._year) { if (_month > d._month) { return true; } else if (_month == d._month) { if (_day > d._day) { return true; } } } return false;}bool Date:: operator>=(Date& d){ return(operator>(d) || operator==(d));}bool Date:: operator<(Date& d){ return(!operator>=(d));}bool Date:: operator<=(Date& d){ return(operator<(d) || operator==(d));}//判断是否为闰年bool Date::IsLeapYear(int year){ return(((0 == (year % 4)) && (0 != year % 100))||(0 == year%400));}//判断指定月有多少天int Date::GetMonthDay(int year, int month){ //一年每个月的天数 int MonthArry[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int MonthDays = MonthArry[month]; //如果为闰年,且为2月,则为29天 if (IsLeapYear(year) && (month == 2)) { MonthDays = 29; } return MonthDays;}//日期前置++Date& Date::operator++(){ //求这一年的这个月有多少天 int ret = GetMonthDay(_year,_month); //判断这一天是不是这个月最后一天,如果不是直接++ if (_day < ret) { _day++; } //如果是最后一天,需继续判断 else if (_day == ret) { //判断是不是这一年的最后一个月 if (12 == _month) { _day = 1; _month = 1; _year++; } else { _day = 1; _month++; } } return *this;}//日期后置++Date Date::operator++(int){ //求这一年的这个月有多少天 int ret = GetMonthDay(_year, _month); Date tmp(*this); //判断这一天是不是这个月最后一天,如果不是直接++ if (_day < ret) { _day++; } //如果是最后一天,需继续判断 else if (_day == ret) { //判断是不是这一年的最后一个月 if (12 == _month) { _day = 1; _month = 1; _year++; } else { _day = 1; _month++; } } return tmp;}//日期前置--Date& Date:: operator--(){ //判断这一天是不是为一个月月初 if (1 == _day) { //判断这个月是否为这一年的最后一个月 if (1 == _month) { _day = 31; _month = 12; _year--; } else { _day = GetMonthDay(_year, _month - 1); _month--; _year; } } else { _day--; _month; _year; } return *this;}//日期后置--Date Date::operator--(int){ Date tmp(*this); //判断这一天是不是为一个月月初 if (1 == _day) { //判断这个月是否为这一年的最后一个月 if (1 == _month) { _day = 31; _month = 12; _year--; } else { _day = GetMonthDay(_year, _month - 1); _month--; _year; } } else { _day--; _month; _year; } return tmp;}//日期加天数Date Date:: operator+(int day){ Date tmp(*this); int SumDay = tmp._day + day; while (true) { if (SumDay <= GetMonthDay(tmp._year, tmp._month)) { tmp._day = SumDay; return tmp; } else { SumDay -= GetMonthDay(tmp._year, tmp._month); tmp._month++; if (tmp._month > 12) { tmp._year++; tmp._month %= 12; } } }}//把该日期改为加上指定数目的天数后的日期Date&Date:: operator+=(int day){ *this = operator+(day); return* this;}//日期减去指定数目的天数Date Date::operator-(int day){ Date tmp(*this); int SumDays = tmp._day + day; while (true) { if (day < tmp._day) { tmp._day -= day; return tmp; } else { day -= tmp._day; if (1 == tmp._month) { tmp._month = 12; tmp._year--; } else { tmp._month--; } tmp._day = GetMonthDay(tmp._year, tmp._month); } }}//把该日期改为减去指定数目天数的日期Date&Date::operator-=(int day){ *this = operator-(day); return* this;}//日期-日期int Date::operator-(const Date& d){ //异常情况 if (_year < d._year) { return -1; } if ((_year == d._year) && (_month < d._month)) { return -1; } if ((_year == d._year) && (_month == d._month) && (_day < d._day)) { return -1; } //正常情况 Date tmp_d(d); Date tmp_d_day(*this); int count = 0; while ((tmp_d != tmp_d_day)) { tmp_d.operator++();; count++; } return count;}//判断这一天是周几int Date::IsWeekDay(Date&d){ Date tmp(1900, 1, 1); int days = d.operator-(tmp); int weekday = days % 7+1; return weekday;}//打印函数void Date::display(){ cout << _year << "-" << _month << "-" << _day << endl;}
测试函数
#define _CRT_SECURE_NO_WARNINGS 1#include"date.h"void test1(){ Date d1(2017,7,17); /*int day=d1.IsWeekDay(d1); cout << day << endl;*/ Date d2(2017,1,3); //Date d3(2016,2,9); //int ret = d1 - d3; //cout << ret << endl; //Date d4; //d4 = d3; //Date d4 = d3-d1; //Date d4 = d3++; //d1.display(); //d2.display(); //d4.display(); //d4.display(); if (d2 <= d1) { cout << "d4 == d3" << endl; } //if (d2 == d3) //{ // cout << "d2 == d3" << endl; //} //if (d2 != d3) //{ // cout << "d2 != d3" << endl; //} //if (d1 >= d3) //{ // cout << "d1>=d3" << endl; //} //if (d3 < d1) //{ // cout << "d3 < d1" << endl; //}}int main(){ test1(); system("pause"); return 0;}
阅读全文
0 0
- 简单日期类操作(c++)
- C#.NET 日期操作类
- 一个简单的日期操作类
- 日期时间简单操作
- 日期简单操作
- java中简单操作日期
- 简单日期类(文件)
- C#-日期时间-时间日期操作
- objective-C日期相关操作
- 简单的日志文件操作类 (C#)
- js操作日期函数的一系列简单问题(日期加一个月)
- 简单的几个文件操作(C)
- IO(C#) 读写文件简单操作
- C++Vector简单操作
- c#MQ简单操作
- 【C#-Hashtable简单操作】
- c简单位操作
- 【C++】日期类+日期万年历+日期计算器
- Linux下串口编程总结
- vue中slot插口的用法
- 选择排序改进(二元选择排序)
- 1003. 我要通过!(20)
- java实现Excel将数据传进数据库中
- 简单日期类操作(c++)
- 互联网金融违约和个人信用的个人理解
- Hdu 1229 还是A+B
- Android中使用HelloChart开源库图表来实现折线图
- 【使用cnn降低假正率】Pulmonary Nodule Detection in CT Images笔记
- 正则匹配手机号中段
- c++动态内存管理
- windows下线程同步
- MySQL:ERROR 1820 (HY000)