您的位置:首页 > 房产 > 建筑 > C++《日期》实现

C++《日期》实现

2024/12/23 15:21:24 来源:https://blog.csdn.net/2301_80109683/article/details/140404008  浏览:    关键词:C++《日期》实现

C++《日期》实现

  • 头文件
    • 实现文件

头文件

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

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;}elsereturn 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++,++dDate& operator++();Date& operator++(int x);//d--和--dDate& 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;
}``

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com