当前位置:网站首页>Day5: Interview mandatory programming questions
Day5: Interview mandatory programming questions
2022-08-06 00:30:00 【Slow Just Fast】
Day5:面试必考题目
文章目录
Date classes are a must for almost every project,Enterprises often test the realization of the date class

日期类的实现(handwritten orOJ)
1.The content of the subject requirements
主要实现:
1.日期的加减
2.运算符重载
3.自定义拷贝构造、Full default copy construction
4.测试类
内容划分:
1.Date.h
2.Date.cpp
3.Test.cpp
2.Date class code implementation
//Date.h
#pragma once
#include<iostream>
using 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);
//Simple time notation
static int Days[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (year > 0 && month > 0 && month < 13)//ifYou can also use assertions to judge
{
if (month == 2 && isLeapYear(year))
return 29;
else
return Days[month];
}
}
// 全缺省的构造函数---Constructors can only be written in a class
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;
}
}
// 赋值运算符重载---Self-generated writable or not,unless you customize it
// d2 = d3 -> d2.operator=(&d2, d3)
//Date& operator=(const Date& d) const;
//析构函数 ~Date();---Self-generated writable or not
// 日期+=天数
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 addition and subtraction and pre- and post-summary:
//先写-=和+=Then interoperate to implement other operations
//Pre and post attention Post-add parameters,No parameters are added before,Use a backDate 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); Missing is equal to the situation
return *this < d || *this == d;
}
// !=运算符重载(互用)
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
//运算符重载总结:
//实现两个,Other interoperability implementations,注意>= 和<=That equals judgment,Interoperable operator overloading is availableinline提高效率
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
int flag = 1;
Date max = *this;//represent large numbers
Date min = d;//表示小数
if (*this < d)//Automatic conversion of large and small numbers,保持max和min的正确
{
min = *this;
max = d;
flag = -1;
}
int n = 0;//n计数,一直让nAdd the decimal to the larger number to get the difference in days
while (min != max)
{
++n;
++min;
}
return n * flag;//×flagIt is to ensure the positive and negative of the number of days played
}
#include"Date.h"
void Test1()//测试运算符重载
{
Date d1(2022, 1, 1);
Date d2(2022, 2, 1);
Date d3(2022, 2, 1);
cout << "d1<d2:" << (d1 < d2) << endl;
cout << "d1>d2:" << (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()//Addition or subtraction of test days
{
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)<<endl;
cout << "d3和d4的天数差:";
cout << "d3-d4=" << (d3 - d4)<<endl;
cout << endl;
}
void Test4()//测试 前置后置
{
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<<endl;
cout << "d2--(2022,1,1):";
cout << "原d2:";
d2.Print();
d2--;
cout << "d2--后d2:";
d1.Print();
cout << endl;
cout << "++d3(2022,1,1):";
cout << "原d3:";
++d3;
d3.Print();
cout << "++d3后d3:";
d3.Print();
cout << endl;
cout << "--d4(2022,1,1):";
cout << "原d4:";
--d4;
d4.Print();
cout << "--d4后d4:";
d4.Print();
cout << endl;
}
int main()
{
cout << "测试运算符重载:"<<endl;
Test1();
Test2();
Test3();
Test4();
return 0;
}
3.The date class code runs reasonably(Typesetting)

边栏推荐
猜你喜欢

(3.2) Metasploit kali - the exploit 】 【 basis (under) : MSF terminal using process

Viola-Jones检测器(VJ)---学习笔记

Privacy Computing Basic Component Series - Obfuscation Circuit

物理 NFT 有什么特别之处?

Day4:面试必考选择题

eBay、亚马逊、Lazada、Shopee、速卖通、美客多等跨境电商平台,测评自养号需要满足什么条件?listing如何优化?

隐私计算综述

安全测试-数据安全测试(2)

GPS-based NTP time synchronization server

文档对象模型 DOM ——让JS有能力和网页进行对话
随机推荐
Day6:面试必考选择题
横向联邦学习-梯度安全聚合(二)
【kali-漏洞利用】(3.2)Metasploit基础(下):MSF终端利用过程
和为S的连续正数序列
Teach you the object-src of the CSP series
Taught you how to locate online MySQL lock timeout problem, BaoJiao package
在VR里弹空气吉他?开发者:手势优化有技巧
pod详解
区间DP
[2022 强网杯] house_of_cat 战战兢兢的复现
【LeetCode】209. 长度最小的子数组
从零开始用C#做产品:私人日记(14)Sqlite封装
eBay, Amazon, Lazada, Shopee, AliExpress, Meikeduo and other cross-border e-commerce platforms, what conditions do I need to meet to evaluate the self-supporting account?How to optimize listing?
什么是中间人攻击?
新型数据中心,如何推动以数据为关键要素的数字经济落地?
ERP库存管理
Oracle学习笔记组合索引(十二)
Is it safe to choose a mobile phone application for a securities account?
安全测试-AndroidManifest.xml元素分析(1)
redis面试题