c语言定义一个结构体变量(包括年、月、日),输入一个日期,计算该日在本年中是第几天。

2024-11-08 11:56:34
推荐回答(1个)
回答(1):

#include
#include
#include

struct ymd {
int Y,M,D;
};
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int YMD_2_JD(int Y, int M, int D){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=D;
for (i=0;iif (((Y%4==0)&&(Y%100!=0)||(Y%400==0)) && (M>2)) JD++;
return JD;
}

void main()
{
int d,k;
struct ymd a;
printf("input Year Month day\n");
k = scanf("%4d %2d %2d",&a.Y,&a.M,&a.D);
if (k != 3) {printf("input err\n"); exit(0);};
if (a.M < 1 || a.M > 12) {printf("err Month\n"); exit(0); };
if ( (a.M==2) && (a.D==29) ) {
if ( (a.Y%4==0)&&(a.Y%100!=0)||(a.Y%400==0)) {goto Lab;} else {printf("err Day\n"); exit(0); };
}
if (a.D < 1 || a.D > MonthDay[a.M]) {printf("err Day\n"); exit(0); };
Lab:;
if (a.Y < 0) {printf("negtive year\n"); exit(0);};
d=YMD_2_JD(a.Y,a.M,a.D);
printf("%d",d);
exit(0);
}