输入现在的日期,输出明天的日期

首先, 定义结构体, 年, 月, 日

struct date{
    int month;
    int day;
    int year;
};

然后,就可以输入今天的日期了

    struct date today,tomorrow;

    printf("Enter today's date(mm dd yyyy):");
    scanf("%i %i %i",&today.year,&today.month,&today.day);

这里需要判断平年和闰年, 所以先调用一个函数

bool isLeap(struct date d)
{
    bool leap = 0;
    if((d.year %4==0 && d.year %100 !=0) || d.year%400 == 0){
        leap = 1;
    }
    return leap;
}

返回的数值,就可以判断润平年了

我们思考一下,现在知道二月的天数了, 所以,我们要想知道明天日期,只用号数加一就可以了,  这里需要使用if 语句进行逐层排除

最简单的就是 , 只用号数加一天, 别的不用变,  所以我们要确保今天不是本月的最后一天, 所以就是拿着今天的号数和本月的天数作对比

today.day != number0fDays(today)

所以, 这里我们又需要进行, 调用一个含有结构体的函数, 来判断本月的天数了, 因为还要掺杂二月,  所以也得调用判断润平年的函数

int number0fDays(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;
    }else{
        days = daysPerMOnth[d.month-1];
    }
    return days;
}


我们这里和润平年定义的结构体参数是一样的, 因为我们需要同一个数据, 首先就是定义天数,  然后就是创建一个数组,

对每一个月进行天数填充,  可以只填平年的,做一个二月的判断就行了,  灵活掌握

然后就是, 天数对应的数组地址是月份减一, 因为是从0 开始算的. 

也可以分开,自己掌握


int number0fDays(struct date d)
{
    int days;
    const int daysPerMOnth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    const int daysPerMOnth_1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    if(isLeap(d)){
        
        days = daysPerMOnth[d.month-1];
    }else{
        days = daysPerMOnth_1[d.month-1];
    }
    return days;
}

接下来, 就简单了,判断,  当本号不等于 本月的天数的时候,  就把号数加一, 其他不变就行了


    if(today.day != number0fDays(today)){
        tomorrow.day = today.day+1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
    }

现在思考特殊情况,  每月最后一天,  和一年最后一天,  先限制最少的, 所以就是, 每年最后一天的话,  第二天,  天数月份都变为

1,  年份加一就行了

else if(today.month == 12){
        tomorrow.day = 1;
        tomorrow.month = 1;
        tomorrow.year = today.year+1;
    }

最后就是, 每月的最后一天了,  直接号数变为1, 月份加一,  年份不变

else{
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }

从这里就得出一个规律, if语句, 先限制小范围, 剩余的就是  大范围相同的类型,  减少麻烦

最后输出就行了

printf("tomorrow是%i-%i-%i.\n",tomorrow.year,tomorrow.month,tomorrow.day);
    return 0;

总结:我们在利用结构体进行函数参数调用的时候,  可以一次性调用很多, 但是如果真的要改变结构体函数的值的话,  就需要

利用指针, 对传入参数的地址,  进行修改,  不然的话,  调用函数的话,  只能通过return 返回数值了.

源代码:

#include <stdio.h>
#include <stdbool.h>

struct date{
	int month;
	int day;
	int year;
};

bool isLeap(struct date d);
int number0fDays(struct date d);

int main(int argc, char const *argv[])
{
	struct date today,tomorrow;

	printf("Enter today's date(mm dd yyyy):");
	scanf("%i %i %i",&today.year,&today.month,&today.day);

	if(today.day != number0fDays(today)){
		tomorrow.day = today.day+1;
		tomorrow.month = today.month;
		tomorrow.year = today.year;
	}else if(today.month == 12){
		tomorrow.day = 1;
		tomorrow.month = 1;
		tomorrow.year = today.year+1;
	}else{
		tomorrow.day = 1;
		tomorrow.month = today.month + 1;
		tomorrow.year = today.year;
	}
	printf("tomorrow是%i-%i-%i.\n",tomorrow.year,tomorrow.month,tomorrow.day);
	return 0;

}
int number0fDays(struct date d)
{
	int days;
	const int daysPerMOnth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
	const int daysPerMOnth_1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	if(isLeap(d)){
		
		days = daysPerMOnth[d.month-1];
	}else{
		days = daysPerMOnth_1[d.month-1];
	}
	return days;
}

bool isLeap(struct date d)
{
	bool leap = 0;
	if((d.year %4==0 && d.year %100 !=0) || d.year%400 == 0){
		leap =1 ;
	}
	return leap;
}