设计一个Date类,如下:完善各方法的实现,并对方法进行简单的应用演示
#include
<iostream>
#include
<fstream>
#include
<cassert>
using namespace
std;
class
Date
{
public
:
Date(
int
yy
= 1900,
int
mm
= 1,
int
dd
= 1);
virtual
~Date();
//得到年月日
int
Getyear() {
return
year; }
int
Getmonth() {
return
month; }
int
Getday() {
return
day; }
//设置日期
void
setDate();
void
setDate(
int
yy
,
int
mm
,
int
dd
);
int
Betweendays(
const
Date
&
d
)
const
;
virtual void
showdate();
int
DaysofMonth(
int
year
,
int
month
)
const
;
Date
Changeofdays(
int
days
);
int
Getmonthday(
int
year
,
int
month
);
bool
Isvalid();
//bool Isleap();
string
ItoA(
int
kk
);
//日期加减days个天数单目
Date
operator+=
(
int
days
);
Date
operator-=
(
int
days
);
Date
operator++
();
Date
operator++
(
int
);
Date
operator--
();
Date
operator--
(
int
);
Date
operator+
(
int
days
);
Date
operator-
(
int
days
);
//重载运算符双目
friend long
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
);
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
ostream
&
operator<<
(
ostream
&
out
,
const
Date
&
d
);
friend
fstream
&
operator>>
(
fstream
&
in
,
Date
&
d
);
friend
ofstream
&
operator<<
(
ofstream
&
myout
,
Date
&
d
);
private
:
int
year, month, day;
};
#include <iostream>
#include <fstream>
#include <cassert>
#include <cmath>
#include <string>
using namespace std;
//定义一个Date类
class Date {
public:
//构造函数
Date(int yy = 1900, int mm = 1, int dd = 1);
Date(const Date& d) { m_year = d.m_year, m_month = d.m_month, m_day = d.m_day; };
//析构函数
virtual ~Date() {};
//获取年月日
int Getyear() { return m_year; };
int Getmonth() { return m_month; };
int Getday() { return m_day; };
//设置日期
void setDate() { m_year = 1900, m_month = 1, m_day = 1; };
void setDate(int yy, int mm, int dd);
int Betweendays(const Date& d) const;
//virtual 关键字 虚拟函数
virtual void showdate() { cout << m_year << "-" << m_month << "-" << m_day << endl; };
//返回一个月有多少天
int DaysofMonth(int year, int month) const; //const关键字 表示常函数,不能对数据成员进行修改
int Getmonthday(int year, int month);
//对Date的加减 单位为天
Date Changeofdays(int days);
//bool Isvalid();
//检查是否为闰年
friend bool isleap(int year);
// itoa 将整型转换为字符串
string ItoA(int kk);
//日期加减days个天数
Date operator+=(int days);
Date operator-=(int days);
Date operator++();
Date operator++(int);
Date operator--();
Date operator--(int);
Date operator+(int days);
Date operator-(int days);
//重载运算符双目
friend long 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);
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 ostream& operator << (ostream& cout, const Date& d);
friend fstream& operator >> (fstream& in, Date& d); //读写操作
friend ofstream& operator<< (ofstream& myout, Date& d); //将内容写入文件中
private:
int m_year, m_month, m_day;
};
Date::Date(int yy, int mm, int dd) {
if (yy >= 1900 && mm > 0 && mm < 13 && dd > 0 && dd < DaysofMonth(yy, mm)) {
m_year = yy;
m_month = mm;
m_day = dd;
}
else {
m_year = 1900;
m_month = 1;
m_day = 1;
cout << "您输入的数据不合法!已将日期置为1900-01-01" << endl;
}
}
void Date::setDate(int yy, int mm, int dd) {
if (yy >= 1900 && mm > 0 && mm <= 12 && dd > 0 && dd <= DaysofMonth(yy, mm)) {
m_year = yy;
m_month = mm;
m_day = dd;
}
else {
m_year = 1900;
m_month = 1;
m_day = 1;
cout << "您输入的数据不合法!已将日期置为1900-01-01" << endl;
}
}
bool isleap(int year) {
//能被4整除且不能被100整除,或能被400整除的为闰年
return ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0 ) ? 1 : 0;
}
int Date::DaysofMonth(int year, int month) const {
if (month == 2)
if (isleap(year))
return 29;
else
return 28;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
};
int Date::Getmonthday(int year, int month) {
if (month == 2)
if (isleap(year))
return 29;
else
return 28;
//小于等于7月 单数月有31天, 大于7月 双数月有31天
if (month <= 7) {
if (month % 2 == 1)
return 31;
}
if (month > 7)
if (month % 2 == 0)
return 31;
return 30;
}
int Date::Betweendays(const Date & d)const{
int td1=0, td2=0;
//如果年份月份相等 天数相减
if (m_year == d.m_year && m_month == d.m_month)
{
return abs(m_day - d.m_day);
}
//计算思路:以小的年份为基准线,计算出大年份到小年份的天数加上大年份当年的天数,再减去小年份当年的天数,即为两个日期间隔的天数
//两个相邻的天数间隔为1
int yearsub = 0, lowyear, j;
//计算小年份到大年份的天数
for (lowyear = (m_year < d.m_year ? m_year : d.m_year) , j = lowyear; lowyear < j + abs(m_year - d.m_year); lowyear++) {
if (isleap(lowyear))
yearsub += 366;
else
yearsub += 365;
}
//计算所在年份已有天数
for (int i = m_month - 1; i > 0; i--)
td1 += DaysofMonth(m_year, i);
td1 += m_day;
for (int i = d.m_month - 1; i > 0; i--)
td2 += DaysofMonth(d.m_year, i);
td2 += d.m_day;
//大年份的天数减小年份的天数即为间隔天数
if (m_year > d.m_year) {
return yearsub + td1 - td2;
}
else {
return yearsub + td2 - td1;
}
}
Date Date::Changeofdays(int days) {
// 加法
if (days > 0) {
while (days--) {
m_day++;
//天数满一个月 进位
if (m_day > DaysofMonth(m_year, m_month)) {
m_day = 1;
m_month++;
}
if (m_month > 12) {
m_month = 1;
m_year++;
}
}
}
// 减法
else{
while (days++) {
m_day--;
//天数小于1 借位 月份减1
if (m_day < 1) {
m_month--;
//月份小于1 借位 年份减1
if (m_month < 1) {
m_year--;
m_month = 12;
}
m_day = DaysofMonth(m_year,m_month);
}
}
}
return *this;
}
Date Date::operator+=(int days) {
Changeofdays(days);
return *this;
}
Date Date::operator-=(int days) {
Changeofdays(-days);
return *this;
}
Date Date::operator++() {
Changeofdays(1);
return *this;
}
//后置++ 先保存当前值 再加一
Date Date::operator++(int) {
Date t;
t = *this;
Changeofdays(1);
return t;
}
Date Date::operator--() {
Changeofdays(-1);
return *this;
}
Date Date::operator--(int) {
Date t;
t = *this;
Changeofdays(-1);
return t;
}
Date Date::operator+(int days) {
Date d = *this;
d.Changeofdays(days);
return d;
}
Date Date::operator-(int days) {
Date d = *this;
d.Changeofdays(-days);
return d;
}
long operator-(const Date& d1, const Date& d2) {
if(d1 > d2)
return d1.Betweendays(d2);
else
return -d1.Betweendays(d2);
}
//先比较年份 再比较月份 最后比较天数
bool operator >(const Date& d1, const Date& d2) {
Date dd1 = d1, dd2 = d2;
if (dd1.Getyear() != dd2.Getyear())
return (dd1.Getyear() > dd2.Getyear());
if (dd1.Getmonth() != dd2.Getmonth())
return (dd1.Getmonth() > dd2.Getmonth());
return dd1.Getday() > dd2.Getday();
}
bool operator <(const Date& d1, const Date& d2) {
Date dd1 = d1, dd2 = d2;
if (dd1.Getyear() != dd2.Getyear())
return (dd1.Getyear() < dd2.Getyear());
if (dd1.Getmonth() != dd2.Getmonth())
return (dd1.Getmonth() < dd2.Getmonth());
return dd1.Getday() < dd2.Getday();
}
bool operator >=(const Date& d1, const Date& d2) {
Date dd1 = d1, dd2 = d2;
if (dd1.Getyear() != dd2.Getyear())
return (dd1.Getyear() >= dd2.Getyear());
if (dd1.Getmonth() != dd2.Getmonth())
return (dd1.Getmonth() >= dd2.Getmonth());
return dd1.Getday() >= dd2.Getday();
}
bool operator <=(const Date& d1, const Date& d2) {
Date dd1 = d1, dd2 = d2;
if (dd1.Getyear() != dd2.Getyear())
return (dd1.Getyear() <= dd2.Getyear());
if (dd1.Getmonth() != dd2.Getmonth())
return (dd1.Getmonth() <= dd2.Getmonth());
return dd1.Getday() <= dd2.Getday();
}
bool operator ==(const Date& d1, const Date& d2) {
// 如果日期相等 相隔的天数为 0
return !d1.Betweendays(d2);
}
bool operator !=(const Date& d1, const Date& d2) {
return d1.Betweendays(d2);
}
ostream& operator<< (ostream& cout, const Date& d) {
cout << d.m_year << "-" << d.m_month << "-" << d.m_day;
return cout;
}
fstream& operator >> (fstream& in, Date& d) {
return in;
}
ofstream& operator<< (ofstream& myout, Date& d) {
myout << d.m_year << "-" << d.m_month << "-" << d.m_day;
return myout;
}
int main() {
Date date2(2019,9,1);
Date date1 = date2;
Date guoqing(2022, 10, 1), birthday;
date1.setDate(2022, 3, 1);
cout << "------调用showdate()函数输出日期------" << endl;
cout << "date1 : ";
date1.showdate();
cout << "date2 : ";
date2.showdate();
cout << "birtnday : ";
birthday.showdate();
cout << "-----调用setdate()函数给birthday设置日期为1999-1-11 -----" << endl;
birthday.setDate(1999, 1, 11);
cout << "------调用<<运算符重载输出日期------" << endl;
cout << "birthday : " << birthday << endl;
cout << "guoqing : " << guoqing << endl;
cout << "------调用date1.DaysofMonth(date1.Getyear(),date1.Getmonth()) 函数获得data1所在月份的天数------" << endl;
cout << date1.Getyear()<<"年"<<date1.Getmonth()<<"月的天数为:" << date1.DaysofMonth(date1.Getyear(), date1.Getmonth()) << endl;
cout << "------调用date1.DaysofMonth(2022,1) 函数获得2022年1月的天数------" << endl;
cout << "2022年1月的天数为:" << date1.Getmonthday(2022, 1) << endl;
cout << "------调用date1.Betweendays(date2) 函数获得date1与date2间隔的天数------" << endl;
cout << "date1与date2间隔的天数为:" << date1.Betweendays(date2) << endl;
cout << "------调用date2.Changeofdays(1096) 函数对date2进行加365天操作------" << endl;
date2.Changeofdays(1096);
cout << "date2加1096天的日期为:" << date2 <<endl;
cout << "------调用date2.Changeofdays(365) 函数对date2进行减365天操作------" << endl;
date2.Changeofdays(-365);
cout << "date2减365天的日期为:" << date2 << endl << endl << endl;
cout << "birthday : " << birthday << endl;
cout << "guoqing : " << guoqing << endl;
cout << "date1 : " << date1 << endl;
cout << "date2 : " << date2 << endl;
cout << "------------运算符重载-----------" << endl << endl;
guoqing += 7;
cout << "guoqing += 7天 = " << guoqing << endl;
date2 -= 244;
cout << "date2 -= 244天 = " << date2 << endl;
cout << "++date2 = " << ++date2 << endl;
cout << "date2++ = " << date2++ << endl;
cout << "date2 = " << date2 << endl;
cout << "--guoqing = " << --guoqing<< endl;
cout << "guoqing-- = " << guoqing-- << endl;
cout << "guoqing = " << guoqing << endl;
birthday = date1 + 96;
cout << "birthday = date1 + 96天 = " << date1 << " + 96天 = " << birthday << endl;
guoqing = date2 - 93;
cout << "guoqing = date2 - 93天 = " << date2 << " - 93天 = " << guoqing << endl;
date2.setDate(2019, 12, 31);
guoqing.setDate(2020, 1, 1);
cout << "guoqing - date2 = " << date2 << " - " << guoqing << " = " << date2 - guoqing<< endl;
cout << "------------日期大小比较-----------" << endl << endl;
cout << "date1 :" << date1 << endl;
cout << "date2 :" << date2 << endl;
cout << "guoqing :" << guoqing << endl;
cout << "birthday :" << birthday << endl;
cout << endl << "比较date1与date2的日期" << endl;
if (date1 > date2)
cout << date1 << " > " << date2 << endl;
if (date1 < date2)
cout << date1 << " < " << date2 << endl;
if (date1 >= date2)
cout << date1 << " >= " << date2 << endl;
if (date1 <= date2)
cout << date1 << " <= " << date2 << endl;
if (date1 == date2)
cout << date1 << " == " << date2 << endl;
if (date1 != date2)
cout << date1 << " != " << date2 << endl;
cout << endl << "比较guoqing与birthday的日期" << endl;
if (guoqing > birthday)
cout << guoqing << " > " << birthday << endl;
if (guoqing < birthday)
cout << guoqing << " < " << birthday << endl;
if (guoqing >= birthday)
cout << guoqing << " >= " << birthday << endl;
if (guoqing <= birthday)
cout << guoqing << " <= " << birthday << endl;
if (guoqing == birthday)
cout << guoqing << " == " << birthday << endl;
if (guoqing != birthday)
cout << guoqing << " != " << birthday << endl;
cout << endl <<"------------文件操作重载-----------" << endl << endl;
ofstream ofs;
ofs.open("myout.txt", ios::out);
ofs << "date1的日期为: " << endl;
ofs << date1 << endl;
ofs.close();
fstream fs, fs1;
fs.open("myout.txt", ios::in);
if (!fs.is_open()) {
cout << "文件打开失败!" << endl;
}
else
{
string buf;
cout << "读取myout.txt" << endl;
while (getline(fs, buf))
cout << buf << endl;
}
fs.close();
fs1.open("myout1.txt", ios::out);
fs1 << date2 << endl;
fs1.close();
return 0;
}