服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++逐步介绍日期类的使用

C++逐步介绍日期类的使用

2023-02-24 15:30幻荼 C/C++

下面小编就为大家带来一篇C++实现日期类(Date类)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

我们今天实现一个简单的计算日期

C++逐步介绍日期类的使用

我们这里先给出头文件,后面一个一个解释

头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#pragma once
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
    //定义构造函数
    Date(int year = 1900, int month = 1, int day = 1);
    //打印日期
    void Print()
    {
        cout << _year <<" " <<_month<<" " << _day << endl;
    }
    //运算符重载
    Date operator+(int day);
    Date operator-(int day);
    Date& operator+=(int day);
    Date& operator-=(int day);
    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);
    Date& operator++();//前置
    Date operator++(int);//后置
    Date& operator--();//前置
    Date operator--(int);//后置
    int operator-(Date d);//计算两个日期的差值
private:
    int _year;
    int _month;
    int _day;
};

详细步骤

第一步

计算闰年和判断日期是否合法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
inline int GetMonthDay(int year, int month)
{
    //多次调用的时候static能极大减小内存消耗,但是也可以删除,并不影响程序运行
    static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    int day = _monthArray[month];
    if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//判断是否是闰年
    {
        day = 29;
    }
    return day;
}
Date::Date(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
    if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))//看日期是否合法
    {
        cout << "fail" << endl;
        exit(-1);
    }
}

我们将year,month,day赋值之后,首先进行判断,如果出现异常天数直接终结程序(如2022,1,32,这种不存在的日期)

随后一个问题就出来了,因为闰年和非闰年的2月不同,所以我们又使用一个函数getmonthday,来判断闰年的同时获取当月天数;

有些同学可能直接是【0,31,59(31+28 1月+2月),89(1月+2月+3月)......+.....365】

我们稍后再解释为什么要用我这种定义方式。

第二步

各类运算符重载

我们首先来看运算符”+”和”+=”

这是一般情况下的+

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Date Date::operator+(int day)
{
    Date tmp(*this);
    tmp._day += day;
    //判断天数是否大于当月的天数
        while (tmp._day > GetMonthDay(tmp._year, tmp._month))
        {
            tmp._day -= GetMonthDay(tmp._year, tmp._month);
            tmp._month++;
            //判断月数是否大于12
            if (tmp._month > 12)
            {
                tmp._year++;
                tmp._month = 1;
            }
        }
        return tmp;
}

这是一般情况下的+=

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Date& Date::operator+=(int day)
{
    Date ret(*this);
        ret._day += day;
        //判断天数是否超越了当月的天数
        while (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            _month++;
            //判断月数是否超过了12
            if (_month > 12)
            {
                _year++;
                _month = 1;
            }
        }
    }
    return *this;
}

我们可以发现两者其实都用了同一个while循环来判断日期是否合法。

我们可以选择将while循环打包成一个新的函数,也可以选择直接复用

+复用+=

?
1
2
3
4
5
6
Date Date::operator+(int day)
{
    Date tmp(*this);
    tmp += day;//直接调用operator的+=
        return tmp;
}

总代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
inline int GetMonthDay(int year, int month)
{
    static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    int day = _monthArray[month];
    if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        day = 29;
    }
    return day;
}
Date::Date(int year, int month, int day)
{
    _year = year;
    _month = month;
    _day = day;
    if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))
    {
        cout << "!legal" << endl;
    }
}
Date& Date::operator+=(int day)
{
    if (day < 0)
    {
        *this -= -day;
    }
    else {
        _day += day;
        while (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            _month++;
            if (_month > 12)
            {
                _year++;
                _month = 1;
            }
        }
    }
    return *this;
}
Date& Date::operator-=(int day)
{
    if (day < 0)
    {
        *this += -day;
    }
    else {
        _day -= day;
        while (_day <= 0)
        {
            if (_month == 1)
            {
                --_year;
                _month = 12;
                _day += GetMonthDay(_year, _month);
            }
            else
            {
                --_month;
                _day += GetMonthDay(_year, _month);
            }
        }
    }
    return *this;
}
Date Date::operator+(int day)
{
    Date tmp(*this);
    tmp += day;
        return tmp;
}
Date Date::operator-(int day)
{
    Date tmp(*this);
    tmp -= day;
        return tmp;
}
Date& Date::operator++()//前置
{
    return  *this += 1;;
}
Date  Date::operator++(int)//后置
{
    Date tmp(*this);
    *this += 1;
    return tmp;
}
Date& Date::operator--()//前置
{
    return *this -= 1;
}
Date  Date::operator--(int)//后置
{
    Date tmp(*this);
    *this -= 1;
    return tmp;
}
bool Date::operator>(const Date& d)
{
    if (_year > d._year)
        return true;
    else if (_year < d._year)
        return false;
    else//_year < d._year
    {
        if (_month > d._month)
            return true;
        else if (_month < d._month)
            return false;
        else//_month < d._month
        {
            if (_day > d._day)
                return true;
            else
                return false;
        }
    }
}
bool  Date::operator<(const Date& d)
{
    return !(*this > d || *this == d);
}
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)
{
    if (_year == d._year)
    {
        if (_month == d._month)
        {
            if (_day == d._day)
                return true;
        }
    }
    return false;
}
bool  Date::operator!=(const Date& d)
{
    //复用==
    return !(*this == d);
}
int Date::operator-(Date d)
{
    int count = 0;
    Date tmp(*this);
    if (tmp < d)
    {
        while (tmp != d)
        {
            ++count;
            tmp += 1;
        }
    }
    else if (tmp > d)
    {
        while (tmp != d)
        {
            --count;
            tmp -= 1;
        }
    }
    return count;
}

到此这篇关于C++逐步介绍日期类的使用的文章就介绍到这了,更多相关C++日期类内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_62718027/article/details/125086459

延伸 · 阅读

精彩推荐
  • C/C++C++学习心得之扫雷游戏

    C++学习心得之扫雷游戏

    这篇文章主要为大家详细介绍了C++学习心得之扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    璀璨的冰11372021-08-25
  • C/C++华为面试题数字大小写转换

    华为面试题数字大小写转换

    华为面试题:一个四位数,如1024,1004,打印出他们的中文形式,如果一千零二十四,一千零四,大家参考使用吧...

    C语言教程网11372021-01-12
  • C/C++关于C语言中参数的传值问题

    关于C语言中参数的传值问题

    C语言中参数的传值一直比较含糊,今天在网上看到三个面试题的详解,感觉讲的很好,就拿来记下,方便学习和记忆...

    C语言教程网6342021-01-04
  • C/C++C++实现LeetCode165.版本比较)

    C++实现LeetCode165.版本比较)

    这篇文章主要介绍了C++实现LeetCode165.版本比较),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    Grandyang6392021-12-09
  • C/C++Qt学习教程之表格控件蚂蚁线详解

    Qt学习教程之表格控件蚂蚁线详解

    如果有用过PS的选区工具应该就会知道蚂蚁线是什么东西了,就是用来表示选区的一种虚线,关键还是要动态的!下面这篇文章主要给大家介绍了关于Qt学习...

    朝十晚八12462021-06-27
  • C/C++C++之内存泄漏排查详解

    C++之内存泄漏排查详解

    这篇文章主要介绍了c++ 如何排查内存泄漏,帮助大家更好的理解和学习使用c++,感兴趣的朋友可以了解下,希望能够给你带来帮助...

    自由追光者4202022-01-20
  • C/C++C++ 中RTTI的使用方法详解

    C++ 中RTTI的使用方法详解

    这篇文章主要介绍了C++ 中RTTI的使用方法详解的相关资料,希望通过本文大家能理解使用RTTI,需要的朋友可以参考下...

    a9920367956062021-06-01
  • C/C++C 语言条件运算符详细讲解

    C 语言条件运算符详细讲解

    本文主要介绍C语言中的条件运算符,并提供示例代码以便大家学习参考,希望能帮助学习 C语言的同学...

    C语言中文网5202021-04-12