当前位置:网站首页>【C语言补充】判断明天是哪一天(明天的日期)
【C语言补充】判断明天是哪一天(明天的日期)
2022-07-01 16:28:00 【一苇以航fp】
一、概述
1.1 功能介绍
- 随机输入一个有效日期,输出第二天的日期
1.2 重点
- 该日期是否是某月最后一天?
- 是否是闰二月?
- 是否是最后一个月?
1.3 实现方法
- 使用结构体,将年月日存储在一个结构变量中
二、代码
#include<stdio.h>
#include<stdbool.h> //引入bool类型
struct date {
//结构标号date
int year;
int month;
int day;
};
int numberOfDays(struct date d); //某月天数
bool isLeap(struct date d); //判断闰年
int main(int argc, char* const argv[]){
struct date today, tommorow;
printf("输入日期(年 月 日):");
scanf(" %i %i %i", &today.year, &today.month, &today.day);
if( today.day != numberOfDays(today) ){
//比较天数, 判断是否最后一天
tommorow.day = 1;
tommorow.month = today.month + 1;
tommorow.year = today.year;
}
else if( today.month == 12 ){
tommorow.day = 1;
tommorow.month =1;
tommorow.year = today.year + 1;
}
else {
tommorow.day = today.day + 1;
tommorow.month = today.month;
tommorow.year = today.year;
}
printf("明天是 %i年%i月%i日.\n",
tommorow.year, tommorow.month, tommorow.day);
return 0;
}
//判断本月天数
int numberOfDays(struct date d)
{
int days; //记录本月天数
const int daysPerMonth[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //全年各月天数
if( d.month == 2 && isLeap(d) )
days = 29; //闰二月,取29天
else
days = daysPerMonth[d.month-1];
return days;
}
//判断闰年
bool isLeap(struct date d)
{
bool leap = false;
//闰年:能被4整除但不能被100整除,或能被400整除
if( (d.year%4 == 0 && d.year%100 != 0) || d.year%400 == 0 )
leap = true;
return leap;
}
边栏推荐
- P2592 [zjoi2008] birthday party (DP)
- Why is the pkg/errors tripartite library more recommended for go language error handling?
- 【Kotlin】高阶函数介绍
- 【直播预约】数据库OBCP认证全面升级公开课
- AI高考志愿填报:大厂神仙打架,考生付费围观
- sql刷题586. 订单最多的客户
- MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
- Research and investment strategy report of neutral protease industry in China (2022 Edition)
- Detailed explanation of activity life cycle and startup mode
- Rhcsa Road
猜你喜欢

为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!

C language input / output stream and file operation

How to solve the problem that the battery icon of notebook computer does not display

Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!

MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一

Buuctf gold III

Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers

Endeavouros mobile hard disk installation

sql刷题586. 订单最多的客户

嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
随机推荐
红队第8篇:盲猜包体对上传漏洞的艰难利用过程
Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
制造业数字化转型究竟是什么
How to maintain the laptop battery
Redis6.0 新功能
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
[flask introduction series] cookies and session
模板引擎Velocity 基礎
苹果自研基带芯片再次失败,说明了华为海思的技术领先性
The difference between the lazy mode of singleton mode and the evil mode
Comprehensively view the value of enterprise digital transformation
How to cancel automatic search and install device drivers for laptops
C语言输入/输出流和文件操作
C language input / output stream and file operation
Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
How to solve the keyboard key failure of notebook computer
Introduction to software engineering - Chapter 6 - detailed design
软件工程导论——第六章——详细设计
Rhcsa Road