首先,我们应知道日期计算器包括哪些功能

1、明天的日期

2 、n天后的日期

3、两个日期之间的天数

我们先从第一个功能开始,首先创建一个日期的结构体,包括:年、月、日。

struct date

{

int day;

int month;

int year;

};

其次来看一下解决这个问题的思想:

然后我们要写一个判断闰年的函数:

int Leap(struct date d)

{

int leap = false;

if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)

leap = true;

return leap;

}

上述代码中,我们需要先定义两个宏

#define true 1

#define false 0

假如是闰年,便返回1,否则返回0;

接下来,我们要写一个判断今天是否为本月的最后一天的函数:

int lastdays(struct date d)

{

int days;

int Month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

if (d.month == 2 && Leap(d))

days = 29;

else

days = Month[d.month - 1];

return days;

}

上面我们先定义了一个数组,里面是每个月的天数,其中二月有28,或者29天,我们在数组中定义二月为28天,然后判断本月是否为2月且是闰年,如果是,便返回29天,否则,返回每月的天数即可;

下面便是主函数了:

int main()

{

struct date today, tomorrow;

scanf("%d %d %d", &today.year, &today.month, &today.day);

if (today.day != lastdays(today))

{

tomorrow.day = today.day + 1;

tomorrow.month = today.month;

tomorrow.year = today.year;

}

else if (today.month == 12) {

tomorrow.day = 1;

tomorrow.month = 1;

tomorrow.year = today.year + 1;

}

else {

tomorrow.day = 1;

tomorrow.month = today.month + 1;

tomorrow.year = today.year;

}

printf("%d %d %d", tomorrow.year, tomorrow.month, tomorrow.day);

return 0;

}

其实当我们知道如何算明天的日期,后面两个问题也就迎刃而解了。对于第二个问题,只需要在主函数里面加循环就可以了,假如n为50的话,为非就是50个明天。

int main()

{

struct date today, tomorrow;

int n = 0;

scanf("%d %d %d", &today.year, &today.month, &today.day);

scanf("%d", &n);

while (n != 0)

{

if (today.day != lastdays(today))

{

tomorrow.day = today.day + 1;

tomorrow.month = today.month;

tomorrow.year = today.year;

}

else if (today.month == 12) {

tomorrow.day = 1;

tomorrow.month = 1;

tomorrow.year = today.year + 1;

}

else {

tomorrow.day = 1;

tomorrow.month = today.month + 1;

tomorrow.year = today.year;

}

n--;

today.day = tomorrow.day;

today.month = tomorrow.month;

today.year = tomorrow.year;

}

printf("%d %d %d\n", tomorrow.year, tomorrow.month, tomorrow.day);

return 0;

}

对于第三个问题,就是加一个判断,判断是否第二天是后面一天的日期,如果不是,就让count++:

int main()

{

struct date today, tomorrow, hou;

int count = 0;

scanf("%d %d %d", &today.year, &today.month, &today.day);

scanf("%d %d %d", &hou.year, &hou.month, &hou.day);

while (hou.year!=today.year || hou.month!=today.month || hou.day!=today.day)

{

if (today.day != lastdays(today))

{

tomorrow.day = today.day + 1;

tomorrow.month = today.month;

tomorrow.year = today.year;

}

else if (today.month == 12) {

tomorrow.day = 1;

tomorrow.month = 1;

tomorrow.year = today.year + 1;

}

else {

tomorrow.day = 1;

tomorrow.month = today.month + 1;

tomorrow.year = today.year;

}

today.day = tomorrow.day;

today.month = tomorrow.month;

today.year = tomorrow.year;

count++;

}

printf("%d\n", count);

return 0;

}这便是一个简单的日期计算器。

c语言日期计算器程序代码,用C语言写一个日期计算器相关推荐

  1. python实现一个整数加法计算器_如何使用python编程写一个加法计算器-Python教程...

    一.关上idle.点击file,而后点击new file.这是创立一个新的文件. 新建一个文件之后,咱们输出第一行代码,应用print函数,正在屏幕上打印一句话,此中字符串要应用双引号,输出法要应用英 ...

  2. java计算器程序代码实现加减乘除_Python+tkinter能实现计算器!太神奇了

    听说Python很强大,有第三方库啥事儿都能干,不仅能写一个计算器框架,还能实现加减乘除运算,听着甚是崇拜啊! 废话不啰嗦了,让我们一起来看看Python是如何把计算器用一个个代码实现的. 这里我们用 ...

  3. c语言删除一行程序代码,删除C语言程序中所有的注释语句的实现代码

    一种解法非常好:状态机.在各种状态之间跳转,逻辑清晰,不易出错,出错了也容易调试. 下面把代码贴出来: #include int state; int c1,c2; void change_state ...

  4. c语言常用的代码,初学C语言常用简单程序代码;

    <初学C语言常用简单程序代码;>由会员分享,可在线阅读,更多相关<初学C语言常用简单程序代码;(16页珍藏版)>请在人人文库网上搜索. 1.初学C语言常用简单程序代码素数的筛选 ...

  5. 用微信开发者小程序写一个加法计算器小程序

    文章目录 前言 一.calculator.wxml代码 二.calculator.js代码 结果 前言 提示:用软件微信开发者小程序写一个加法计算器小程序 一.calculator.wxml代码 &l ...

  6. 用Java写一个年薪计算器,助你简单完成大量税前税后总薪计算量

    每个有规模一点的公司都会有会计部门这是毋庸置疑的,但是会计的工作真的如大部分人想的那么简单,只是在每个月的最后几天才忙得不可开交吗 ? 答案当然不是,不然这个岗位早就人满为患了. 目前就我了解到的会计 ...

  7. linux shell 计算器 除0,用shell写一个简易计算器,可以实现加、减、乘、除运算,假如脚本名字为1.sh,执行示例:./1....

    用shell写一个简易计算器,可以实现加.减.乘.除运算,假如脚本名字为1.sh,执行示例:./1.sh 1 + 2#!/bin/bash if [ $# -ne 3 ] then echo &quo ...

  8. linux脚本计算器加减乘除,用shell写一个简易计算器,可以实现加、减、乘、除运算,假如脚本名字为1.sh,执行示例:./1....

    用shell写一个简易计算器,可以实现加.减.乘.除运算,假如脚本名字为1.sh,执行示例:./1.sh 1 + 2#!/bin/bash if [ $# -ne 3 ] then echo &quo ...

  9. 用shell写一个简易计算器,可以实现加、减、乘、除运算,假如脚本名字为1.sh,执行示例:./1....

    用shell写一个简易计算器,可以实现加.减.乘.除运算,假如脚本名字为1.sh,执行示例:./1.sh 1 + 2 #!/bin/bash if [ $# -ne 3 ] thenecho &quo ...

  10. java判断那个时间更晚_如何用Java判断日期是早于还是晚于另一个日期

    如何用Java判断日期是早于还是晚于另一个日期 另一个工作中常见的操作就是如何判断给定的一个日期是大于某天还是小于某天?在Java 8中,LocalDate类有两类方法isBefore()和isAft ...

最新文章

  1. 从1的补码说起计算机的数制
  2. 找不到具有绑定 MetadataExchangeHttpBinding 的终结点的与方案 http 匹配的基址。注册的基址方案是 [https]...
  3. 'avpicture_fill': 被声明为已否决
  4. Doc2Vec,Word2Vec文本相似度 初体验。
  5. C++起始(关键字,命名空间,缺省参数,函数重载(c语言为什么不支持函数重载))
  6. Java概述、环境变量、注释、关键字、标识符、常量
  7. Simpsons’ Hidden Talents(HDU-2594)
  8. Rabbitmq的原生javaAPI讲解
  9. 太难了!经营12年的明星机构都关停了,却还是有玩家疯狂入局
  10. 分治-Strassen矩阵乘法
  11. Log4j2 杀不死 Java
  12. win7 64位_VS2010的fftw3配置
  13. 下载论文的技巧及经验
  14. 字节跳动python面试题校招_字节跳动校招----编程题(Python)
  15. 好用的5款火狐浏览器必备插件,每一款都很实用
  16. webpack 基础学习
  17. 海洋cms index.php被修改,海洋CMS(SEACMS)新版本V6.55补丁仍可被绕过执行任意代码...
  18. cad命令栏怎么调出来_软件CAD | 基本操作逻辑
  19. CDH kudu Unable to load consensus metadata for tablet
  20. [书目20141024]王正良硬笔书法教学

热门文章

  1. windows下DxDiag查看笔记本电脑主板型号信息
  2. linux系统软路由软件,使用Linux+Zebra构建软路由系统
  3. PS新手教程,教你打造一个炫酷的冰冻字效_桂桂博客
  4. 笔记本安装系统不认硬盘?
  5. Neural Machine Translation by Jointly Learning to Align and Translate论文及代码助解
  6. 虚拟机Linux下如何查看自己的IP地址
  7. pvs-stdio ue4_云中的PVS-Studio:Azure DevOps
  8. pvs-stdio ue4_PVS-Studio –用于C,C ++,C#和Java的静态代码分析器
  9. SQL语句进阶学习一(where、通配符、正则表达式、计算字段、数据处理函数、分组数据)
  10. python中使用splash如何挂代理?