【C补充】判断明天是哪一天(明天的日期)

这篇博客介绍了一个C语言程序,用于接收用户输入的有效日期,并计算并输出第二天的日期。程序通过结构体存储年月日,检查日期是否为某月最后一天,是否为闰年的二月,以及是否为最后一个月。通过`numberOfDays()`函数计算月份天数,`isLeap()`函数判断闰年,确保日期转换的准确性。

一、概述

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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值