C++《日期》实现

cccccc语言我来了 2024-08-01 11:05:05 阅读 76

C++《日期》实现

头文件实现文件

头文件

在该文件中是为了声明函数和定义类成员

<code>using namespace std;

class Date

{

friend ostream& operator<<(ostream& out, const Date& d);//友元

friend istream& operator>>(istream& cin, Date& d);//友元<这是为了把定义在类外面的函数,能够访问到类中的成员>

public:

Date(int year = 1990, int month = 1, int days = 1);

void print();

int Getdaymonth(int year,int month)//日期获取

{

int getday[13] = { -1,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;

}

else

return getday[month];

}

bool cheakdate();

//d1+=/+

Date& operator+=(int days);

Date operator+(int days);

bool operator<(const Date& d)const;

bool operator>(const Date& d)const;

bool operator==(const Date& d)const;

bool operator<=(const Date& d)const;

bool operator>=(const Date& d)const;

bool operator!=(const Date& d)const;

//d1-= 和 -

Date& operator-=(int days);

Date operator-(int days);

//d++,++d

Date& operator++();

Date& operator++(int x);

//d--和--d

Date& operator--();

Date operator--(int);

//d1-d2(俩日期相减)

int operator-(const Date& d)const;

private:

int _year;

int _month;

int _days;

};

ostream& operator<<(ostream& out,const Date& d);//流输出

istream& operator>>(istream& cin, Date& d);//流提取

实现文件

这里是对头文件外部函数中的成员函数的逐一实现:

#include"标头.h"

bool Date::cheakdate()

{

if (_month < 0 || _month>12)

{

return false;

}

else

{

return true;

}

}

Date::Date(int year,int month,int days)

{

_year = year;

_month = month;

_days = days;

if (!cheakdate())

{

cout << "非法日期" << endl;

}

}

void Date::print()

{

cout << _year << "-" << _month << "-" << _days << endl;

}

Date& Date::operator+=(int days)

{

if (days < 0)

{

return *this -= -days;

}

_days += days;

while (_days > Getdaymonth(_year, _month))

{

_days -= Getdaymonth(_year, _month);

_month++;

if (_month == 13)

{

_year++;

_month = 1;

}

}

return *this;

}

Date Date::operator+(int day)

{

Date tmp = *this;

tmp += day;

return tmp;

}

bool Date:: operator<(const Date& d)const

{

if (_year < d._year)

{

return true;

}

else if (_year == d._year)

{

if (_month < d._month)

{

return true;

}

else if (_month == d._month)

{

return _days < d._days;

}

}

return false;

}

bool Date::operator>=(const Date& d)const

{

return !(*this < d);

}

bool Date::operator==(const Date& d)const

{

return _year == d._year && _month == d._month && _days == d._days;

}

bool Date:: operator<=(const Date& d)const

{

return *this < d || *this == d;

}

bool Date::operator>(const Date& d)const

{

return !(*this <= d);

}

bool Date:: operator!=(const Date& d)const

{

return !(*this == d);

}

Date& Date::operator-=(int days)

{

if (days < 0)

{

return *this += -days;

}

_days -= days;

while (_days<= 0)

{

_month--;

if (_month == 0)

{

_year--;

_month = 12;

}

_days += Getdaymonth(_year, _month);

}

return *this;

}

Date Date::operator-(int days)

{

Date tmp = *this;

tmp -= days;

return tmp;

}

Date& Date:: operator++()

{

return *this += 1;

}

Date& Date:: operator++(int x)

{

Date tmp = *this;

*this += 1;

return tmp;

}

Date& Date::operator--()

{

*this -= 1;

return *this;

}

Date Date::operator--(int)

{

Date tmp = *this;

*this -= 1;

return tmp;

}

int Date::operator-(const Date& d)const

{

Date max = *this;

Date min(d);

int flag = 1;

int count = 0;

if (*this <d)

{

max = d;

min = *this;

flag = -1;

}

while (min != max)

{

count++;

min++;

}

return count * flag;

}

ostream& operator<<(ostream& out, const Date& d)

{

out << d._year << "/" << d._month << "/" << d._days << endl;

return out;

}

istream& operator>>(istream& cin, Date& d)

{

while (1)

{

cout << "请依次输入数据>:";

cin >> d._year >> d._month >> d._days;

if (!d.cheakdate())

{

cout << "输入日期非法:";

d.print();

cout << "请重新输入:";

}

else

{

break;

}

}

return cin;

}``



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。