|
임의의 날짜까지의 경과일구하는건 알겠는데요..
여기서 경과일을 변경시키고 다시 그 경과일로 날짜를 출력하는 함수를 알려주세요..
밑에는 날짜에서 경과일을 출력하는 프로그램입니다.
#include "stdio.h"
int leap_year(int year);
int total_days(int year, int month, int day);
void main()
{
int year, month, day, total;
char date[][10]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
printf("enter year, month, day : ");
scanf("%d%d%d", &year, &month, &day);
total = total_days(year, month, day);
// 1980년 1월 1일이 화요일이므로 +1
printf("total days : %d / %s\n", total, date[(total+1) % 7]);
}
int leap_year(int year)
{
if (!(year % 400)) return 1;
else if (!(year % 100)) return 0;
else if (!(year % 4)) return 1;
else return 0;
}
// 1980년 1월 1일 입력하면 1을 리턴함.
// 0을 리턴해야 된다면 약간 수정해야 함
int total_days(int year, int month, int day)
{
int total=0, i;
int dayofmonth[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year < 1980) return 0;
for (i = 1980 ; i < year ; i++)
{
total += 365;
if (leap_year(i)) total++;
}
for (i = 1 ; i < month ; i++) total+=dayofmonth[i-1];
// 윤년이고 3월 이후면 2월 29일 고려 +1
if (leap_year(year) && (month>2)) total++;
total+=day;
return total;
}
|