当前位置:网站首页>【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;
}
边栏推荐
- Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
- Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
- How to restore the system of Sony laptop
- Judge whether a binary tree is a balanced binary tree
- Stegano in the world of attack and defense
- FPN网络详解
- Research and investment strategy report of neutral protease industry in China (2022 Edition)
- Comprehensively view the value of enterprise digital transformation
- 数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
- China carbon disulfide industry research and investment strategy report (2022 Edition)
猜你喜欢
Buuctf gold III
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
sql刷题627. 变更性别
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
Template Engine Velocity Foundation
[pyg] document summary and project experience (continuously updated
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
模板引擎Velocity 基础
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
How to maintain the laptop battery
随机推荐
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
How to solve the keyboard key failure of notebook computer
China carbon disulfide industry research and investment strategy report (2022 Edition)
【PyG】文档总结以及项目经验(持续更新
SystemVerilog-结构体(二)
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
Analysis of PostgreSQL storage structure
Endeavouros mobile hard disk installation
PR basic clip operation / video export operation
P2893 [usaco08feb] making the grade g (DP & priority queue)
为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
P2592 [zjoi2008] birthday party (DP)
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
数据库系统原理与应用教程(004)—— MySQL 安装与配置:重置 MySQL 登录密码(windows 环境)
What is the effect of choosing game shield safely in the game industry?
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
游戏行业安全选择游戏盾,效果怎么样?
Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI