日期类的创建和一些基本应用。
来源:互联网 发布:2015十大网络神曲视频 编辑:程序博客网 时间:2024/05/22 04:37
#include<iostream>using namespace std;class Date{public:Date(int year = 2017, int month = 9, int day = 10): _year(year), _month(month), _day(day){
//这里是判断如果输入内容不符合日期的格式,就会默认为1990年的1月1日再输出。if (!((_year > 0) && (_month > 0) && (_month < 13) && (day>0) && (day <= GetMonthDays(_month)))){_year = 1990;_month = 1;_day = 1;} }
//拷贝构造函数,使用类创建新类。Date(const Date& d):_year(d._year),_month(d._month),_day(d._day){}
//输出符号的重载。ostream& operator<<(ostream& _cout){_cout << _year << '-' << _month << '-' << _day;return _cout;}
//赋值的重载。Date& operator=(const Date& d){_day = d._day;_month = d._month;_year = d._year;return *this;}// 前置++ Date& operator++(){*this = *this + 1;return *this;}// 后置++ Date operator++(int){Date temp(*this);*this = *this + 1;return temp;}Date& operator--(){*this = *this - 1;return *this;}Date operator--(int){Date temp(*this);*this = *this - 1;return temp;}//days天之后的日期 Date operator+(int days){Date temp(*this);temp._day += days; //我们在这里先将天数直接加进去int daysInmonth = temp.GetMonthDays(temp._month); //在这里我们可以得到这个月的总天数while (temp._day > daysInmonth){temp._day -= daysInmonth; //我们将这个月的总天数减掉,相当于走过了这些天数,日期就到了下个月的相同天数temp._month += 1; //走过了天数,我们将月份加1if (temp._month > 12) //在这里我们判断的是如果月份在12时天数还大于31时的情况{temp._month = 1;temp._year += 1;}daysInmonth = temp.GetMonthDays(temp._month);// 由于月份改变,每月的天数我们也要改变}return temp;}// days天之前的日期 Date operator-(int days) //这个跟上面步骤相似{Date temp(*this);temp._day -= days;int daysInmonth = temp.GetMonthDays(temp._month);while (temp._day <= 0){temp._month--;if (temp._month <= 0){temp._year--;temp._month = 12;}temp._day += temp.GetMonthDays(temp._month);}return temp;}// 两个日期之间的距离 int operator-(const Date& d){Date max(*this);Date min(d);if (min > max){Date temp = max;max = min;min = temp;}int count = 0;{while (min < max){min = min + 1;count += 1;}}return count;}bool operator==(const Date& d){if (_year == d._year&&_month == d._month&&_day == d._day){return true;}return false;}bool operator!=(const Date& d){return !(*this == d);}bool operator>(const Date& d){if ((_year > d._year) || (_year == d._year) && (_month > d._month) || (_year == d._year) && (_month == d._month) && (_day > d._day)){return true;}return false;}bool operator<(const Date& d){if ((_year < d._year) || (_year == d._year) && (_month < d._month) || (_year == d._year) && (_month == d._month) && (_day < d._day)){return true;}return false;}bool IsLeapYear(){if ((_year % 4 == 0) && (_year % 100 != 0) || _year % 400 == 0){return true;}return false;}int GetMonthDays(int month){int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (IsLeapYear() && (2 == month)){days[month] += 1;}return days[month];}private:int _year;int _month;int _day;};int main(){Date a(2016, 2, 28); /*Date b = a++;*/Date c(2015, 2, 28);Date b;b = a;b - 100 << cout;cout << endl;cout << a - c << endl;cout << (b > a);getchar();return 0;}
//下图为我的编译器显示结果,
阅读全文
0 0
- 日期类的创建和一些基本应用。
- 日期类的一些基本操作
- java日期的一些应用
- 类的创建与基本应用--时钟
- 有关一些swift 控件的基本创建和使用
- 数据库的一些基本应用
- iOS中时间日期的基本应用
- 关于日期选择器的一些小应用
- SQLServer中的存储过程和函数的创建使用以及一些常用的日期函数
- 一些容易被大家忽视的C语言函数和一些C++函数的基本应用
- 日期类的应用
- 日期 日历的一些常用类和方法
- 经验总结:mysql 的一些基本应用
- CAlayer的一些基本属性应用
- SQL语法的一些基本应用
- CAlayer的一些基本属性应用
- Android Studio的一些基本应用
- 二、Python的一些基本应用
- HDU2709||Sumsets
- 在VIM中显示缩进对齐线
- Windows7无法设置Aero效果/透明效果
- py-faster-rcnn运行实例与安装问题解决
- CSS预编译工具(SASS,LESS)
- 日期类的创建和一些基本应用。
- 心情
- 虚拟机vmnet0、vmnet1和vmnet8的区别 虚拟网卡概述
- 搞cms程序的第一天
- 确保不是在栈区申请内存
- HDU
- 蓝桥杯 算法提高 欧拉函数
- C#学习问题一:使用GDI+画图类进行图形绘制
- SVM进阶问题