- Day5:面试必考题目
- 日期类的实现(手写或者OJ)
- 1.题目要求内容
- 2.日期类代码实现
- 3.日期类代码合理运行结果(排版自排)
日期类几乎是每个项目必有的东西,企业常考日期类的实现问题
通过写日期类考核对于基本语法的考核与思考问题的缜密性
日期类的实现(手写或者OJ) 1.题目要求内容
2.日期类代码实现主要实现:
1.日期的加减
2.运算符重载
3.自定义拷贝构造、全缺省拷贝构造
4.测试类
内容划分:
1.Date.h
2.Date.cpp
3.Test.cpp
//Date.h #pragma once #includeusing std::cin; using std::cout; using std::endl; class Date { public: //判断平年 bool isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; else return false; } // 获取某年某月的天数 int GetMonthDay(int year, int month) { //assert(year>0 && month>0 && month<13); //简便时间表示法 static int Days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (year > 0 && month > 0 && month < 13)//if也可以用断言判断 { if (month == 2 && isLeapYear(year)) return 29; else return Days[month]; } } // 全缺省的构造函数---只能写在类中才算构造函数 Date(int year = 2022, int month = 1, int day = 1) { if (year > 0 && month > 0 && month < 13 && day>0 && day <= GetMonthDay(year, month)) { _year = year; _month = month; _day = day; } } // 拷贝构造函数 d2(d1) Date(const Date& d) { if (*this != d) { _year = d._year; _month = d._month; _day = d._day; } } // 赋值运算符重载---自主生成可写可不写,除非自定义它 // d2 = d3 -> d2.operator=(&d2, d3) //Date& operator=(const Date& d) const; //析构函数 ~Date();---自主生成可写可不写 // 日期+=天数 Date& operator+=(int day); // 日期+天数 Date operator+(int day); // 日期-天数 Date operator-(int day); // 日期-=天数 Date& operator-=(int day); // 前置++ Date& operator++(); // 后置++ Date operator++(int); // 后置-- Date operator--(int); // 前置-- Date& operator--(); // >运算符重载 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); // 日期-日期 返回天数 int operator-(const Date& d); // 打印日期 void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; };
//Date.cpp
#include"Date.h"
// 日期+=天数
Date& Date::operator+=(int day)
{
if (day < 0)
return *this -= -day;
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
if (day < 0)
return *this += -day;
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
// 后置--
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
// 前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//日期加减和前后置总结:
//先写-=和+=然后互用实现其他操作
//前后置注意 后置加参数,前置不加参数,后置用个Date ret来中转*this
// >运算符重载(实现)
bool Date::operator>(const Date& d)
{
if (_year > d._year ||
_year==d._year &&_month > d._month ||
_year>d._year && _month==d._month &&_day > d._day)
{
return true;
}
else
return false;
}
// ==运算符重载(实现)
bool Date::operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
// >=运算符重载(互用)
bool Date::operator >= (const Date& d)
{
return *this > d || *this == d;
}
// <运算符重载(互用)
bool Date::operator < (const Date& d)
{
return !(*this>=d);
}
// <=运算符重载(互用)
bool Date::operator <= (const Date& d)
{
//写成这个 return !(*this>d); 缺少了等于情况
return *this < d || *this == d;
}
// !=运算符重载(互用)
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
//运算符重载总结:
//实现两个,其他互用实现,注意>= 和<=那个等号判断,互用的运算符重载可以用inline提高效率
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
int flag = 1;
Date max = *this;//表示大数
Date min = d;//表示小数
if (*this < d)//大数小数自动换,保持max和min的正确
{
min = *this;
max = d;
flag = -1;
}
int n = 0;//n计数,一直让n从小数加到大数从而得到天数差
while (min != max)
{
++n;
++min;
}
return n * flag;//×flag是为了保障打出来的天数的正负
}
#include"Date.h"
void Test1()//测试运算符重载
{
Date d1(2022, 1, 1);
Date d2(2022, 2, 1);
Date d3(2022, 2, 1);
cout << "d1d2:" << (d1 > d2) << endl;
cout << "d1=d3:" << (d1 == d3) << endl;
cout << "d2=d3:" << (d2 == d3) << endl;
cout << "d1<=d3:" << (d1 <= d3) << endl;
cout << "d1>=d2:" << (d1 >= d2) << endl;
cout << endl;
}
void Test2()//测试天数的加减
{
Date d1(2022, 1, 1);
Date d2 = d1 + 15;
cout << "d1的日期:";
d1.Print();
cout << "d2的日期(d1+15):";
d2.Print();
cout << endl;
}
void Test3()//测试日期-日期=天数
{
Date d1(2022, 1, 5);
Date d2(2022, 2, 2);
Date d3(2022, 5, 1);
Date d4(2022, 3, 1);
cout << "d1和d2的天数差:";
cout << "d1-d2=" << (d1 - d2)<
Date d1(2022, 1, 1);
Date d2(2022, 1, 1);
Date d3(2022, 1, 1);
Date d4(2022, 1, 1);
cout<<"d1++(2022,1,1):";
cout << "原d1:";
d1.Print();
d1++;
cout << "d1++后d1:";
d1.Print();
cout<
cout << "测试运算符重载:"<
3.日期类代码合理运行结果(排版自排)



