当前位置:网站首页>【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 principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
- UML旅游管理系统「建议收藏」
- Redis 分布式锁
- Rhcsa Road
- 红队第8篇:盲猜包体对上传漏洞的艰难利用过程
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- sql刷题627. 变更性别
- sql刷题584. 寻找用户推荐人
- Zabbix2.2 monitoring system and application log monitoring alarm
- Research and investment strategy report of neutral protease industry in China (2022 Edition)
猜你喜欢

模板引擎Velocity 基礎

数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)

sql刷题584. 寻找用户推荐人

游戏行业安全选择游戏盾,效果怎么样?

How to restore the system of Sony laptop
![[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting](/img/82/3bb382893682a30e8af130365ec4ef.jpg)
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting

Analysis of PostgreSQL storage structure

Buuctf gold III
![[flask introduction series] cookies and session](/img/2e/d50e0a032c4ec48935cb5df206a29b.png)
[flask introduction series] cookies and session

Template engine velocity Foundation
随机推荐
Kali install Nessus
Zabbix2.2监控之系统及应用日志监控报警
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...
Judge whether the binary tree is a binary search tree
拼接字符串,得到字典序最小的结果
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
Comprehensively view the value of enterprise digital transformation
China nylon 11 industry research and future forecast report (2022 Edition)
Basic use of MySQL
Rhcsa Road
虚拟串口模拟器和串口调试助手使用教程「建议收藏」
Introduction to software engineering - Chapter 6 - detailed design
Principle of SSM framework
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Go 语言怎么使用对称加密?
How to use F1 to F12 correctly on laptop keyboard
苹果自研基带芯片再次失败,说明了华为海思的技术领先性