C/C++实现日期计算器的示例代码
更新时间:2020年4月25日 17:29 点击:1819
问题介绍:
今天突然看到一个问题看起来蛮有趣的,跟大家分享一下. 给定任意日期对该日期进行加减天数,最后得出加减后出现的日期.以及给两个日期你可以得出他们两个之间相隔多少天.(需要考虑闰年,每个月天数不同,我们需要写一个我们直接可以使用的日期加减器)因为时间比较仓促,我也没有写界面,只有其中几个主要的函数的架构思想以及简单的调试就发出来了.
代码实现:
#include<iostream> #include<Windows.h> #include<assert.h> using namespace std; class Date { public: Date(int year = 1997,int month = 1,int day = 1) :years(year) , months(month) , days(day) { assert(IScorrect()); } Date& operator=(const Date& d) { if (this != &d) { years = d.years; months = d.months; days = d.days; } return *this; } Date& operator + (int day) { while (day > 365) { if (ISleapyear() && day > 366) { years++; day = day - 366; } else { years++; day = day - 365; } } while (day >= Getmonthsday()) { //注意这里的次序问题,一定先减 再加 最后再判断. 如果顺序错了会出BUG的. day = day - Getmonthsday(); months++; if (months > 12) { years++; months = 1; } } while (day > 0) { DateAdvance(); day = day - 1; days++; } return *this; } Date& operator - (int day) //先减去一年,然后在使用加的重载,所以你只需要写一个无懈可击的加算法就够了. { while (day > 365) { if (ISleapyear() && day > 366) { day = day - 366; years--; } else { day = day - 365; years--; } } if (ISleapyear()) { day = 366 - day; years--; } else { day = 365 - day; years--; } operator+(day); return *this; } void DateAdvance() //用于出现可以进化的情况 { if (days > Getmonthsday()) { months++; days = 1; } if (months > 12) { years++; months = 1; } } int operator - (Date D) { int count = 0; if (*this > D) { while (*this != D) { D.operator+(1); count++; } } else { while (*this != D) { operator+(1); count++; } } return count; } bool ISleapyear() { if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) { return true; } return false; } int Getmonthsday() { int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (ISleapyear() && months == 2) { return 29; } return monthDays[months]; } void print() { cout << "目前的时间为"; cout << years << "." << months << "." <<days<< endl; } bool IScorrect() { if (years > 0 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) && days < 367)//闰年 { if (months >0 && months < 13) { if (days > 0 && days <= Getmonthsday()) { return true; } } } else if (years >0 && days < 366) //非闰年 { if (months >0 && months < 13) { if (days > 0 && days <= Getmonthsday()) { return true; } } } return false; } Date operator += (int day) { *this = *this + 100; return *this; } Date operator -= (int day) { return *this = *this - day; } inline Date& operator++() { *this += 1; return *this; } inline Date operator++(int) { Date tmp(*this); *this = *this + 1; return tmp; } bool operator == (const Date& d) { return (years == d.years&& months == d.months&&days == d.days); } bool operator != (const Date& d) { return !(*this == d); } bool operator >(const Date& d) { if (years > d.years || (years == d.years&&months > d.months) || (years == d.years&&months == d.months && days > d.days)) { return true; } return false; } bool operator < (const Date& d) { return !(*this > d); } bool operator >= (const Date& d) { return (*this == d) && (*this > d); } bool operator <= (const Date& d) { return (*this == d) && (*this < d); } private: int years; int months; int days; }; void Test() { Date d1(2012, 4, 5); Date d2(2013, 4, 5); d1.print(); /*d1 = d1 - 400;*/ d1.print(); cout << d1 - d2 << endl; d1.print(); system("pause"); }
总结:
日期类对我们掌握面向对象这里还是一个蛮重要的知识,你至少要能很熟练很正确地自己快速写出这个整个框架,然后一个一个实现函数,我只能说很重要,很重要,很重要大家一定要掌握.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 这篇文章主要介绍了R语言作图:坐标轴的设置方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-06
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- 这篇文章主要介绍了R语言删除指定变量或对象的操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-06
- 这篇文章主要介绍了c++中system("pause")的作用和含义,非常不错,具有参考借鉴价值,需要的朋友参考下吧...2020-04-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 这篇文章主要介绍了Go语言压缩和解压缩tar.gz文件的方法,实例分析了使用Go语言压缩文件与解压文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-05-03
基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍
这篇文章主要介绍了基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍 的相关资料,需要的朋友可以参考下...2016-05-14- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 这篇文章主要介绍了R语言基本画图函数与多图多线的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-06
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
InterlliJ IDEA2020新建java web项目找不到Static Web的解决
这篇文章主要介绍了InterlliJ IDEA2020新建java web项目找不到Static Web的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-02- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25