c语言(两天打鱼,三天晒网)

博客介绍了如何使用C语言判断两个日期间间隔年的闰平年情况,通过一个for循环从起始年份的下一年开始遍历到结束年份的前一年。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
typedef struct date {
    int year;
    int month;
    int day;
}DATE;


int countday(DATE from, DATE to);
int isLeap(int year);


int main()
{
    DATE end, start;


    int totaldays;
    int result;
    printf("请输入开始日期\n");
    scanf("%d%d%d", &start.year, &start.month, &start.day);


    printf("请输入结束日期\n");
    scanf("%d%d%d", &end.year, &end.month, &end.day);


    totaldays = countday(start, end);
    result = totaldays % 5;
    if(result > 0 && result < 4)
        puts("今天打鱼");
    else
        puts("今天晒网");
    return 0;
}


int isLeap(int year)
{
    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return 1;
    return 0;
}


int countday(DATE from, DATE to)
{
    int perMonth[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int result = 0, i;


    if (isLeap(from.year))
        perMonth[2] = 29;


    int t = 12;


    if (from.year == to.year) //同一年
        t = to.month - 1;


    for(i = from.month; i <= t; i++) {      //开始的那年的月份直接加到12月
        result += perMonth[i];
    }
    result -= from.day;
    perMonth[2] = 28;


    for (i = from.year + 1; i <= to.year - 1; i++) //判断中间间隔年的润平

{

        if (isLeap(i))
            result += 366;
        else
            result += 365;
    }

    if(isLeap(to.year))
        perMonth[2] = 29;
    if (from.year != to.year)
        for(i = 1; i <= to.month - 1; i++) {    //把终点月-1加上,不算当月  
            result += perMonth[i];
        }
    result += to.day;//将当月多的几天算进去
     
    return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值