ë�°ë¸”스 ìº í”„ ë„·ì§¸ë‚ ë§Œë…„ë‹¬ë ¥ 코드
~cpp
#include <iostream>
#include <climits>
using namespace std;
void output(int , int);
int deter_date(int, int);
int lastdays(int, int);
int how_much_days(int, int);
int main()
{
int year, month;
while (true)
{
cout << "ì—°ë�„를 ìž…ë ¥í•˜ì„¸ìš” : ";
cin >> year;
cout << "ì›”ì�„ ìž…ë ¥í•˜ì„¸ìš” : ";
cin >> month;
if ( !year || !month )
break;
if ( year <= 0 || year >INT_MAX || month <=0 || month>12)
{
cout << "잘못 ìž…ë ¥í•˜ì…¨ìŠµë‹ˆë‹¤." << endl;
continue;
}
output(year, month);
}
return 0;
}
void output(int year, int month)
{
int days = how_much_days(year, month);
int date;
if (year%400 != 0)
date = deter_date(year%400, month);
//삽질(?) year%400 ëŒ€ì‹ yearì�„ ì“°ë©´ ì—�러...스íƒ� 오버플로우?
//400년주기로 ë‹¬ë ¥ì�´ 같으므로 year%400로 해서 í•´ê²°.
else
date = deter_date(400, month);
cout << "====================================================" << endl
<< "ì�¼\tì›”\tí™”\t수\t목\t금\tí† " << endl;
for ( int j=0 ; j<date ; j++) //숫ìž�를 ì°�기 ì „ì—� ìš”ì�¼ë§Œí�¼ 빈칸ì�„ ì°�어줌
cout << "\t";
for ( int i=0 ; i<days ; i++) //1ì—�서 days까지 ì¶œë ¥
{
if ( date > 6 ){ //í† ìš”ì�¼ì�„ 넘어가면
cout << endl; //다ì�Œì¤„로 ê°€ê³
date=0; //요�� �요�으로
}
cout << i+1 << "\t";
date++; //보통때는 요�� �가
}
cout << endl << "====================================================" << endl;
}
int deter_date(int year, int month )//ìš”ì�¼ì�„ ì •í•˜ëŠ” 함수(0ì�€ ì�¼ìš”ì�¼, 6ì�€ í† ìš”ì�¼)
{
if ( month == 0 ){
year--;
month = 12;
}
else if ( year == 1 && month == 1)
return 1; // 1년 1월�는 월요�부터 시작
return (lastdays(year,month) + deter_date(year, month-1)) % 7;//핵심 코드
/*
지난 달ì�´ ë©°ì¹ ì�¸ì§€ë¥¼ ì•Œê³ ê±°ê¸°ì—� 지난달ì�´ 시작하는 ìš”ì�¼ì�„ 알면
�번달� 무슨 요��서 시작하는지 알 수 있다. 예를 들어 1년 2월 같� 경우
1ë…„ 1ì›”ì�´ 31ì�¼ì�´ê³ 월요ì�¼ë¶€í„° 시작하므로 (31 + 1) % 7 == 4 로
목요�부터 시작함� 알 수 있다.
*/
}
int lastdays(int year, int month)//지난 달 ë‚ ìˆ˜ë¥¼ 계산
{
int last;
switch(month)
{
case 3:
if ( year%4 == 0)
{
last = 29;
if ( year%400 == 0 )
last = 29;//윤달
if ( year%4000 == 0 )
last = 28;//�달
else if ( year%100 == 0 )
last = 28;//�달
}
else
last = 28;
break;
case 5: case 7: case 10: case 12:
last = 30;
break;
case 1: case 8: case 2:
last = 31;
break;
case 4: case 6: case 9: case 11:
last = 31;
break;
}
return last;
}
int how_much_days(int year, int month)
{
int days;
switch(month)//한 달 ë‚ ì§œë¥¼ ì •í•´ì¤Œ
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 2:
if ( year%4 == 0)
{
days = 29;
if ( year%400 == 0 )
days = 29;//윤달�경우
if ( year%4000 == 0)
days = 28;//�달� 경우
else if ( year%100 == 0 )
days = 28;//�달�경우
}
else//�달� 경우
days = 28;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
}
return days;
}
네ì�´ë²„ì—�서 ë§Œë…„ë‹¬ë ¥ 검색하다가 들어오게ë�œ 페ì�´ì§€. 하하하 - ì�´ìŠ¹í•œ










