当前位置:网站首页>【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;
}
边栏推荐
- 数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
- Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- 判断一棵二叉树是否为平衡二叉树
- Research and investment strategy report of neutral protease industry in China (2022 Edition)
- What is the effect of choosing game shield safely in the game industry?
- Determine whether the linked list is a palindrome linked list
- 复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
- Template Engine Velocity Foundation
- Why is the pkg/errors tripartite library more recommended for go language error handling?
猜你喜欢

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

VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...

今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!

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

Stegano in the world of attack and defense

广东用电量大跌,说明高新技术产业替代高能耗产业已取得初步成果

Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!

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

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

Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
随机推荐
[flask introduction series] cookies and session
SQL question brushing 584 Looking for user references
C语言输入/输出流和文件操作
Introduction to software engineering - Chapter 6 - detailed design
C language input / output stream and file operation
Sword finger offer II 015 All modifiers in the string
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
Go 语言源码级调试器 Delve
[nodemon] app crashed - waiting for file changes before starting... resolvent
Concatenate strings to get the result with the smallest dictionary order
想做软件测试的女孩子看这里
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
P2592 [zjoi2008] birthday party (DP)
Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid
Flux d'entrées / sorties et opérations de fichiers en langage C
Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
How to solve the keyboard key failure of notebook computer
What are the differences between PHP and DW
Leetcode 77 combination -- backtracking method
C語言輸入/輸出流和文件操作