输入年和第几天算日期

#include "stdio.h"

typedef struct{
	int day;
	int year;
}indate;

typedef struct{
	int day;
	int month;
	int year;
}opdate;

bool Isleap(int d);
void caltime(indate *dd, opdate *dt);

int main(){
	indate ti = { 0, 0 };
	opdate time = { 0, 0, 0 };
	//Time(&ti);
	do{
		printf("input the year and the day's number (year num)\n");
		scanf("%d %d", &ti.year, &ti.day);

	} while (ti.day<1 || (ti.day>366 && Isleap(ti.year))||(ti.day>365 && !Isleap(ti.year)));
	caltime(&ti, &time);
	printf("The date is %d/%d/%d (d/m/y)\n", time.day, time.month, time.year);
}

bool Isleap(int d){
	int answer;
	if ((d % 4 == 0 && d % 100 != 0) || d % 400 == 0){
		answer = 1;
	}
	else{
		answer = 0;
	}
	return answer;
}

void caltime(indate *dd, opdate *dt){
	int mnth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int yer = dd->year;
	int idx = 0;
	int mnday = 0;
	if (Isleap(yer)){
		mnth[1]=29;
	}
	while (mnday < dd->day){
		mnday += mnth[idx];
		idx++;
	}
	dt->year = dd->year;
	dt->month = idx;
	dt->day = mnth[idx - 1] - (mnday - dd->day);
}

猜你喜欢

转载自blog.csdn.net/jzjz73/article/details/78035179