【c++】日期类的实现-Class Date

简介: 【c++】日期类的实现-Class Date

代码实现

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date {
public:
  // 获取某年某月的天数
  int GetMonthDay(int year, int month)
  {
    static int monteDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
      return 29;
    }
    return monteDays[month];
    /*if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
      return 31;
    }
    else if (month == 2)
    {
      if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
      {
        return 29;
      }
      else
      {
        return 28;
      }
    }
    else
    {
      return 30;
    }*/
  }
  // 全缺省的构造函数
  Date(int year = 1900, int month = 1, int day = 1)
  {
    _year = year;
    _month = month;
    _day = day;
  }
  // 拷贝构造函数
    // d2(d1)
  Date(const Date& d)
  {
    this->_year = d._year;
    this->_month = d._month;
    this->_day = d._day;
  }
  // 赋值运算符重载
  //d2 = d3 -> d2.operator=(&d2, d3)
  Date& operator=(const Date& d)
  {
    if (this != &d)
    {
      this->_year = d._year;
      this->_month = d._month;
      this->_day = d._day;
    }
    return *this;
  }
  // 析构函数
  ~Date()
  {
    ;
  }
  // 日期+=天数
  Date& operator+=(int day)
  {
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
      _day -= GetMonthDay(_year, _month);
      ++_month;
      if (_month == 13)
      {
        ++_year;
        _month = 1;
      }
    }
    return *this;
  }
  // 日期+天数
  Date operator+(int day)
  {
    /*Date tmp(*this);
    tmp._day += day;
    while (tmp._day > GetMonthDay(tmp._year, tmp._month))
    {
      tmp._day -= GetMonthDay(tmp._year, tmp._month);
      ++tmp._month;
      if (tmp._month == 13)
      {
        ++tmp._year;
        tmp._month = 1;
      }
    }
    return tmp;*/
    Date tmp(*this);
    tmp += day;
    return tmp;
  }
  // 日期-=天数
  Date& operator-=(int day)
  {
    _day -= day;
    while (_day <= 0)
    {
      --_month;
      if (_month == 0)
      {
        --_year;
        _month = 12;
      }
      _day += GetMonthDay(_year, _month);
    }
    return *this;
  }
  // 日期-天数
  Date operator-(int day)
  {
    //Date tmp = *this;
    Date tmp(*this);
    tmp -= day;
    return tmp;
  }
 
  // 前置++
  Date& operator++()
  {
    *this += 1;
    return *this;
  }
  // 后置++
  Date operator++(int)
  {
    Date tmp = *this;
    *this += 1;
    return tmp;
  }
  // 后置--
  Date operator--(int)
  {
    Date tmp = *this;
    *this -= 1;
    return tmp;
  }
  // 前置--
  Date& operator--()
  {
    *this -= 1;
    return *this;
  }
  // >运算符重载
  bool operator>(const Date& d)
  {
    if (this->_year > d._year)
    {
      return true;
    }
    else if (this->_year == d._year)
    {
      if (this->_month > d._month)
      {
        return true;
      }
      else if (this->_month == d._month)
      {
        if (this->_day > d._day)
        {
          return true;
        }
      }
    }
    return false;
  }
  // ==运算符重载
  bool operator==(const Date& d)
  {
    return _year==d._year
      && _month==d._month
      && _day==d._day;
  }
  // >=运算符重载
  bool operator >= (const Date& d)
  {
    return (*this > d)||(*this==d);
    /*if (this->_year > d._year)
    {
      return true;
    }
    else if (this->_year == d._year)
    {
      if (this->_month > d._month)
      {
        return true;
      }
      else if (this->_month == d._month)
      {
        if (this->_day >= d._day)
        {
          return true;
        }
      }
    }
    return false;*/
  }
  // <运算符重载
  bool operator < (const Date& d)
  {
    return !(*this >= d);
    /*if (this->_year < d._year)
    {
      return true;
    }
    else if (this->_year == d._year)
    {
      if (this->_month < d._month)
      {
        return true;
      }
      else if (this->_month == d._month)
      {
        if (this->_day < d._day)
        {
          return true;
        }
      }
    }
    return false;*/
  }
  // <=运算符重载
  bool operator <= (const Date& d)
  {
    return !(*this > d);
    /*if (this->_year < d._year)
    {
      return true;
    }
    else if (this->_year == d._year)
    {
      if (this->_month < d._month)
      {
        return true;
      }
      else if (this->_month == d._month)
      {
        if (this->_day <= d._day)
        {
          return true;
        }
      }
    }
    return false;*/
  }
  // !=运算符重载
  bool operator != (const Date& d)
  {
    return !(*this == d);
    /*if (this->_year == d._year)
    {
      if (this->_month == d._month)
      {
        if (this->_day == d._day)
        {
          return true;
        }
      }
    }*/
    return false;
  }
  // 日期-日期 返回天数
  int operator-(const Date& d)
  {
    int flag = 1;
    Date max = *this;
    Date min = d;
    if (*this < d)
    {
      int flag = -1;
      max = d;
      min = *this;
    }
    int n = 0;
    while (min != max)
    {
      ++min;
      ++n;
    }
    return n * flag;
  }
private:
  int _year;
  int _month;
  int _day;
};
int main()
{
  Date d1;//1900/1/1
  Date d2(d1);
  Date d3(2024,1,28);//2024/1/28
  d2.operator=(d3);//d2=d3
  Date d4(2000,1,1);
  Date d5(2000,1,1);
 
  cout << d1.operator<(d3) << endl;
  cout << d3.operator<(d1) << endl;
  
  cout << d2.operator==(d3) << endl;
  cout << d1.operator==(d3) << endl;
 
  cout << d1.operator>(d2) << endl;
  cout << d2.operator>(d1) << endl;
 
  cout << d1.GetMonthDay(2000, 1) << endl;
 
  cout << d4.operator>=(d5) << endl;
  cout << d4.operator>(d5) << endl;
  cout << d4.operator<=(d5) << endl;
  cout << d4.operator<(d5) << endl;
  cout << d4.operator!=(d5) << endl;
  cout << d4.operator!=(d3) << endl;
 
  Date d6(2020, 1, 1);
  d6.operator+=(20);
  Date d7 = d6.operator+(30);
  Date d8(2024, 1, 30);
  d8.operator-=(30);
  Date d9 = d8.operator-(30);
 
  Date d10(2000, 1, 30);
  //Date D1 = d10.operator++(0);//d10++
  //Date D2 = d10.operator--(0);//d10--;
  //Date D3 = d10.operator++();//++d10;
  //Date D4 = d10.operator--();//--d10;
  d10++;
  d10--;
  ++d10;
  --d10;
 
  Date d11(2024, 1, 30);
  Date d12(2024, 8, 1);
  cout << d12 - d11 << endl;
  cout << d11 - d12 << endl;
  return 0;
}

测试截图



相关文章
|
1天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
1天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
1天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
1天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现
|
1天前
|
编译器 C++
【C++类和对象】日期类的实现(上)
【C++类和对象】日期类的实现
|
8天前
|
存储 编译器 C++
c++的学习之路:6、类和对象(2)
c++的学习之路:6、类和对象(2)
22 0
|
8天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
23 0
|
8天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
20 0
|
1天前
|
编译器 C++ 索引
【C++类和对象】拷贝构造与赋值运算符重载(下)
【C++类和对象】拷贝构造与赋值运算符重载
|
1天前
|
存储 编译器 C++
【C++类和对象】拷贝构造与赋值运算符重载(上)
【C++类和对象】拷贝构造与赋值运算符重载