修改后的日期类
来源:互联网 发布:美股行情软件下载 编辑:程序博客网 时间:2024/06/06 11:23
之前的一篇没有考虑如果超过一个月天数的情况,现做以调整:
//完善日期类,需要辅助操作可自行添加。#include<iostream>using namespace std;class Date{public: Date(int year = 2017, int month = 9, int day = 10) : _year(year) , _month(month) , _day(day) {} Date& operator=(const Date& d) //赋值运算符重载 { if (this != &d) { _year = d._year; _month = d._month; _day = d._day; } return *this; } //获得某月的天数 int Getmonthday(int _year, int _month) { //闰年 if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0)) { if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12) { return 31; } else if (_month == 2) { return 29; } else { return 30; } } else//平年 { if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 ) { return 31; } else if (_month == 2) { return 28; } else { return 30; } } } //判断日期是否合法 bool IsInvalid(int year, int month, int day)//判断日期是否合法 { //此处从1900年开始,如果小于这个日期,则返回fasle if ((year > 1900) && (month > 0) && (month<13) && (day>0) && (day < Getmonthday(year, month))) { return true; } else return false; } // 前置++ Date& operator++() { _day += 1; return *this; } // 后置++ Date operator++(int) { Date temp(*this); //保留当前对象 _day += 1; return temp; } Date& operator--() { _day -= 1; return *this; } Date operator--(int) { Date temp(*this); _day -= 1; return temp; } //days天之后的日期 Date operator+(int days) { if (days < 0)//天数若为负数变为减 { return *this - (0 - days); } Date temp(*this); temp._day += days; int daysinmonth = 0; while (temp._day > (daysinmonth = Getmonthday(temp._year, temp._month))) { temp._day -= daysinmonth; temp._month++; if (temp._year > 12) { temp._year += 1; temp._month = 1; } } return temp; } // days天之前的日期 Date operator-(int days) { if (days < 0) { return *this + (0 - days); } Date temp(*this); temp._day -= days; while (temp._day <= 0) { temp._month -= 1; if (temp._month <1) { temp._year -= 1; temp._month += 1; } temp._day += Getmonthday(temp._year, temp._month); } return temp; } // 两个日期之间的距离 int operator-(const Date& d) { Date maxDate = *this; Date minDate = d; if (maxDate < minDate) { maxDate = d; minDate = *this; } int days = 0; while (minDate!= maxDate) { ++minDate; days++; } return days; } /*while (1) { if (minDate + days == maxDate) break; days++; } return days;*/ bool operator==(const Date& d) { if ((_year == d._year) && (_month == d._month) && (_day == d._day)) { return true; } else return false; } bool operator!=(const Date& d) { if ((_year != d._year) || (_month != d._month) || (_day != d._day)) return true; else return false; } bool operator>(const Date& d) { if ((d._year < _year) || (d._month < _month) || (d._day < _day)) { return true; } else return false; } bool operator<(const Date& d) { if ((d._year > _year) || (d._month > _month) || (d._day > _day)) { return true; } else return false; } void display() { cout << _year << "-"<<_month << "-"<<_day << endl; } ~Date() {}private: int _year; int _month; int _day;}; int main(){ Date c1; c1.display(); Date c2(2017, 9, 11); c2.display(); Date c3; c3 = c2; c3.display(); c3 = c1++; c3.display(); c3 = ++c1; c3.display(); cout << "对日期减天数" << endl; cout<< (c3-c2)<<endl; Date c4 = c1-100; c4.display(); cout << "两日期比较" << endl; Date d4(2016, 10, 9); Date d5(2016, 10, 8); Date c5; cout << boolalpha << c5.IsInvalid(2017, 1, 88) << endl; cout << boolalpha << (Date(2016, 10, 1) ==d5) << endl; system("pause");}//扩展://1、可在日期类中添加星期//2、按照日历的形式显式日期//3、可以显式上个月下个月类似日历的翻页//4、……
测试结果如下:
阅读全文
0 0
- 修改后的日期类
- C#控制台基础 打开指定的文件夹后,第一眼看到的文件夹的名字,创建日期,最后修改日期
- 关于new Date()获取到Date时间后的日期格式修改
- 计算给定日期的后一天日期
- AssetsManager.cpp 手动修改后的类
- 更改文件的修改日期
- 修改nginx的日期格式
- 修改日期格式的方法
- 获取当前日期的星期&获取前几天,后几天的日期的工具类
- Linux读写文件所需要的权限,查看stat文件后最后三行日期的含义与修改
- java Calendar 工具类(几天前的日期,几天后的日期)(持续补充)
- java 日期 格式 前多少天, 后多少天的获取方法,日期工具类 星期
- 时间日期工具类 封装工作中常用的一些时间日期计算方法(转+自己修改)
- C# 获得程序集版本信息,文件修改日期,最后修改日期的类,IList 排序类
- 修改后的Blog样式
- 修改后的MFCC提取
- 修改后的通达信截图
- 修改后的cxgrid样式
- 第三周 【项目 4
- 菜鸟先飞之jAVA_异常
- Quartus_II_13.1c (64-bit)与仿真器的安装与破解
- 第四周 建立单链表算法库
- AVA技术路线图
- 修改后的日期类
- (欧) 第6章 预处理、const与sizeof
- 虚函数
- hadoop之yarn的资源分配详解
- [NLP]1.StanfordNLP的安装和初探
- 设计模式9-外观模式
- 第四周 建立双链表算法库
- Lua文件夹及文件操作(清空文件夹下所有文件、创建多级文件夹、删除文件夹所有文件等)
- PHP对图像函数进行封装,以左下角为坐标原点