您的位置:首页 > 编程语言 > C语言/C++

C++,笔试面试,使用C++编程,实现万年历

2016-01-31 21:32 841 查看
要求实现:基本成员函数,日期加减天数,日期与日期相减

#include<iostream>
using namespace std;

class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year), _month(month), _day(day)
{
//cout << "Date(int year, int month, int day)" << endl;
if (!_IsEffective())
{
printf("日期无效,已置为1900-1-1\n");
_year = 1900;
_month = 1;
_day = 1;
}
}
Date(const Date& x)
{
//cout << "Date(Date& x)" << endl;
_year = x._year;
_month = x._month;
_day = x._day;
}
~Date()
{
//cout << "~Date()" << endl;
}

void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool operator>(const Date& d)
{
if (_year > d._year)
return true;
else if (_year == d._year&&_month > d._month)
return true;
else if (_month == d._month&&_day > _day)
return true;
else
return false;
}

bool operator==(const Date& d)
{
if (_year == d._year&&_month == d._month&&_day == d._day)
return true;
return false;
}

bool operator>=(const Date& d)
{
return operator==(d) || operator>(d);
}

bool operator<(const Date& d)
{
return !operator>=(d);
}
bool operator<=(const Date& d)
{
return !operator>(d);
}

Date operator+(const int day)
{
Date tmp = *this;
if (day < 0)
operator-(-day);
tmp._day += day;
while (tmp._day>tmp._MonthDay())
{
tmp._day -= tmp._MonthDay();
tmp._month++;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}

Date operator+= (const int day)
{
if (day < 0)
{
operator-=(-day);
return *this;
}
*this = this->operator+(day);
return *this;
}

Date operator- (const int day)
{
Date tmp = *this;
if (day < 0)
operator+(-day);
tmp._day -= day;
while (tmp._day<=0)
{
tmp._month--;
tmp._day += tmp._MonthDay();
if (tmp._month <1)
{
tmp._year--;
tmp._month = 12;
}
}
return tmp;
}
Date operator-= (const int day)
{
if (day < 0)
{
operator+=(-day);
return *this;
}
*this = this->operator-(day);
return *this;
}

Date operator++()//前置++
{
Date tmp = *this;
operator+=(1);
return tmp;
}
Date operator++(int)//后置++
{
operator+=(1);
return *this;
}

Date operator--()//前置--
{
Date tmp = *this;
operator-=(1);
return tmp;
}
Date operator--(int)//后置--
{
operator-=(1);
return *this;
}

int operator-(const Date& d)
{
int days = _day;
Date tmp = d;
if (*this < tmp)
return -tmp.operator-(*this);
while (*this>tmp)
{
days += tmp._MonthDay();
tmp._month++;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return days - tmp._day;
}

private:
bool _IsLeapYear()
{
if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
return true;
else
return false;
}

int _MonthDay()
{
int monthdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (_IsLeapYear() == 1&&_month == 2)//不是二月没有必要改变到29(算是一个优化)
monthdays[2] += 1;
return monthdays[_month];
}
int _IsEffective()
{
if (_month > 0 && _month < 13 && _day > 0 && _day <= _MonthDay())
return 1;
else
return 0;
}

private:
int _year;
int _month;
int _day;
};

void Test1()
{
Date d1(2018, 2, 20);
Date d2(2016, 2, 20);
Date d3(2016, 2, 30);
printf("%d\n", d1 > d2);
printf("%d\n", d1 < d2);
printf("%d\n", d1 >= d2);
printf("%d\n", d1 <= d2);
printf("%d\n", d1 == d2);
d1.Display();
d3.Display();
(d1 + 8).Display(); //+(day)
(d1 + 9).Display(); //+(day)
(d1 + 10).Display(); //+(day)
(d2 + 10).Display(); //+(day)
(d2 + 31).Display(); //+(day)
printf("\n");

d1++;  //++
d1.Display();
++d1;  //++
d1.Display();
d1 += 10;//+=
d1.Display();
d2 += 10;
d2.Display();
printf("\n");

d1--;  //++
d1.Display();
--d1;  //++
d1.Display();
d1 -= 10;//+=
d1.Display();
d2 -= 10;
d2.Display();
printf("\n");
d1 += -33;
d1.Display();
d1 -= -33;
d1.Display();
printf("\n");

cout << "d1 - d2 = " << d1 - d2 << endl;
cout << "d1 - d2 = " << d2 - d1 << endl;
}

int main()
{
Test1();
system("pause");
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 万年历 笔试面试