运算符重载的应用:日期类
来源:互联网 发布:c语言 property get 编辑:程序博客网 时间:2024/06/05 20:02
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std;class Date{public: Date(int year = 1900, int month = 1, int day = 1) { if (year > 0 && (month > 0 && month < 13) && (day > 0 && day < Getcount(year, month))) { _year = year; _month = month; _day = day; } else cout << "the day is illegal" << endl; } Date(const Date& d) :_year(d._year) , _month(d._month) , _day(d._day) {} //合法性判断 bool leap_year(int year) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else return false; } int Getcount(int year,int month) { int arr[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (leap_year(year) && (2 == month)) { arr[month] += 1; } return arr[month]; } 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; } else 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; } else false; } bool operator == (const Date &d) { return ((_day == d._day) && (_month == d._month) && (_year == d._year)); } bool operator != (const Date &d) { return((_year != d._year) && (_month != d._month) && (_day != d._day)); } friend ostream& operator<<(ostream &_cout, const Date& d); Date& operator = (const Date& d) { if (this != &d) { _year = d._year; _month = d._month; _day = d._day; } return *this; } Date operator+(int days) { Date temp(*this); if (days < 0) { days = -days; return temp - days; } temp._day += days; while (temp._day > Getcount(temp._year, temp._month)) { temp._day -= Getcount(temp._year, temp._month); if (temp._month == 12) { temp._year ++; temp._month = 1; } else { temp._month++; } } return temp; } Date operator-(int days) { Date temp(*this); if (days < 0) { days = -days; return temp + days; } temp._day -= days; while (temp._day <= 0) { temp._day += Getcount(temp._year, temp._month); if (temp._month == 1) { temp._year -= 1; temp._month = 12; } else 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) { days++; } return days; } Date& operator--()//前置-- { return (*this = *this - 1); } Date operator--(int)//后置-- { Date temp = (*this); _day -= 1; return temp; } Date& operator++()//前置++ { _day += 1; return (*this); } Date operator++(int)//后置++ { Date tmp = (*this); _day += 1; return tmp; } void display() { cout << _year << "-" << _month << "-" << _day << endl; }private: int _year; int _month; int _day;};ostream& operator<<(ostream &_cout, const Date& d){ _cout << d._year << "-" << d._month << "-" << d._day << endl; return _cout;}int main(){ /*Date d; system("pause\n");*/ Date d(2017, 3, 15); d =d + 20; d.display(); system("pause\n"); return 0;}
阅读全文
0 0
- 运算符重载的应用:日期类
- 日期类(Date)_运算符重载简单应用
- 运算符重载----日期类
- 运算符重载--日期类
- c++日期类的实现级运算符的重载
- C++日期类的运算符重载和简单边界条件
- C++日期类(运算符的重载)
- 重载运算符的应用1
- 重载运算符的实现与应用
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 类的运算符重载
- 运算符重载--类的赋值运算符重载
- C++ 声明并实现一个日期类(运算符重载)
- 日期加减运算符重载C++修改
- 日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)
- 运算符的重载:[],(),->;
- 运算符的重载
- 运算符的重载
- LeetCode-92-Reverse Linked List II 链表反转 各种边界条件
- 栈+回溯求解数独
- 前端常见跨域解决方案(全)
- 第四单元作业
- 计算机网络作业
- 运算符重载的应用:日期类
- [NOIP2010]三国游戏 T4 题解
- 使用xshell连接 vmware
- 基于《第一行代码第二版》的天气app的扩展开发
- 说说如何实现高性能的网站架构
- jzoj 2017.10.06 模拟赛
- [NOIP2010]三国游戏 T4 模拟
- 数据结构第二周项目-体验复杂度
- 【Linux】系统目录结构