C++日期计算器的实现

先来波剧透:

这里写图片描述

验证

这里写图片描述
具体实现代码如下:

test.h

#include<iostream>
using namespace std;

class Date
{
    friend void print_date(int year, int month);//打印日历
    friend ostream& operator<<(ostream&_cout, const Date& d);
    friend istream& operator>>(istream&_cin, Date& d);

public:
    int days_aimmonth(int year, int month);//所求月的天数
    int week_firstday(int year, int month);//统计目标月份第一天是周几
    Date();
    Date(int year, int month, int day);
    Date(const Date&d);
    Date& operator=(const Date& d);
    Date& operator+(int day);
    Date& operator++(int);
    Date& operator++();
    Date& operator--();
    Date& operator--(int);
    int operator-(Date& d); 
    Date& operator-(int day);

    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);

private:
    int _year;
    int _month;
    int _day;
};

bool Judge_leap(int year);//判断是否为闰年
static int week_firstday(int year, int month);

date.cpp

#include "test.h"
ostream& operator<<(ostream&_cout, const Date& d)
{
    _cout << d._year << "-" << d._month << "-" << d._day;
    return _cout;
}

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

Date::Date()
{}

Date::Date(int year = 2017, int month = 1, int day = 1)
:_year(year), _month(month), _day(day)
{
    if (day > days_aimmonth(_year,_month))
    {
        _day = day - days_aimmonth(_year, _month);
        _month += 1;

        if (month > 12)
        {
            _month -= 12;
            _year += 1;
        }
    }
}

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

Date& Date::operator=(const Date &d)
{
    if (*this != d)
    {

        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    return *this;
}

Date& Date::operator+(int day)
{
    _day = _day + day;

    while (_day > days_aimmonth(_year, _month))
    {
        _day = _day - days_aimmonth(_year, _month);
        _month += 1;

        if (_month > 12)
        {
            _year += 1;
            _month = 1;
        }
    }
    return *this;
}

Date& Date::operator++(int)
{
    ++_day;
    if (_day > days_aimmonth(_year, _month))
    {
        _day = _day - days_aimmonth(_year, _month);
        _month += 1;
        if (_month > 12)
        {
            _year+=1;
            _month -= 12;
        }
    }
    return *this;
}

Date& Date::operator++()
{
    _day++;
    if (_day > days_aimmonth(_year, _month))
    {

        _day = _day - days_aimmonth(_year, _month);
        _month += 1;
        if (_month > 12)
        {
            _year += 1;
            _month -= 12;
        }
    }
    return *this;
}

Date& Date::operator-(int day)
{
    _day = _day - day;
    while (_day <= 0)
    {
        _day += days_aimmonth(_year, _month);
        _month -= 1;
        if (_month <= 0)
        {
            _year -= 1;
            _month = 12;
        }
    }
    return *this;
}

int Date::operator-(Date& d) //日期减日期
{
    Date min(*this);
    Date max(d);
    int tag = -1;
    int count = 0;
    if (d < *this)
    {
        min = d;
        max = *this;
        tag = 1;
    }
    while (min != max)
    {
        ++min;
        count++;
    }
    return tag*count;
}

Date& Date::operator--()
{
    _day--;
    if (_day == 0)
    {
        _day = days_aimmonth(_year, _month);
        _month -= 1;
        if (_month == 0)
        {
            _year -= 1;
            _month = 12;
        }
    }
    return *this;
}

Date& Date::operator--(int)
{
    --_day;
    if (_day == 0)
    {
        _day = days_aimmonth(_year, _month);
        _month -= 1;
        if (_month == 0)
        {
            _year -= 1;
            _month = 12;
        }
    }
    return *this;
}

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;
            }
        }
    }
    return false;
}

bool Date::operator<(const Date d)
{
    if (_year < d._year)
        return true;
    else if (_year > d._year)
        return false;
    else
    {
        if (_month < d._month)
            return true;
        else if (_month > d._month)
            return false;
        else
        {
            if (_day<d._day)
                return true;
            else
                return false;
        }
    }
}

bool Date::operator>=(const Date d)
{
    if (_year >= d._year)
        return true;
    else if (_month >= d._month)
        return false;
    else
    {
        if (_month >= d._month)
            return true;
        else if (_month <= d._month)
            return false;
        else
        {
            if (_day>=d._day)
                return true;
            else
                return false;
        }
    }
}

bool Date::operator<=(const Date d)
{
    if (_year <= d._year)
        return true;
    else if (_month <= d._month)
        return false;
    else
    {
        if (_month <= d._month)
            return true;
        else if (_month <= d._month)
            return false;
        else
        {
            if (_day<=d._day)
                return true;
            else
                return false;
        }
    }
}

bool Date::operator==(const Date d)
{
    if ((_year == d._year) && (_month == d._month) && (_day == d._day))
        return true;
    else
        return false;
}

bool Date::operator!=(const Date d)
{
    if (*this == d)
        return false;
    else
        return true;
}

void print_date(int year, int month)//打印目标月份日历
{
    cout << year << "年" << month << "月" << endl;
    cout << "日 " << "一 " << "二 " << "三 " << "四 " << "五 " << "六 " << endl;
    int i = 0;
    int j = 0;
    Date temp(year, month, 1);
    int week = week_firstday(year, month);
    int day = temp.days_aimmonth(year, month);
    int k = 1;

    for (i = 0; i < week; i++)
    {
        cout << "   ";
    }
    for (i = 0; i < 6; i++)
    {
        for (j = 0; j < 7; j++)
        {
            if (i == 0)
            {
                if (j == 7 - week)
                    break;
            }
            if (k <= day)
                printf("%2d ", k++);
        }
        cout << endl;
    }
}

static bool Judge_leap(int year)//判断是否为闰年
{
    if ((year % 4 == 0) || (year % 400 == 0) && (year % 100 != 0))
        return true;
    else
        return false;
}

int Date::days_aimmonth(int year, int month)//求目标月份天数
{
    int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if (Judge_leap(year) == true)
    {
        day[2] = 29;
    }
    return day[month];
}

static int week_firstday(int year, int month)
{
    Date d1(1949, 9, 1);//新中国自1949年开始使用公元纪年法
    int week = 4;//公元1949年9月1日为周四
    int day = 0;    
    Date d2(year, month, 1);
    day = d2 - d1;
    week += day % 7;
    if (week > 7)
    {
        week -= 7;
    }
    return week;
}

test.cpp

#include "test.h"

void menu()
{
    cout << "####  ---请输入想进行的运算的序号---  ####" << endl;
    cout << "#### 1.打印目标月份日历(格式:年 月)####" << endl;
    cout << "#### 2.相隔天数计算  #####################" << endl;
    cout << "#### 3.日期推算(填负数向前推进) ##########" << endl;
    cout << "#### 0.退出###############################" << endl;

}

int main()
{
    menu();
    while (1)
    {
        int num = 0;
        cin >> num;

        switch (num)
        {
        case 1:
        {
                  cout << "请输入年份月份:";
                  int a = 0, b = 0;
                  cin >> a;
                  cin >> b;
                  print_date(a, b);
                  break;
        }
        case 2:
            {
                  cout << "请输入日期:";
                  Date d1;
                  cin >> d1 ;

                  cout << "请输入日期:";
                  Date d2;
                  cin >> d2;

                  int day = 0;
                  day = d1 - d2;
                  cout <<"日期相差:"<< day <<"天"<< endl;
                break;
            }

        case 3:
            {
                  cout << "请输入日期:";
                  Date d1,d2;
                  cin >> d1;

                  cout << "请输入推算的天数:";
                  int n = 0;
                  cin >> n;

                  cout << "推算后日期为:";
                  if (n > 0)
                  {
                      d2 = d1 + n;
                  }
                  else
                  {
                      d2 = d1 - (-n);
                  }
                  cout<<d2;
                  break;
        }
        case 0:
            return 0;   
        }
    }

    system("pause");
    return 0;
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fly_bit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值