日期

本文提供了一个使用C++编程语言实现的功能,可以将月份名称转换为对应的月份数字,并判断给定年份是否为闰年。通过输入特定格式的日期,程序能够计算两个日期之间的差异。主要涉及字符串比较、条件判断和日期计算等概念。

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

#include<bits/stdc++.h>
using namespace std;
int tans(char *s){
    if(!strcmp(s,"January")){
        return 1;
    }else if(!strcmp(s,"February")){
        return 2;
    }else if(!strcmp(s,"March")){
        return 3;
    }else if(!strcmp(s,"April")){
        return 4;
    }else if(!strcmp(s,"May")){
        return 5;
    }else if(!strcmp(s,"June")){
        return 6;
    }else if(!strcmp(s,"July")){
        return 7;
    }else if(!strcmp(s,"August")){
        return 8;
    }else if(!strcmp(s,"September")){
        return 9;
    }else if(!strcmp(s,"October")){
        return 10;
    }else if(!strcmp(s,"November")){
        return 11;
    }else if(!strcmp(s,"December")){
        return 12;
    }
}
bool isleap(int year){
    if(year%400==0)
        return 1;
    if(year%4==0&&year%100!=0)
        return 1;
    return 0;
}
int main(){
    int t,cnt=0;
    char mm1[20],mm2[20];
    int day1,day2,year1,year2,month1,month2;
    scanf("%d",&t);
    while(t--){
        scanf("%s %d, %d",mm1,&day1,&year1);
        scanf("%s %d, %d",mm2,&day2,&year2);
        month1=tans(mm1);
        month2=tans(mm2);
        if(year2<year1){
            swap(year1,year2);
            swap(month1,month2);
            swap(day1,day2);
        }else if(year1==year2&&month1>month2){
            swap(month1,month2);
            swap(day1,day2);
        }else if(year1==year2&&month1==month2&&day1>day2){
            swap(day1,day2);
        }
//        printf("1]  %d %d %d\n",year1,month1,day1);
//        printf("2]  %d %d %d\n",year2,month2,day2);
        int num2,num1,num;
        num2=year2/400+year2/4-year2/100;
        num1=(year1-1)/400+(year1-1)/4-(year1-1)/100;
        num=num2-num1;
        if(isleap(year2)){
            if(month2==1||month2==2&&day2<29){
                num--;
       //         printf("%d+++++\n",num);
            }
        }
        if(isleap(year1)){
            if(month1>=3){
                num--;
         //       printf("%d-----\n",num);
            }
        }

        printf("Case %d: %d\n",++cnt,num);
    }

    return 0;
}
/*
5
August 12, 2000
August 12, 2005

*/

  

转载于:https://www.cnblogs.com/chengsheng/p/4679973.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值