Implement the operator ++(prefix), --(prefix), ++(postfix), --(postfix), in the class Date
class Date
{
public:
Date(int y=0, int m=1, int d=1);
static bool leapyear(int year);
int getYear() const;
int getMonth() const;
int getDay() const;
friend ostream& operator<<(ostream&, const Date&);
// add any member you need here
};
You implementation should enable the usage like this:
void f()
{
Date date(2004,2,28);
cout << "date = " << date << endl;
cout << "++date = " << ++date << endl;
cout << "--date = " << --date << endl;
cout << "date++ = " << date++ << endl;
cout << "date-- = " << date-- << endl;
cout << "date = " << date << endl;
}
The output of f() should be:
date = 2004-2-28
++date = 2004-2-29
--date = 2004-2-28
date++ = 2004-2-28
date-- = 2004-2-29
date = 2004-2-28
提交时不需要提交operator << 重载。
#include<iostream>
using namespace std;
class Date
{
public:
Date(int y=0, int m=1, int d=1);
static bool leapyear(int year);
int getYear() const;
int getMonth() const;
int getDay() const;
Date operator++();
Date operator++(int x);
Date operator--();
Date operator--(int x);
friend ostream& operator<<(ostream&, const Date&);
// add any member you need here
private:
int year;
int month;
int day;
};
#include<iostream>
#include<string>
#include"1001.h"
using namespace std;
ostream& operator<<(ostream& os, const Date& date)
{
os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
return os;
}
/*
f3_1() test for:
operator
++(prefix), --(prefix),
++(postfix), --(postfix),
*/
void f3_1(const Date& d)
{
Date date = d;
cout << "date = " << date << endl;
cout << "++date = " << ++date << endl;
cout << "--date = " << --date << endl;
cout << "date++ = " << date++ << endl;
cout << "date-- = " << date-- << endl;
cout << "date = " << date << endl;
cout << endl;
}
void f3()
{
Date date1, date2(2004,2,28), date3(2007,2,28),date4(2000,2,28),date5(99,12,31);
f3_1(date1);
f3_1(date2);
f3_1(date3);
f3_1(date4);
f3_1(date5);
}
int main()
{
f3();
//system("PAUSE");
return 0;
}
Date::Date(int y, int m, int d):year(y),month(m),day(d){}
bool Date::leapyear(int year)
{
if((year%400)==0||(year%4==0&&year%100!=0))
return true;
return false;
}
int Date::getYear() const
{
return year;
}
int Date::getMonth() const
{
return month;
}
int Date::getDay() const
{
return day;
}
Date Date::operator++()
{
day++;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year))
{
if(day>b[month]){
day=1;month++;
}
if(month>12){
month=1;year++;day=1;
}
}
else{
if(day>a[month]){
day=1;month++;
}
if(month>12){
month=1;year++;day=1;
}
}
return *this;
}
Date Date::operator++(int x)
{
Date temp(year,month,day);
day++;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year))
{
if(day>b[month]){
day=1;month++;
}
if(month>12){
month=1;year++;day=1;
}
}
else{
if(day>a[month]){
day=1;month++;
}
if(month>12){
month=1;year++;day=1;
}
}
return temp;
}
Date Date::operator--()
{
day--;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year))
{
if(day==0)
{
month--;
day=b[month];
}
if(month==0)
{
year--;
month=12;
day=31;
}
}
else
{
if(day==0)
{
month--;
day=a[month];
}
if(month==0)
{
year--;
month=12;
day=31;
}
}
return *this;
}
Date Date::operator--(int x)
{
Date temp(year,month,day);
day--;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year))
{
if(day==0)
{
month--;
day=b[month];
}
if(month==0)
{
year--;
month=12;
day=31;
}
}
else{
if(day==0)
{
month--;
day=a[month];
}
if(month==0)
{
year--;
month=12;
day=31;
}
}
return temp;
}
心得:
(1)如果在类的声明构造函数里面有了默认参数,那么在定义的时候就不能够再写出参数了;
(2)++的思路是:先让日期加一,然后判断是否是闰年,然后判断日期数目是否大于本月份的日期数目,然后再判断月份数目是否大于12;
if(leapyear(year))
{
if(day>b[month]){
day=1;month++;
}
if(month>12){
month=1;year++;day=1;
}
}
(2) - -的思路是:先让日期减一,然后判断是否是闰年,然后判断日期数目是否等于0,然后再判断月份数目是否等于0;
if(leapyear(year))
{
if(day==0)
{
month--;
day=b[month];
}
if(month==0)
{
year--;
month=12;
day=31;
}
}