当前位置:网站首页>【C語言補充】判斷明天是哪一天(明天的日期)
【C語言補充】判斷明天是哪一天(明天的日期)
2022-07-01 16:54: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;
}
边栏推荐
- FPN network details
- Soft test network engineer full truth simulation question (including answer and analysis)
- Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
- Is the securities account given by the head teacher of goucai school safe? Can I open an account?
- 数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
- Authentication processing in interface testing framework
- China sorbitol Market Forecast and investment strategy report (2022 Edition)
- Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
- Buuctf gold III
- sql刷题586. 订单最多的客户
猜你喜欢

Redis6.0 new features
![[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境](/img/0e/52e37527bc717c7a55741725087bad.png)
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境

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

【C语言基础】12 字符串

巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...

【C补充】【字符串】按日期排序显示一个月的日程

Redis Distributed Lock

Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)

Stegano in the world of attack and defense

How to restore the system of Sony laptop
随机推荐
Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
China sorbitol Market Forecast and investment strategy report (2022 Edition)
How to repair the laptop that cannot connect to the wireless network
Jojogan practice
AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
Activity的生命周期和启动模式详解
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
EndeavourOS移动硬盘安装
How to cancel automatic search and install device drivers for laptops
Kali install Nessus
SystemVerilog structure (II)
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
[live broadcast appointment] database obcp certification comprehensive upgrade open class
C language input / output stream and file operation
How to use phpipam to manage IP addresses and subnets
判断链表是否是回文链表
SQL question brushing 584 Looking for user references
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"