当前位置:网站首页>【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;
}
边栏推荐
- PR basic clip operation / video export operation
- Basic use of MySQL
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
- [flask introduction series] cookies and session
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- 如何使用 etcd 实现分布式 /etc 目录
- Is the programmer's career really short?
- How does go use symmetric encryption?
- Rhcsa Road
猜你喜欢
The amazing open source animation library is not only awesome, but also small
How to restore the system of Sony laptop
SQL question brushing 627 Change gender
Flux d'entrées / sorties et opérations de fichiers en langage C
Basic usage of Frida
Rhcsa Road
Authentication processing in interface testing framework
Redis distributed lock
How to repair the laptop that cannot connect to the wireless network
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
随机推荐
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
Machine learning 11 clustering, outlier discrimination
Flux d'entrées / sorties et opérations de fichiers en langage C
EndeavourOS移动硬盘安装
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
判断一棵二叉树是否为平衡二叉树
模板引擎Velocity 基础
mysql -- explain性能优化
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"
P2893 [usaco08feb] making the grade g (DP & priority queue)
FPN network details
Basic usage of Frida
SQL question brushing 584 Looking for user references
Sword finger offer II 015 All modifiers in the string
Golang爬虫框架初探
The amazing open source animation library is not only awesome, but also small
How to solve the problem that the battery icon of notebook computer does not display