【C++】日期类
2016-11-09 22:10
417 查看
用C++实现一个日期类,完成以下接口
class Date{
public:
Date();
Date(const Date& d);
Date& operator=(const Date& d);
Date operator+(int day);
Date& operator++();
Date operator++(int);
Date operator-(int day);
int operator-(const Date& d);
Date& operator--();
Date operator--(int);
bool operator>(const Date& d);
bool operator<(const Date& d);
bool operator==(const Date& d);
bool operator!=(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
private:
int year;
int month;
int day;
static int op[13];
};
这里面涉及到了运算符重载,构造以及拷贝构造函数
注意输出和输入运算符需要重载成友元函数,这一点在很久之前就讲解过~
实现代码
#include<iostream> using namespace std; class Date { public: Date(int _year = 1900, int _month = 1, int _day = 1) :year(_year), month(_month), day(_day) {} Date(const Date& d) { this->day = d.day; this->month = d.month; this->year = d.year; } Date& operator=(const Date& d); Date operator+(int day); Date& operator++(); Date operator++(int); Date operator-(int day); int operator-(const Date& d); Date& operator--(); Date operator--(int); bool operator>(const Date& d); bool operator<(const Date& d); bool operator==(const Date& d); bool operator!=(const Date& d); bool operator>=(const Date& d); bool operator<=(const Date& d); friend ostream& operator<<(ostream& _cout, const Date& d); friend istream& operator>>(istream& _cin, Date& d); private: int year; int month; int day; static int op[13]; }; int Date::op[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; bool IsLeapYear(int year) { if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { return true; } return false; } Date& Date::operator=(const Date& d) { this->day = d.day; this->month = d.month; this->year = d.year; return *this; } Date Date::operator+(int day) { int add_year = 0; int add_month = 0; int tmp_month = this->month; while ((day + this->day)>(op[tmp_month] + (tmp_month == 2 && IsLeapYear(this->year + add_year))))//(20+40)>30 { add_month++; if (add_month + this->month>12) { add_month -= 12; } day -= (op[month] + (tmp_month == 2 && IsLeapYear(this->year + add_year))); if (tmp_month != 12) { tmp_month++; } else { add_year++; tmp_month = 1; } } this->day += day; this->month += add_month; this->year += add_year; return (*this); } Date& Date::operator++()//前置++ { *this = *this + 1; return *this; } Date Date::operator++(int)//后置++ { Date old(*this); ++(*this); return old; } Date Date::operator-(int day) { int sub_year = 0; int tmp_month = this->month; while (day - this->day >= 0)//1>1 { tmp_month--; if (tmp_month == 0) { tmp_month = 12; sub_year++; } day -= (op[tmp_month] + (tmp_month == 2 && IsLeapYear(this->year - sub_year))); if ((tmp_month == 2 && IsLeapYear(this->year - sub_year)) == true) { day--; } } this->year -= sub_year; this->month = tmp_month; this->day -= day; return (*this); } Date& Date::operator--()//前置-- { *this = *this - 1; return (*this); } Date Date::operator--(int)//后置-- { Date old(*this); --(*this); return old; } int Date::operator-(const Date& d) { size_t count = 0; Date Temp = d; if (Temp>*this) { while (Temp != *this) { --Temp; count++; } } else { while (Temp != *this) { ++Temp; count++; } } return count; } bool Date::operator>(const Date& d) { if (this->year>d.year) { return true; } else if (this->year == d.year && this->month > d.month) { return true; } else if (this->year == d.year && this->month == d.month && this->day > d.day) { return true; } else { return false; } } bool Date::operator<(const Date& d) { if (this->year<d.year) { return true; } else if (this->year == d.year && this->month < d.month) { return true; } else if (this->year == d.year && this->month == d.month && this->day < d.day) { return true; } else { return false; } } bool Date::operator==(const Date& d) { if (this->year == d.year && this->month == d.month && this->day == d.day) { return true; } else { return false; } } bool Date::operator!=(const Date& d) { if (this->year != d.year || this->month != d.month || this->day != d.day) { return true; } else { return false; } } bool Date::operator>=(const Date& d) { if (this->year >= d.year) { return true; } else if (this->year == d.year && this->month >= d.month) { return true; } else if (this->year == d.year && this->month == d.month && this->day >= d.day) { return true; } else { return false; } } bool Date::operator<=(const Date& d) { if (this->year <= d.year) { return true; } else if (this->year == d.year && this->month <= d.month) { return true; } else if (this->year == d.year && this->month == d.month && this->day <= d.day) { return true; } else { return false; } } ostream& operator<<(ostream &_cout, const Date &d) { _cout << d.year << "-" << d.month << "-" << d.day; return _cout; } istream& operator>>(istream& _cin, Date& d) { _cin >> d.year >> d.month >> d.day; return _cin; }
相关文章推荐
- C++ 获取当前日期时间 毫秒级
- c++中日期和时间的使用详解
- C++中的日期和时间
- C/C++中的日期和时间 TIME_T与STRUCT TM转换(转载)
- C/C++中的日期和时间 time_t与struct tm
- c++匿名函数与日期时间
- C/C++完成两个日期之间相隔天数的计算
- c++当前日期和时间
- C/C++中的日期和时间 time_t与struct tm转换
- C/C++中的日期和时间
- C/C++-星系炸弹(计算日期)
- ARM - Linux嵌入式C/C++各种资料分享【更新日期:2012/04/24】
- C/C++中怎样获取日期和时间
- Work with dates and times(C和C++程序设计中使用日期和时间)
- C++ 习题 输出日期时间--友元函数
- c++日期类的功能实现
- C++实现日期类
- (zt)C/C++中的日期和时间 time_t与struct tm转换
- C/C++中的日期和时间 TIME_T与STRUCT TM转换
- C/C++中怎样获取日期和时间