实现日期类(日期计算器)

在日常生活中,需要我们计算一些日期,如果一些小的数字相加的话,我们便可以很方便的就计算出来,然而如果需要加上一些特别大的数字的时候,则会很浪费我们的时间,因此,日期计算机的出现,极大的方便了我们的生活,为我们带来极大的便利。
下面,我们将给出这个日期计算机的实现方式,希望大家相互学习,相互鼓励。

代码思路:当天满时,则向月进位,当月满时,则向年进位。借位同理(判断是否为有效年)。

这里写图片描述

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

class Date
{
public:
    //思路:定义一个起始日期,如果日期合法,发返回,否则返回-1;

    Date(int year = 2017, int month = 9, int day = 10)//构造函数
        : _year(year)  //初始化列表
        , _month(month)
        , _day(day)
    {}

    Date(const Date& d)//拷贝构造函数
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    //获得某月的天数
    int Getmonthday(int _year, int _month)
    {
        //闰年
        if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
        {
            if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12)
            {
                return 31;
            }
            else if (_month == 2)
            {
                return 29;
            }
            else
            {
                return 30;
            }
        }
        else//平年
        {
            if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12)
            {
                return 31;
            }
            else if (_month == 2)
            {
                return 28;
            }
            else
            {
                return 30;
            }
        }
    }

    //判断日期是否合法 
    bool IsInvalid(int year, int month, int day)//判断日期是否合法
    {
    //此处从1900年开始,如果小于这个日期,则返回fasle
        return ((year > 1900) && (month > 0) && (month<13) && (day>0) && (day < Getmonthday(year, month)));
    }

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

     前置++ 分情况(方法一)
    //Date& operator++()
    //{
    //  if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12)
    //  {
    //      if (_day < 31)
    //      {
    //          _day += 1;
    //      }
    //      else
    //      {
    //          if (_month < 12)
    //          {
    //              _month += 1;
    //              _day = 1;
    //          }
    //          else
    //          {
    //              _year += 1;
    //              _month = 1;
    //              _day = 1;
    //          }
    //      }
    //  }

    //  else if (_month == 4 || _month == 6 || _month == 9 || _month == 11)
    //  {
    //      if (_day < 30)
    //      {
    //          _day += 1;
    //      }
    //      else
    //      {
    //              _month += 1;
    //              _day = 1;
    //      }
    //  }
    //  //判断是否是闰年
    //  else
    //  {
    //      if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
    //      {
    //          if (_day < 29)
    //          {
    //              _day += 1;
    //          }
    //          else
    //          {
    //              _month += 1;
    //              _day = 1;
    //          }
    //      }
    //      else
    //      {
    //          if (_day < 28)
    //          {
    //              _day += 1;
    //          }
    //          else
    //          {
    //              _month += 1;
    //              _day = 1;
    //          }
    //      }
    //  }
    //  return *this;
    //}

    //方法二:前置++
    Date operator++()
    {
        _day += 1;
        if (_day > Getmonthday(_year, _month))//当加一天之后,这个天数大于这个月的标准天数,才向下一个月进一位
        {
            ++_month;
            _day = 1;
            if (_month > 12)//如果月进完之后,加完之后的月大于12,则要向年进位
            {
                ++_year;
                _month = 1;
                _day = 1;
            }
        }
        return *this;
    }


    // 后置++ 返回值中day的天数不能变
    Date operator++(int)
    {
        Date tmp(*this);//保存当前对象
        ++tmp;
        //tmp._day += 1;
        return *this;
    }


    //方法一:前置--
    //Date& operator--()
    //{
    //  switch (_month)
    //  {
    //  case 5:
    //  case 7:
    //  case 8:
    //  case 10:
    //  case 12:
    //      if (_day > 1)
    //      {
    //          _day -= 1;
    //      }
    //      else
    //      {
    //          _month -= 1;
    //          _day = 30;
    //      }
    //      break;
    //  case 4:
    //  case 6:
    //  case 9:
    //  case 11:
    //      if (_day > 1)
    //      {
    //          _day -= 1;
    //      }
    //      else
    //      {
    //          _month -= 1;
    //          _day = 31;
    //      }
    //      break;
    //  case 1:
    //      if (_day > 1)
    //      {
    //          _day -= 1;
    //      }
    //      else
    //      {
    //          _year -= 1;
    //          _month = 12;
    //          _day = 31;
    //      }
    //      break;
    //  case 3:
    //      if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))//闰年
    //      {
    //          if (_day > 1)
    //          {
    //              _day -= 1;
    //          }
    //          else
    //          {
    //              _month -= 1;
    //              _day = 29;
    //          }
    //      }
    //      else
    //      {
    //          if (_day > 1)
    //          {
    //              _day -= 1;
    //          }
    //          else
    //          {
    //              _month -= 1;
    //              _day = 28;
    //          }
    //      }
    //      break;
    //  case 2:
    //      if (_day > 1)
    //      {
    //          _day -= 1;
    //      }
    //      else
    //      {
    //          _month -= 1;
    //          _day = 31;
    //      }
    //      break;
    //  }
    //  return*this;
    //}


    //方法二:前置--
    Date operator--()
    {
        _day -= 1;
        if (_day == 0)
        {
            --_month;
            _day = Getmonthday(_year, _month);
            if (_month == 0)
            {
                --_year;
                _month = 12;
                _day = 31;
            }
        }
        return* this;
    }

    Date operator--(int)
    {
        Date tmp(*this);//保存当前对象
        --tmp;
        //tmp._day -= 1;
        return *this;
    }

    //days天之后的日期 
    Date operator+(int day)
    {
        if (day < 0)
        {
            return *this - (-day);
        }
        _day += day;
        while (_day>Getmonthday(_year, _month))//当当前加上的天数,大于合法的日期,则要要下一个月进位
        {
            _day = _day - Getmonthday(_year, _month);//这块得到的天数,亦指本月已加满的天数
            ++_month;
            if (_month > 12)//如果加上的天数,超过一年,则需要年进位
            {
                ++_year;
                _month = 1;
            }
        }
        return *this;
    }

    // days天之前的日期 
    Date operator-(int days)
    {
        if (days < 0)
        {
            return *this + (-days);//需要减的天数,如果小于0,则相当于加上某些天
        }
        _day -= days;
        while (_day < 0)//当减去之后,这个数小于0,则需要向上一个月借
        {
            --_month;
            if (_month == 0)//如果借完之后,_month == 0,则需要向上一年借
            {
                --_year;
                _month = 12;
            }
            _day = Getmonthday(_year, _month)+days;//此时的days相当于减了之后的days
        }
        return*this;
    }




    int operator-(const Date& d)//计算两个日期之间差了多少天
    {
        int count = 0;
        Date big;//定义一个大日期
        Date small;//定义一个小日期
        if (operator<(d))//定义一个大小日期,用小日期一天一天的加,直到等于大的日期,计算中间加了多少天,返回那个较小的日期
                    //机制:d与this指针相比 即d._year<_year...等
            {
                big = *this;
                small = d;
            }
        else
            {
                big = d;
                small = *this;
            }
        while (small != big)
            {
                small.operator++();
                count++;//让小的那个天数,一天天的加,直到两个相等为止
            }
        return count;//返回加的天数
    }

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

    bool operator!=(const Date& d)const 
    {
        //法一:
        //if ((_year != d._year) || (_month != d._month) || (_day != d._day))
        //{
        //  return true;//不相等
        //}
        //else
        //return false;

        //法二:
        return !(*this == d);
    }

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

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

    void PrintData()
    {
        cout << _year << "  " << _month << "  " << _day<<endl;
    }

    ~Date()//调用析构函数
    {}

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

下面将是测试代码:

int main()
{
    Date d1;
    Date d2(2017, 9,23);
    Date d3(2017, 1, 1);
    Date d4(2017, 9, 30);
    Date d5(d1);//调用拷贝构造函数

    d1.operator+(600);
    d1.PrintData();

    d2.Getmonthday(2018,10);
    d2.PrintData();

    d3.IsInvalid(2017,1,36);
    d3.PrintData();

    d4.operator++();
    d4.PrintData();

    d3.operator++(1);
    d3.PrintData();

    d3.operator--();
    d3.PrintData();

    d3.operator--(1);
    d3.PrintData();

    d4.operator<(d3);
    d4.PrintData();

    d2.operator=(d2);
    d2.PrintData();

    d2.operator==(d4);
    d2.PrintData();

    d1.operator>(d2);
    d1.PrintData();

    d1.operator!=(d3);
    d1.PrintData();

    system("pause");
    return 0;
}

写到这里,一个日期类大概就实现了,如果大家有更好的意见,,一起交流哦!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值