My_A 发表于 2019-4-5 13:38:16

[C语言][100例题]根据年月日判断是一年中的第几天

#include <stdio.h>

/*
题目:输入某年某月某日,判断这一天是这一年的第几天?
    特殊情况,闰年且输入月份大于3时需考虑多加一天
*/

typedef enum {
    Jan = 31,Feb = 28,Mar = 31,Apr = 30,May = 31,June = 30,
    July = 31,Aug = 31,Sep = 30,Oct = 31,Nov = 30,Dec = 31
}month_day;

int _4_main()
{
    int count_day = 0;
    int year,month,day;
    printf("输入年月日(yyyy-mm-dd):");
    scanf("%d-%d-%d",&year,&month,&day);

    count_day += day;

    // 判断是否是闰年
    _Bool flag = 0;
    if(((year%100!= 0)&&(year%4 == 0))||(year%100 != 0)&&(year%400 == 0)) flag == 1;

    switch (month) {
    case 12:count_day += Nov;
    case 11:count_day += Oct;
    case 10:count_day += Sep;
    case9:count_day += Aug;
    case8:count_day += July;
    case7:count_day += June;
    case6:count_day += May;
    case5:count_day += Apr;
    case4:count_day += Mar;
    case3:count_day += (flag ? Feb+1 : Feb);
    case2:count_day += Jan;
    case1:;

    }

    printf("%d-%02d-%02d是一年中的第%d天",year,month,day,count_day);

    return 0;
}

页: [1]
查看完整版本: [C语言][100例题]根据年月日判断是一年中的第几天