完成日期类的实现
来源:互联网 发布:四川校校通网络平台 编辑:程序博客网 时间:2024/06/07 21:51
#include<stdio.h>
#include<assert.h>
#include<iostream>
#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;
}
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"
#include"Date.h"
int main()
{
test();
return 0;
}
{
test();
return 0;
}
阅读全文
0 0
- 完成日期类的实现
- C++完成日期类的实现--Date
- C++完成日期类的实现——Date
- C++完成日期类
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 自动完成的实现
- 代码完成的实现
- 自己实现的日期类
- C++:日期类的实现
- C++日期类的实现
- c++日期类的实现
- C++日期类的实现
- 三级缓存
- leetcode 119
- maven
- Python学习教程(一)——初步认识
- ObjectAnimation动画 + ValueAnimation动画监听
- 完成日期类的实现
- 使用SharedPreferences进行数据的存储实例:增删改查联系人界面
- test
- Tried to assign property OutStationNo:java.lang.Integer to bean of class
- 使用 MQTTnet 快速实现 MQTT 通信
- GouWu
- 趣图:为了说服经理重写代码
- java定时器-固定时间间隔内执行操作
- Sql- Group By ,having 分组查询