给定一个日期,输出前一天和后一天的日期

/*************************************************************************
	> File Name: yesterdayAndTomorrow.c
	> Author:zhangji 
	> Mail:mrzhangji@outlook.com 
	> Created Time: Sun 01 Oct 2023 09:57:53 PM CST
 ************************************************************************/

#include<stdio.h>
const int True = 1;
const int False =0;
// 判断是否闰年
int isLeap(int year){
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)     return True;
    return False;
}
// 根据年和月确定本月月底是几号
int getMonthEnd(int year, int month){
    if(isLeap(year) && month == 2 ) return 29;
    
    if(month == 1 || month == 4 || month == 6 || month == 9 || month == 11)
        return 30;
    else if(month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        return 31;
    else
        return 28;
}
//根据给定的年月日,判断今天是否是本月最后一天
int isMonthEnd(int year, int month,int day){
    if(month == 2 && isLeap(year) && day == 29) return True;
    if(month == 2 && !isLeap(year) && day == 28) return True;
    if((month == 1 || month == 4 || month == 6 || month == 9 || month == 11) && day == 30) return True;
    if((month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day == 31) return True;
    return False;
    
}
//输出前一天日期
void printYesterday(int year, int month, int day){
    if(day >= 2)            //如果今天不是本月第一天,天数直接减1
        day--;
    else {
        if(month > 1) month --;             //不是1月,则月份减1,否则置为12月,并把年份减1
        else{
            month = 12;
            year --;
        }
        day =  getMonthEnd(year, month);     //根据年和月确定上个月月底是几号
    }
    printf("%d %d %d\n", year, month, day);
}

void printTomorrow(int year, int month, int day){
    if(!isMonthEnd(year, month, day))   //如果不是本月最后一天,天数直接加1,年月不变
        day ++;
    else{
        day = 1;                    //是本月最后一天,天数置为1,若是12月,月份置1,否则加1
        if(month == 12){
            month = 1;
            year ++;
        }else month ++;
    }
    printf("%d %d %d\n", year, month, day);
}

int main(){
    int y, m, d;
    scanf("%d%d%d", &y, &m, &d);
    printYesterday(y, m, d);
    printTomorrow(y, m, d);


    return 0;
}

问题描述:给定一个日期,输出前一天和后一天日期。

算法思路:

在输出前一天时,首先判断是否是月首:若不是,则说明年月不变,天数直接减一即可;若是,需要判断这一天的上个月月底是几号,进而需要判断上个月是几月,若是2月还需要判断是否闰年。

在输出后一天时,首先判断是否月底,若不是,说明年月不变,天数直接加1即可;若是说明下一天是月首,需要进一步判断本月是否是12月,如果是说明跨年,年份加1,月份置1,否则年份不变,月份加1.

遇到的问题:

 if(month == 1 || month == 4 || month == 6 || month == 9 || month == 11 && day == 30) return True;
    if(month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 && day == 31) return True;

测试时发现这里有个严重的bug: 

当输入2000 1 1,输出 2000 2 1.

排除bug思路:根据输出可以断定,isMonthEnd()函数里出现了逻辑错误,当天是月首,但判断是否是月底时,返回为True;

必然是分支结构中出现错误,仔细观察发现,由于逻辑与&&优先级比逻辑或||要高,因此先计算month == 11(条件不成立),注意执行完这一步后不是从左到右执行逻辑或,再计算day == 30;

而是直接计算month == 1(条件成立,返回True),这是由于逻辑与和逻辑或的短路和断路原则导致的.

解决办法:让&&两边结构平行(先执行完所有逻辑或),即在左边加一对括号即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值