日期类

//#include <iostream>
//using namespace std;
//
//class Array
//{
//public:
//  Array(size_t size)
//  {
//      _array = (int*) malloc(sizeof(int)*size);
//  }
//  }
//
//  ~Array()
//  {
//      free(_array);
//      _array = NULL;
//  }
//private:
//  int * _array;
//};
//
//class Date
//{
//public:
//  Date(int year, int month, int day)
//  {
//      _year = year;
//      _month = month;
//      _day = day;
//  }
//
//  ~Date()
//  {
//      //cout<<"~Date()"<<endl;
//      //cout<<_year<<"-"<<_month<<endl;
//  }
//
//  void Display()
//  {
//      cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
//  }
//
//  //bool Greater(const Date& d);
//  bool 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
//          {
//              return _day > d._day;
//          }
//      }
//  }
//  
//  // d1 = d2 = d3;
//
//  // d1 = d3;
//  // d1 = d1;
//  Date(const Date& d)
//  {
//      cout<<"Date(const Date&)"<<endl;
//      _year = d._year;
//      _month = d._month;
//      _day = d._day;
//  }
//
//  Date& operator=(const Date& d)
//  {
//      cout<<"operator="<<endl;
//      if (this != &d)
//      {
//          this->_year = d._year;
//          this->_month = d._month;
//          this->_day = d._day;
//      }
//
//      return *this;
//  }
//  /*void operator=(const Date& d)
//  {
//      cout<<"operator="<<endl;
//
//      this->_year = d._year;
//      this->_month = d._month;
//      this->_day = d._day;
//  }*/
//
//private:
//  int _year;
//  int _month;
//  int _day;
//};
//
////bool operator>(const Date& d1, const Date& d2)
////{
////    if (d1._year > d2._year)
////    {
////        return true;
////    }
////    else if (d1._year < d2._year)
    {
        return false;
    }
    else
    {
        if (d1._month > d2._month)
////        {
////            return true;
////        }
////        else if (d1._month < d2._month)
        {
            return false;
        }
        else
        {
            return d1._day > d2._day;
////        }
////    }
////}
//
//int main()
//{
//  // A 2,1 B 1,2
//  Date d1(2016, 1, 1);
//  Date d2(d1);
//  Date d3 = d1;
//  Date d4(2016, 12, 24);
//  d1 = d4;
//
//  //Date d3(2016,12,24);
//
//  // d1.operator(&d1, d2.operator=(&d2, d3));
//  //d1 = d2 = d3;
//  //d1 = d1;
//  //d2.Display();
//  //d3.Display();
//
//  // 可读性
//  //cout<<(d1 > d3)<<endl;
//  //cout<<d1.Greater(d3)<<endl;
//
//  return 0;
//}

#include <iostream>
#include <assert.h>
using namespace std;

class Date
{
public:
    Date(int year, int month, int day)
    {
        _year = year;
        _month = month;
        _day = day;

        assert(IsInvalid());
    }

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

    bool IsInvalid()
    {
        if (_year >= 0
            && _month > 0 && _month < 13
            && _day > 0 && _day <= GetMonthDays(_year, _month))
        {
            return true;
        }

        return false;
    }

    bool IsLeapYear(int year)
    {
        if ((year%4 == 0 && year%100 != 0) || (year%400 == 0))
        {
            return true;
        }

        return false;
    }

    int GetMonthDays(int year, int month)
    {
        assert(month > 0 &&  month < 13);
        static int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int day = days[month];

        if (month == 2 && IsLeapYear(year))
        {
            day += 1; 
        }

        return day;
    }

    // 拷贝构造、赋值运算符的重载、析构
    bool operator>(const Date& d)
    {
        //...
        return true;
    }
    bool operator<(const Date& d);
    bool operator>=(const Date& d);
    bool operator<=(const Date& d);
    // d1 == d2
    bool operator==(const Date& d)
    {
        return _year==d._year
            && _month==d._month
            && _day==d._day;
    }

    // ++d == d.operator++(&d)
    Date& operator++()   // 前置
    {
        int days = GetMonthDays(_year, _month);
        if (_day < days)
        {
            ++_day;
        }
        else if (_day == days)
        {
            if(_month == 12)
            {
                _day = 1;
                _month = 1;
                _year++;
            }
            else
            {
                _day = 1;
                _month++;
            }
        }
        else
        {
            // 防御式编程
            assert(false);
        }

        return *this;
    }

    // d++
    Date operator++(int) //后置
    {
        Date tmp(*this);

        //++(*this); // operator++(this);
        operator++();

        return tmp;
    }

    Date operator--()
    {
        if (_day > 1)
        {
            --_day;
        }
        else if (_day == 1)
        {
            if (_month == 1)
            {
                --_year;
                _month = 12;
                _day = GetMonthDays(_year, _month);
            }
            else
            {
                --_month;
                _day = GetMonthDays(_year, _month);
            }
        }

        return *this;
    }

    //d1 + -40;
    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);
            if (tmp._month == 12)
            {
                ++tmp._year;
                tmp._month = 1;
            }
            else
            {
                ++tmp._month;
            }
        }

        return tmp;
    }

    Date operator-(int day);

    Date& operator+=(int day)
    {
        *this = *this + day;
        return *this;
    }
    Date operator-=(int day);

    // d1 - d2
    int operator-(const Date& d)
    {
        size_t count = 0;
        int tag = 1;
        Date* max = this;
        Date* min = &d;
        if (*this < d)
        {
            max = &d;
            min = this;
            tag = -1;
        }

        while (*min != *max)
        {
            ++*min;
            ++count;
        }

        return count*tag;
    }

protected:
    int _year;
    int _month;
    int _day;
};

void main()
{
    Date d1(2017, 1, 12);
    Date d2(d1);

    d1.Display();
    d1 += 40;
    d1.Display();
    d1 += -40;
    d1.Display();
}


//class Complex
//{
//public:
//  Complex(double real = 0.0, double imge = 0.0)
//  {
//      _imge = imge;
//      _real = real;
//  }
//
//  // 复用
//  // c1 < c2
//  bool operator<(const Complex& c)
//  {
//      if (_real > c._real)
//      {
//          return true;
//      }
//      else if (_real < c._real)
//      {
//          return false;
//      }
//      else
//      {
//          return _imge > c._imge;
//      }
//  }
//
//  // c1 > c2
//  bool operator>(const Complex& c)
//  {
//      return !(*this < c || *this == c);
//  }
//
//  bool operator<=(const Complex& c)
//  {
//      return *this < c && *this == c;
//  }
//
//  bool operator>=(const Complex& c)
//  {
//      //return 
//  }
//
//  bool operator==(const Complex& c)
//  {
//      return _real == c._real && _imge == c._imge;
//  }
//
//  bool operator!=(const Complex& c);
//
//  // ++c->c.operator++(&c) call ___
//  Complex& operator++() // @Zoperator++C
//  {
//      ++_real;
//      return *this;
//  }
//
//  // 占位
//  // c++->c.operator++(&c, int()); // @Z@Zoperator++CI
//  Complex operator++(int)
//  {
//      Complex tmp(*this);
//      ++_real;
//      return tmp;
//  }
//
//  Complex operator--();
//  Complex operator--(int);
//
//  // + - * / += -= *= /=
//private:
//  double _real; // 实部
//  double _imge; // 虚部
//};
//
//int main()
//{
//  cout<<int()<<endl;
//
//  return 0;
//}

关于日期d1-d2所得差值的思路总结:
方法一:重载一个自增运算符,然后使用计数器来计算两个日期之间的差值。
int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2)
{
int flag = 1;
if (d1 > *this)
{
flag = -flag;
}
int count = 0;
Date minDate = *this;
Date maxDate = d1;
if (minDate > maxDate)
{
Swap(minDate, maxDate);
}
while (minDate != maxDate)
{
minDate++;
count++;
}
return flag * count;
}

第二种方法:
①当d1和d2是同年且同月的日期时:
结果为return d1._day - d2._day;
②当d1和d2是同年但不同月时:
则用d1当年的天数减去d2当年的天数。
③当d1和d2不同年时(比较复杂,需仔细分析):

int remainDays = 0;//小的日期在当年剩余的天数(到年底)
int dayInYear = 0;//大的日期在当年的天数
int d_value = 0;//两个日期年之间相差的天数
令变量minDate表示小的日期;
maxDate表示大的日期。
1)首先,求的remainDays的值(通过minDate)
当minDate日期为闰年时:remainDays = 366 - 小日期当年的天数
否则:remianDays = 365 - 小日期当年的天数
2)求的dayInYear的值(通过maxDate)
dayInYear = 大的日期在当年的天数
3)求的d_value的值(通过minDate和maxDate):

所有代码: //Date.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<cassert>
#include<ctime>
class Date
{
public :
    Date(int year = 1990, int month = 1, int day = 1);
    Date operator+(int day);
    Date& operator+=(int day);
    Date operator-(int day);
    Date& operator-=(int day);
    int operator-(const Date& d);
    Date& operator++();//前置++
    Date operator++(int);//后置++
    Date& operator--();//前置--
    Date operator--(int);//后置--
    void Dispaly();
    int Date::DayInYear(const Date& d);
    bool IsLeapYear(int year);
    friend void Swap(Date& d1, Date& d2);
    friend bool operator>(const Date& d1, const Date& d2);
    friend bool operator<(const Date& d1, const Date& d2);
    friend bool operator==(const Date&d1, const Date& d2);
    friend bool operator!=(const Date&d1, const Date& d2);

protected:
    bool IsValid(const Date& d)
    {
        if (d.m_year > 0 && (d.m_month > 0 && d.m_month < 13) && (d.m_day > 0 && d.m_day <= GetMonthDays(d.m_year, d.m_month)))
        {
            return true;
        }
        return false;
    }
    int GetMonthDays(int year, int month)
    {
        static int MonthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (month != 2)
        {
            return MonthDays[month];
        }
        else if (IsLeapYear(year))//是闰年
        {
            return 29;
        }
        else
            return 28;
    }
private :
    int m_year;
    int m_month;
    int m_day;
};

//Date.cpp

#include"Date1.h"
Date::Date(int year, int month, int day)//构造函数
:m_year(year)
, m_month(month)
, m_day(day)
{
    assert(IsValid(*this) != 0);
}
Date Date::operator+(int day)//某个日期加上一个天数(形如:d1+100)
{
    Date tmp(*this);
    tmp.m_day += day;
    while (tmp.m_day > GetMonthDays(tmp.m_year, tmp.m_month))
    {
        tmp.m_day -= GetMonthDays(tmp.m_year, tmp.m_month);
        if (tmp.m_month == 12)
        {
            tmp.m_year++;
            tmp.m_month = 1;
        }
        else
        {
            tmp.m_month++;
        }
    }
    return tmp;
}
Date& Date::operator+=(int day)//注意与operator+之间的区别(一个返回引用,一个返回值)
{
    *this = *this + day;
    return *this;
}
Date& Date::operator++()//前置++
{
    *this = *this + 1;
    return *this;
}
Date Date::operator++(int)//后置++
{
    Date tmp(*this);
    *this = *this + 1;
    return tmp;
}
Date Date::operator-(int day)//某个日期减去某个天数(形如:d1-100)
{
    Date tmp(*this);
    tmp.m_day -= day;
    while (!IsValid(tmp))
    {
        if (tmp.m_month == 1)
        {
            tmp.m_year--;
            tmp.m_month = 12;
        }
        else
        {
            tmp.m_month--;
        }
        tmp.m_day += GetMonthDays(tmp.m_year, tmp.m_month);
    }
    return tmp;
}
Date& Date::operator-=(int day)
{
    *this = *this - day;
    return *this;
}
bool operator>(const Date& d1, const Date& d2)//重载>
{
    if ((d1.m_year > d2.m_year) \
        || (d1.m_year == d2.m_year) && ((d1.m_month > d2.m_month) || ((d1.m_month == d2.m_month) && (d1.m_day > d2.m_day))))
    {
        return true;
    }
    return false;
}
bool operator<(const Date& d1, const Date& d2)//重载<
{
    return !((d1 > d2) || (d1 == d2));
}
bool operator!=(const Date&d1, const Date& d2)//重载!=
{
    return !(d1 == d2);
}
bool operator==(const Date&d1, const Date& d2)//重载==
{
    if ((d1.m_year == d2.m_year) && (d1.m_month == d2.m_month) && (d1.m_day == d2.m_day))
    {
        return true;
    }
    return false;
}
//
//int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2)
//{
//  int flag = 1;
//  if (d1 > *this)
//  {
//      flag = -flag;
//  }
//  int count = 0;
//  Date minDate = *this;
//  Date maxDate = d1;
//  if (minDate > maxDate)
//  {
//      Swap(minDate, maxDate);
//  }
//  while (minDate != maxDate)
//  {
//      minDate++;
//      count++;
//  }
//  return flag * count;
//  
//}
int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2)
{
    int flag = 1;
    if (d1 > *this)
    {
        flag = -flag;
    }
    int remainDays = 0;//小的日期在当年剩余的天数(到年底)
    int dayInYear = 0;//大的日期在当年的天数
    int d_value = 0;//两个日期的年之间相差的天数
    Date d2(*this);
    assert(IsValid(d1) != 0);//判断两个日期的有效性
    assert(IsValid(d2) != 0);
    if ((d1.m_year == d2.m_year) && (d1.m_month == d2.m_month))//当两个日期是同年且同月
    {
        return d1.m_day - d2.m_day;
    }
    else if (d1.m_year == d2.m_year)//当两个日期是同年但不同月
    {
        return (DayInYear(d1) - DayInYear(d2));
    }
    else//当两个日期不同年
    {
        Date minDate = d1;
        Date maxDate = d2;
        if (minDate > maxDate)
        {
            Swap(minDate, maxDate);
        }
        if (IsLeapYear(minDate.m_year))
        {
            remainDays = 366 - DayInYear(minDate);
        }
        else
        {
            remainDays = 365 - DayInYear(minDate);
        }
        dayInYear = DayInYear(maxDate);//获得大的日期在当年的天数
        //获取两个日期的年之间的差值
        for (int year = minDate.m_year + 1; year < maxDate.m_year; year++)
        {
            if (IsValid(year))
            {
                d_value += 366;
            }
            else
            {
                d_value += 365;
            }
        }
    }
    return flag * (d_value + remainDays + dayInYear);
}
Date& Date::operator--()//前置--
{
    *this = *this - 1;
    return *this;
}
Date Date::operator--(int)//后置--
{
    Date tmp(*this);
    *this = *this - 1;
    return tmp;
}
void Swap(Date& d1, Date& d2)//交换两个日期
{
    int tmp = d1.m_year;
    d1.m_year = d2.m_year;
    d2.m_year = tmp;
    tmp = d1.m_month;
    d1.m_month = d2.m_month;
    d2.m_month = tmp;
    tmp = d1.m_day;
    d1.m_day = d2.m_day;
    d2.m_day = tmp;
}
int Date::DayInYear(const Date& d)//获取当年的天数
{
    assert(IsValid(d) != 0);
    int days = d.m_day;
    for (int month = 1; month < d.m_month; month++)
    {
        days += GetMonthDays(d.m_year, month);
    }
    return days;
}
bool Date::IsLeapYear(int year)//判断闰年
{
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 100 == 0))
    {
        return true;
    }
    return false;
}
void Date::Dispaly()//输出日期
{
    cout << m_year << "-" << m_month << "-" << m_day << endl;
}
//测试
void Test()
{
    Date d1(2017, 2, 28);
    Date d2;
    d2 = d1 + 100;
    d2.Dispaly();
    d2 = d2 - 100;
    d2.Dispaly();
}
void Test2()
{
    Date d2(2017, 2, 28);
    Date d1(2015, 3, 8);
    int start = clock();
    int day = d1 - d2;
    int end = clock();
    cout << (end - start) << endl;
    cout << day << endl;
}
void Test3()
{
    Date d2(2017, 2, 28);
    d2.Dispaly();
    d2++.Dispaly();
    Date d1(2015, 3, 8);
    d1.Dispaly();
    ++d1;
    d1.Dispaly();
}
void Test4()
{
    Date d2(2017, 2, 28);
    d2.Dispaly();
    d2--.Dispaly();
    Date d1(2015, 3, 8);
    --d1;
    d1.Dispaly();
}
void Test5()
{
    Date d1(2015, 3, 8);
    d1 += 100;
    d1.Dispaly();
    d1 -= 100;
    d1.Dispaly();
}
void Test6()
{
    Date d1(2015, 3, 8);
    Date d2(2017, 2, 28);
    cout << (d1 < d2) << endl;
    cout << (d1 > d2) << endl;
    cout << (d1 == d2) << endl;
}
int main()
{
    //Test();
    //Test2();
    //Test3();
    //Test4();
    //Test5();
    Test6();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值