C语言计算是否为闰年

简介: 用C语言代码来实现计算闰年

所需头函数:

#include<stdio.h>

#include<stdbool.h>

#include<stdlib.h>


定义一个存储年月日的结构体

structdate {

 intmonth;

 intday;

 intyear;

} ;


完整代码呈现

boolisLeap(structdated);

intnumberOfDays(structdated);


intmain(intargc, charconst *argv[])

{

 structdatetoday, tomorrow;

 

 printf("Enter today's date (mm dd yyyy):");

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

通过三个判断语句来一点一点分析

 if( today.day != numberOfDays(today) ) {

 tomorrow.day = today.day+1;

 tomorrow.month = today.month;

 tomorrow.year = today.year;

} elseif (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("Tomorrow's date is %i-%i-%i.\n", tomorrow.year, tomorrow.month, tomorrow.day);

 

 return0;

}


intnumberOfdays(structdated)

{

 intdays;

 

 constintdaysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

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

{

 days = 29;

}else

{

 days = daysPerMonth[d.month-1];

}

 returndays;

}


boolisLeap(structdated)

{

 boolleap = false;

 

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

 leap = true;

 

 returnleap;

}

相关文章
|
4月前
|
C语言
【C语言】库宏offsetof(结构体成员偏移量计算宏)
【C语言】库宏offsetof(结构体成员偏移量计算宏)
37 0
|
4月前
|
编译器 C语言
【C语言】strlen()函数(字符串长度计算函数)
【C语言】strlen()函数(字符串长度计算函数)
58 0
|
4月前
|
存储 编译器 Linux
【C语言】自定义类型:结构体深入解析(二)结构体内存对齐&&宏offsetof计算偏移量&&结构体传参
【C语言】自定义类型:结构体深入解析(二)结构体内存对齐&&宏offsetof计算偏移量&&结构体传参
|
3月前
|
存储 C语言
【C语言刷题每日一题#牛客网HJ73】——计算日期到天数转换(给定日期,计算是该年的第几天)
【C语言刷题每日一题#牛客网HJ73】——计算日期到天数转换(给定日期,计算是该年的第几天)
|
3月前
|
存储 安全 C语言
【C语言刷题每日一题】——求最大公约数(带数学计算过程详解)
【C语言刷题每日一题】——求最大公约数(带数学计算过程详解)
|
3月前
|
存储 C语言
【C语言刷题每日一题】——计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,打印出结果
【C语言刷题每日一题】——计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,打印出结果
|
3月前
|
存储 C语言
C语言学习记录——联合体(共用体、特点、用法、联合体大小计算)
C语言学习记录——联合体(共用体、特点、用法、联合体大小计算)
48 2
|
3月前
|
C语言
【C语言刷题系列】计算整数的二进制位中1的个数 (三种方式)
【C语言刷题系列】计算整数的二进制位中1的个数 (三种方式)
|
3月前
|
C语言
C语言-----计算两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
C语言-----计算两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
|
3月前
|
Serverless C语言
C语言----递归函数,计算一个非负整数的数字之和
C语言----递归函数,计算一个非负整数的数字之和