当前位置:网站首页>【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;
}
边栏推荐
- Rhcsa Road
- Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
- P2893 [usaco08feb] making the grade g (DP & priority queue)
- Analysis of PostgreSQL storage structure
- Principle of SSM framework
- Advantages, values and risks of chain games compared with traditional games
- Leetcode 216 combined summation III -- backtracking method
- Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
- Redis 分布式鎖
- 瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
猜你喜欢
Endeavouros mobile hard disk installation
VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
【观察】数字化时代的咨询往何处走?软通咨询的思与行
C語言輸入/輸出流和文件操作
Detailed explanation of activity life cycle and startup mode
Kali install Nessus
模板引擎Velocity 基礎
sql刷题627. 变更性别
随机推荐
Zabbix2.2监控之系统及应用日志监控报警
机器学习11-聚类,孤立点判别
Determine whether the linked list is a palindrome linked list
软件工程导论——第六章——详细设计
Origin2018安装与使用(整理中)
【观察】数字化时代的咨询往何处走?软通咨询的思与行
What are the differences between PHP and DW
vim用户自动命令示例
Golang爬虫框架初探
Judge whether the binary tree is a binary search tree
[nodemon] app crashed - waiting for file changes before starting...解决方法
想做软件测试的女孩子看这里
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
How to maintain the laptop battery
如何使用phpIPAM来管理IP地址和子网
Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
Are you still using charged document management tools? I have a better choice! Completely free
Rhcsa Road
SystemVerilog-结构体(二)
How does go use symmetric encryption?