完成日期类的实现

来源:互联网 发布:四川校校通网络平台 编辑:程序博客网 时间:2024/06/07 21:51
#include<stdio.h>
#include<assert.h>
#include<iostream>
using namespace std;
class  Date
{
public:
 Date(int year = 1990,int month = 1,int day = 1)
 {
  _year = year;
  _month = month;
  _day = day;
 }
 Date(const Date& d)
 {
  this->_year = d._year;
  _month = d._month;
  _day = d._day;
 }
 ~Date()
 {
  //cout << "~Date()" << endl;
 }
 void Display()
 {
  cout << _year << "-" << _month << "-" << _day << endl;
 }
 bool IsInvalid()
 {
  if (_year >= 0
   && _month > 0 && _month < 13
   && _day > 0 && _day <= GetMonthDay())
  {
   return true;
  }
  else
  {
   return false;
  }
 }
 int GetMonthDay()    //获得每个月的天数
 {
  assert(_month > 0 && _month < 13);
  static int monthDays[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 monthDays[_month];
 }
 Date& operator=(const Date& d)
 {
  if (this != &d)        //防止自己给自己赋值
  {
   _year = d._year;
   _month = d._month;
   _day = d._day;
  }
  return *this;
 }
 bool operator>(const Date& d)
 {
  if (this->_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 operator==(const Date& d)
 {
  return _year == d._year
   && _month == d._month
   && _day == d._day;
 }
 bool operator!=(const Date& d)
 {
  return !(*this == d);
 }
 bool operator<(const Date& d)
 {
  return !(*this>d || *this == d);
 }
 bool operator>=(const Date& d)
 {
  return !(*this < d);
 }
 bool operator<=(const Date& d)
 {
  return !(*this > d);
 }
 Date operator+(int day)
 {
  if (day<0)
  {
   return *this - (-day);
  }
  Date tmp(*this);
  tmp._day += day;
  while (tmp._day > tmp.GetMonthDay())  //判断天数是否合法
  {
   tmp._day -= tmp.GetMonthDay();
   tmp._month++;
   if (tmp._month > 12)   // 判断月份是否合法
   {
    tmp._year++;
    tmp._month = 1;
   }
  }
  return tmp;
 }
 Date& operator+=(int day)
 {
  *this = *this + day;
  return *this;
 }
 Date& operator-=(int day)
 {
  *this = *this - day;
  return *this;
 }
 Date& operator--()     //前置
 {
  *this -= 1;
  return *this;
 }
 Date operator--(int)  //后置
 { 
  Date tmp(*this);
  *this -= 1;
  return tmp;
 }
 Date& operator++()
 {
  *this += 1;
  return *this;
 }
 Date operator++(int)
 {
  Date tmp(*this);
  *this += 1;
  return tmp;
 }
 Date operator-(int day)
 {
  if (day < 0)
  {
   return *this + (-day);
  }
  Date tmp(*this);
  tmp._day -= day;
  while (tmp._day <= 0)
  {
   tmp._month--;
   if (tmp._month == 0)
   {
    tmp._year--;
    tmp._month = 12;
   }
   tmp._day += tmp.GetMonthDay();
  }
  return tmp;
 }
 int operator-(const Date& d)
 {
  int tag = 1;
  Date greate(*this);
  Date less(d);
  if (*this < d)
  {
   greate = d;
   less = *this;
   tag = -1;
  }
  int count = 0;
  while (less < greate)
  {
   ++count;
   ++less;
  }
  return count*tag;
 }
private:
 int _year;
 int _month;
 int _day;
};
void test()
{
 Date d1(2015,10,25);
 //d1.Display();
 //d2 = d1;
 //Date d2(d1);
 //Date d2 = d1 - 5000;
 //Date d2 = d1 + 5000;
 //Date d3;
 //d3 = d2 = d1;
 //d2.Display();
 //cout <<(d1.operator>(d2)) << endl;
}
#include<iostream>
#include"Date.h"
int main()
{
    test();
 return 0;
}

原创粉丝点击