#include <stdio.h>
#include "math.h"
//定义月份宏
#define JANUARY 31
#define FEBRUARY 28
#define MARCH 31
#define APRIL 30
#define MAY 31
#define JUNE 30
#define JULY 31
#define AUGUST 31
#define SEPTEMBER 30
#define OCTOBER 31
#define NOVEMBER 30
#define DECEMBER 31
//声明一个结构体
typedef struct{
int year;
int month;
int day;
}DAYS;
int main () {
int february = FEBRUARY;
int sum = 0;
DAYS days;
printf("请输入需要查询的年月日\n");
printf("格式为: 年年年年 月月 日\n");
printf("您输入的为: ");
scanf("%d %d %d",&days.year,&days.month,&days.day);
//判断一下是否为闰年
if((days.year%400==0) || (days.year%100!=0&&days.year%4==0))
{
february++;
}
//判断一下day是否正确
if(days.day >= 32 || days.day < 1)
{
printf("data error");
}
switch(days.month)
{
case 1: sum = days.day; break;
case 2: sum = JANUARY + days.day; break;
case 3: sum = JANUARY + february + days.day; break;
case 4: sum = JANUARY + february + MARCH + days.day; break;
case 5: sum = JANUARY + february + MARCH + APRIL + days.day; break;
case 6: sum = JANUARY + february + MARCH + APRIL + MAY + days.day; break;
case 7: sum = JANUARY + february + MARCH + APRIL + MAY + JUNE + days.day; break;
case 8: sum = JANUARY + february + MARCH + APRIL + MAY + JUNE + JULY + days.day; break;
case 9: sum = JANUARY + february + MARCH + APRIL + MAY + JUNE + JULY + AUGUST + days.day; break;
case 10: sum = JANUARY + february + MARCH + APRIL + MAY + JUNE + JULY + AUGUST + SEPTEMBER + days.day; break;
case 11: sum = JANUARY + february + MARCH + APRIL + MAY + JUNE + JULY + AUGUST + SEPTEMBER + OCTOBER + days.day; break;
case 12: sum = JANUARY + february + MARCH + APRIL + MAY + JUNE + JULY + AUGUST + SEPTEMBER + OCTOBER + NOVEMBER + days.day; break;
default:printf("data error");break;
}
printf("\n%d年-%d月-%d日是今年的第 %d 天",days.year,days.month,days.day,sum);
}
例4:输入某年某月某日,判断这一天是这一年的第几天?
最新推荐文章于 2023-08-18 10:43:48 发布