日期类->日期计算器

来源:互联网 发布:数据库性能测试工具 编辑:程序博客网 时间:2024/05/16 06:23

date.h

#pragma once#include<iostream>using namespace std;class Date{    friend ostream& operator<<(ostream& out, const Date& d);    friend istream& operator>>(istream& out, Date& d);public:    Date(int year = 1900, int month = 1, int day = 1)        :_year(year)        ,_month(month)        ,_day(day)    {        // 如何检查一个日期是否合法         int days = GetMonthDays(year, month);        if (days == -1 || day < 1 || day > days)        {            cout << "日期不合法" << endl;            Display();            exit(-1);        }    }    //operator>     inline bool operator>(const Date& d) const    {        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;    }    //operator>=     inline bool operator>=(const Date& d) const    {        return *this > d || *this == d;    }    //operator<     bool operator<(const Date& d) const    {        return !(*this >= d);    }    //operator<=     inline bool operator<=(const Date& d) const    {        return *this < d || *this == d;    }    //operator==     inline bool operator==(const Date& d) const    {        return _year == d._year            && _month == d._month            && _day == d._day;    }    //operator!=     inline bool operator!=(const Date& d) const    {        return !(*this == d);    }    bool IsInvalid()    {        if (_day < 1             || _day >GetMonthDays(_year, _month)            || _year < 1900             || _month < 1             || _month >12)        {            return true;        }        else        {            return false;        }    }    //d1 + 100     Date operator+(int day)    {        if (day < 0)        {            return *this -(-day);        }        Date tmp(*this);        tmp._day += day;        while (tmp.IsInvalid())        {            tmp._day -= GetMonthDays(tmp._year, tmp._month);            tmp._month++;            if (tmp._month == 13)            {                tmp._year++;                tmp._month = 1;            }        }        return tmp;    }    inline Date& operator+=(int day)    {        *this = *this + day;        return *this;    }    Date operator-(int day)    {        if (day < 0)        {            return *this + (-day);        }        Date tmp(*this);        tmp._day -= day;        while (tmp.IsInvalid())        {            --tmp._month;            if (tmp._month == 0)            {                tmp._year--;                tmp._month = 12;            }            tmp._day += GetMonthDays(tmp._year, tmp._month);        }        return tmp;    }    Date& operator-=(int day)    {        *this = *this - day;        return *this;    }    inline Date& operator++()    {        *this += 1;        return *this;    }    inline Date operator++(int)    {        Date tmp(*this);        *this += 1;        return tmp;    }    inline Date& operator--()    {        *this -= 1;        return *this;    }    inline Date operator--(int)    {        Date tmp(*this);        *this -= 1;        return tmp;    }    int operator-(const Date& d)    {        Date min = d, max = *this;        int tag = 1;        if (*this < d)        {            min = *this;            max = d;            int tag = -1;        }        int count = 0;        while (min != max)        {            ++min;            ++count;        }        return count*tag;    }    void Display()const    {        cout << _year << "-" << _month << "-" << _day << endl;    }    inline static bool IsLeapYear(int year)    {        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))        {            return true;        }        return false;    }    inline static int GetMonthDays(int year, int month)//判断日期是否合法    {        if (year < 1900 || month < 1 || month >12)        {            return -1;        }        static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };        int days = monthDays[month];        if (month == 2 && IsLeapYear(year))        {            days += 1;        }        return days;    }private:    int _year;    int _month;    int _day;};//重载<<和>>的原因:例如cout<<d1<<endl,输入输出库函数并不知道类的属性又或者是以什么的格式去输出。ostream& operator<<(ostream& out, const Date& d)//这里不写成void operator<<(ostream& out, const Date& d)的原因是,写成这样只能是写在类的成员函数里,this指针会成为左操作数,会出现类似d1<<cout这样的情况,可读性不好。写成ostream& operator<<(ostream& out, const Date& d),定义成全局的,返回引用,还可以实现链式访问,例如:cout<<d1<<d2<<endl,只不过定义成全局以后,要用友元函数调用日期类。{    out << d._year << "-" << d._month << "-" << d._day << endl;    return out;}istream& operator>>(istream& in,  Date& d)//这里的Date& d不能写成const Date& d,不能加const的原因是输入一个日期会被修改。{    cout<<"请依次输入年月日:";    in >> d._year;    in >> d._month;    in >> d._day;    return in;}void TestDate(){    int num = 0;    while (1)    {        Date d1;        Date d2;        int day;        cout << "***********************************************" << endl;        cout << "请选择" << endl;        cout << "1.日期加一个天数 2.日期减日期 3 退出" << endl;        cout << "***********************************************" << endl;        cin >> num;        switch (num)        {        case 1:            cin >> d1;            cout << "请输入一个天数:";            cin >> day;            cout << "计算结果:" << (d1 + day) << endl;            break;        case 2:            cin >> d1;            cin >> d2;            cout << "相差天数为:" << d2 - d1 << endl;            break;            break;        case 3:            return;        default:            cout << "无效选项,请重新选择!" << endl;            break;        }    }}

test.cpp

#include"date.h"int main(){    TestDate();    system("pause");    return 0;}

程序运行结果:
这里写图片描述

原创粉丝点击