问题介绍:

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

代码实现:

#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 << "." <

}

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语言编写天数计算器,C/C++实现日期计算器的示例代码相关推荐

  1. python有趣的代码-分享几款由quot;Python”语言编写的quot;有趣、恶搞、好玩”的程序代码...

    为提高大家对"Python"编程语言的学习兴趣,今天给大家分享几款有趣的Python程序代码,感兴趣的小伙伴可以跟着学习借鉴哦!qq进群:156846986可以获取Python学习 ...

  2. python有趣小程序-搞几款由quot;Python”语言编写的quot;有趣、恶搞、好玩”的程序代码!...

    原标题:搞几款由"Python"语言编写的"有趣.恶搞.好玩"的程序代码! 为提高大家对"Python"编程语言的学习兴趣,今天给大家分享几 ...

  3. c语言编译星座测试,用c语言编写程序,判断输入的日期(月,日)属于哪个星座?...

    匿名用户 1级 2010-04-29 回答 c语言的输出输入是格式化的,printf表示输出,在屏幕上打印出来:scanf表示读入,即把你在屏幕上如果打10,按回车,10就存储到month那个变量里面 ...

  4. c语言编写二次方程求根程序,一元二次方程求解程序完整代码

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 下面的代码是我刚才无聊写的.对于简单的一元多次方程的迭代 #include #include #include #define MAXTIMES 5 ty ...

  5. 用HTML语言编写 蓝色字体,背景色为蓝色,字体为红色的代码 怎么用HTML的形式表示?...

    方法/步骤 新建一个txt文档,将其文件名和后缀改成"index.html"的html文件,如下图所示. 请点击输入图片描述 新建好html文件后,我们用代码编辑器打开它,然后填写 ...

  6. 用c语言编写的atof函数,自己实现的atof()和atoi()代码

    # re: 自己实现的atof()和atoi()代码  回复  更多评论 2014-09-05 22:27 by memristor 定义函数 double atof(const char *nptr ...

  7. rosserial_java_编写ros串口节点,使用官方serial包(示例代码)

    参考http://www.roswiki.com/read.php?tid=557&fid=39 1.通过sudo apt-get install ros--serial下载ROS对应版本的工 ...

  8. c语言手写指针和乘号,C-学会使用指针(示例代码)

    先说:唯手熟尔 指针 指针存储着一个内存空间的地址 定义一个指针 int a: int * p: 定义一个指针,只需在变量前面加一个 * 号.这里的指针只能存储一个存放整数的内存空间的地址 指针如何存 ...

  9. C语言丨运算符号的三种用法(有示例代码)

    &: 在C语言中有两种意思,一种是取地址符,是单目运算符:另一种是位运算符,表示"按位与",是双目运算符. 1.用于指针赋值 #include<stdio.h> ...

  10. u32在c语言中的作用,c语言中u8,u16,u32(示例代码)

    u8是unsigned char,u16是unsigned short,u32是unsigned long. u8,u16,u32都是C语言数据类型,分别代表8位,16位,32位长度的数据类型,一个字 ...

最新文章

  1. oracle里面子判断,在过程中添加子进程,在子过程中判断
  2. C++11获取double类型的最大最小值
  3. SpringBoot配置Druid
  4. flink編譯hadoop3.1.2(失败,这个问题没有意义,关闭)
  5. 数据库中char, varchar, nvarchar的差异
  6. 2-路插入排序c语言算法,浅谈2路插入排序算法及其简单实现
  7. Android Spinnert的使用
  8. php request对象,PHP 中TP5 Request 请求对象的实例详解
  9. Objective-C 中 NULL、nil、Nil、NSNull 的定义及不同
  10. php mysql web应用,PHP+MySQL Web应用开发
  11. java的HashCode方法
  12. 最近病毒缠身,帖两个病毒的解决方法.
  13. 应对雾霾,哪款口罩适合你?
  14. UML-----实现图(构件图、部署图)
  15. java pdf 页面 拼接_如何使用Java平铺PDF文档中的页面内容?
  16. PHP工程改成微擎的步骤_微擎系统搭建
  17. 如何设置电脑的保护色?
  18. 2015年底学习汇总报告
  19. python正则表达式实战中的总结
  20. Android自定义圆角矩形图片ImageView

热门文章

  1. Indilinx主控固态量产成功修复步骤
  2. 【Paper】Neural Machine Translation by Jointly Learning to Align and Translate
  3. win10系统备份教程
  4. YonMaster开发者认证线上赋能培训班定档4月18日
  5. web资源分享(视频、资料)
  6. 初学者习字如何选择练字用的辅助格子纸?
  7. python mongdb 和 mysql简单使用
  8. Mysql的read_only 只读属性说明 (运维笔记)
  9. 【215期推荐】另类思考:HIS能给医院带来什么“坏处”?
  10. Ubuntu Server 20.04 下 HustOJ 安装