c语言定义一个结构体变量(包括年、月、日),输入一个日期,计算该日在本年中是第几天。
注意:①考虑润年问题②输入的信息为数字以外时要提示错误信息③输入的信息超过相应位数时,只取前面相应位数的信息④输入的年月日不正确的时候需要提示错误信息...
注意:①考虑润年问题 ②输入的信息为数字以外时要提示错误信息 ③输入的信息超过相应位数时,只取前面相应位数的信息 ④输入的年月日不正确的时候需要提示错误信息
展开
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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;i<M;i++) JD+=MonthDay[i];
if (((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);
}
#include <stdlib.h>
#include <time.h>
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;i<M;i++) JD+=MonthDay[i];
if (((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);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询