• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C/C++实现日期计算器的示例代码

c++ 搞代码 4年前 (2022-01-06) 34次浏览 已收录 0个评论

本篇文章主要介绍了C/C++实现日期计算器的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

问题介绍:

今天突然看到一个问题看起来蛮有趣的,跟大家分享一下. 给定任意日期对该日期进行加减天数,最后得出加减后出现的日期.以及给两个日期你可以得出他们两个之间相隔多少天.(需要考虑闰年,每个月天数不同,我们需要写一个我们直接可以使用的日期加减器)因为时间比较仓促,我也没有写界面,只有其中几个主要的函数的架构思想以及简单的调试就发出来了.

代码实现:

 #include #include #include 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< 0 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) && days 0 && months  0 && days 0 && days 0 && months  0 && days (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  d); } bool operator >= (const Date& d) { return (*this == d) && (*this > d); } bool operator <= (const Date& d) { return (*this == d) &<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>& (*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/C++实现日期计算器的示例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C/C++实现日期计算器的示例代码
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址