[프로그램] 달력 만들기
달력을 만들기 위한 명세서 입니다.
1. 1년을 계산 한다.
2. 윤년을 체크 하여 더한다.
3. 한달의 시작점을 찾는다. ( 1년 1월 1일은 월요일 이다. )
4. 출력 한다.
1년을 계산
int day[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char *week[] = {"sun","mon","tues","wed","thu","fri","sat"};
날 수를 계산 합니다.
int year = 2020;
days = ((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400);
( year - 1 ) * 365
년도에 따른 날 수를 계산 합니다.
+((year - 1) / 4)
윤년이 있는 해를 더합니다.-((year - 1) / 100)
100년마다 윤년이 있는 해는 뺀다.
+((year - 1) / 400)
400년이 있는 해는 더한다.
윤년 계산
int day[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int month =4;
for (int i = 0; i < month - 1; i++)
{
if ( month == 2 )
{ // 2 월달
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) //윤년(2/29)
{
day[1] = 29;
}
}
days += day[i];
}
월의 첫 시작 찾기
int first_day = 0;
first_day = days % 7;
printf("%d %d \r\n",first_day,days);
first_day 는 첫 시작 점을 잡기 위한 변수다.
days % 7 은 모두 더한 날 수에서 7로 나눈 값의 나머지 값이다.
출력
int count = 0;
int curday = 24 ;
for (int i = 0; i <= first_day; i++)
{
printf("\t");
count++;
}
for (int i = 1; i <= day[month - 1]; i++)
{
if (count >= 7)
{
printf("\n");
count = 0;
}
if(curday == i)
{
printf("[%d,%s]\t", i,week[count]);
}
else
{
printf("%d\t", i);
}
count++;
}
2020년 4월 24일을 출력 하였습니다.
출력 된 값은 아래 입니다.
scanf로 입력을 받아서 출력 하였습니다.