c++之日期类的实现

Date .h

#pragma  once

#include <iostream>  
using namespace std;

class Date
{
    friend void PrintCalendar(int year, int month);  //打印日历  
    friend istream& operator>>(istream& is, Date& d);
    friend ostream& operator<<(ostream& os, Date& d);
public:
    Date(int year = 1900, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        if (Illegal())
        {
            cout << "输入日期不合法" << endl;
            _year = 1600;
            _month = 1;
            _day = 1;
        }
    }

    Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    Date& operator=(const Date& d);

public:
    bool operator==(const Date& d);
    bool operator!=(const Date& d);
    bool operator<(const Date& d);
    bool operator<=(const Date& d);
    bool operator>(const Date& d);
    bool operator>=(const Date& d);
    //日期计算器  
    Date operator+(int day);
    Date operator+=(int day);
    Date operator-(int day);
    Date operator-=(int day);
    Date& operator++();
    Date operator++(int);
    Date& operator--();
    Date operator--(int);

    int operator-(const Date& d);  //计算两个日期之间相隔的天数  

private:
    bool Illegal(); //检查日期是否非法  
    void ChangeLegal(Date& date); //将日期化为合法日期  
    bool IsLeap(int year);//检查闰年  
    int DayOfMonth(int year, int month); //计算该日期中当月的天数  

private:
    int _year;
    int _month;
    int _day;

};

void PrintCalendar(int year, int month);  //打印日历  
istream& operator>>(istream& is, Date& d);
ostream& operator<<(ostream& os, Date& d);

Date.cpp

#include"Date.h"


bool Date::operator==(const Date& d)
{
    return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d)
{
    return !(*this == d);
}
bool Date::operator<(const 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;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

bool Date::operator<=(const Date& d)
{
    return  operator==(d) || operator<(d);
}

bool Date::operator>(const 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;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

bool Date::operator>=(const Date& d)
{
    return  operator==(d) || operator>(d);
}

Date& Date::operator=(const Date& d)
{
    if (*this == d)
    {
        return *this;
    }
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}

Date Date::operator+(int day)
{
    Date add(*this);
    add._day += day;

    ChangeLegal(add);
    return add;
}

Date Date::operator+=(int day)
{
    _day += day;
    ChangeLegal(*this);
    return *this;
}

Date Date::operator-(int day)
{
    Date del(*this);
    del._day -= day;
    ChangeLegal(del);
    return del;
}

Date Date::operator-=(int day)
{
    _day -= day;
    ChangeLegal(*this);
    return *this;
}

Date& Date::operator++()
{
    _day++;
    ChangeLegal(*this);
    return *this;
}

Date Date::operator++(int)  //后置++,返回值没+1,其实本身已经+1  
{
    Date ret(*this);
    _day++;
    ChangeLegal(*this);
    return ret;
}

Date& Date::operator--()
{
    _day--;
    ChangeLegal(*this);
    return *this;
}

Date Date::operator--(int)
{
    Date ret(*this);
    _day--;
    ChangeLegal(*this);
    return ret;
}

int Date::operator-(const Date& d)
{
    int day = 0;
    Date small(d);
    Date big(*this);

    if (*this < d)
    {
        small = *this;
        big = d;
    }
    else
    {
        small = d;
        big = *this;
    }

    while (small != big)
    {
        small++;
        day++;
    }
    return day;
}

bool Date::Illegal() //检查日期是否非法  
{
    if ((_year < 0) ||
        (_month < 0 || _month>12) ||
        (_day<0 || _day>DayOfMonth(_year, _month)))
    {
        return true;
    }
    else
    {
        return false;
    }
}
void Date::ChangeLegal(Date& date) //将日期转为合法日期  
{
    if (date._day < 1)
    {
        while (date._day < 1)
        {
            date._month--;
            date._day += DayOfMonth(date._year, date._month);
            if (date._month < 1)
            {
                date._year--;
                date._month = 12;
            }
        }
    }
    else if (date._day > DayOfMonth(date._year, date._month))
    {
        while (date._day > DayOfMonth(date._year, date._month))
        {
            date._day -= DayOfMonth(date._year, date._month);
            date._month++;
            if (date._month > 12)
            {
                date._year++;
                date._month = 1;
            }
        }
    }
}

bool Date::IsLeap(int year)   //检查该年是否为闰年  
{
    if (year % 400 == 0)
        return true;
    else
    {
        if (year % 4 == 0 && year % 100 != 0)
            return true;
        else
            return false;
    }
}

int Date::DayOfMonth(int year, int month)   //返回该月的天数  
{
    static int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (IsLeap(year) && month == 2)
    {
        return 29;
    }
    return day[month];
}

int MonthOfOneDay(int year, int month) //计算当前月的第一天为星期几  
{
    if ((year > 1) && (month > 1 && month < 12))
    {
        int week = 1;//1.1.1  为周一  
        int day = 0;
        Date d1(1, 1, 1);
        Date d2(year, month, 1);
        day = d2 - d1;
        week = day % 7 + week;
        if (week > 7)
        {
            week -= 7;
        }
        return week;
    }

}

void PrintCalendar(int year, int month) //打印当前月的日历  
{
    cout << year << "年" << month << "月" << endl;
    cout << "日 " << "一 " << "二 " << "三 " << "四 " << "五 " << "六 " << endl;
    int week = MonthOfOneDay(year, month);
    if (week != -1)
    {
        Date d(year, month, 1);
        int day = 1;
        int i = 0;
        for (i = 0; i < week; i++)
        {
            cout << "   ";
        }
        for (int j = 0; j < 6; j++)
        {

            for (; i < 7; i++)
            {
                if (day <= d.DayOfMonth(d._year, d._month))
                {
                    printf("%2d ", day);
                    day++;
                }
                else
                {
                    break;
                }
            }
            i = 0;
            cout << endl;
        }
    }
}

istream& operator>>(istream& is, Date& d)
{
    is >> d._year >> d._month >> d._day;
    return is;
}

ostream& operator<<(ostream& os, Date& d)
{
    os << d._year << "-" << d._month << "-" << d._day;
    return os;
}

Test1.cpp

#include<iostream>
#include "Date.h"


void fun1() //日期加减天数  
{
    Date date;
    int day;
    cout << "请输入日期" << endl;
    cin >> date;
    cout << "请输入加减的天数(减法输入负数即可)" << endl;
    cin >> day;
    cout << date + day << endl;
}

void fun2() //计算两个日期相差天数  
{
    Date d1;
    Date d2;
    cout << "请输入第一个日期" << endl;
    cin >> d1;
    cout << "请输入第二个日期" << endl;
    cin >> d2;
    cout << "相差天数为:" << d1 - d2 << endl;
}

void fun3()
{
    int year = 0;
    int month = 0;
    cout << "请输入要打印的年份和月份" << endl;
    cin >> year >> month;
    PrintCalendar(year, month);
}
void Print()
{
    cout << "*****************************" << endl;
    cout << "***请选择您要使用的功能:" << endl;
    cout << "***  1. 日期加减天数" << endl;
    cout << "***  2. 计算两个日期相差天数" << endl;
    cout << "***  3. 打印某年某月的日历" << endl;
    cout << "***  0. 退出" << endl;
    cout << "*****************************" << endl;

}

void Menu()
{
    while (1)
    {
        Print();
        int choose = 0;
        cin >> choose;
        switch (choose)
        {
        case 1:
            fun1();
            break;
        case 2:
            fun2();
            break;
        case 3:
            fun3();
            break;
        case 0:
            exit(EXIT_SUCCESS);
            break;
        default:
            cout << "输入有误,请重新输入" << endl;
            break;
        }
    }
}

int main()
{
    Menu();
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值