데블스 캠프 넷째날 만년달력 코드 ---- {{{~cpp #include #include 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 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; } }}} 네이버에서 만년달력 검색하다가 들어오게된 페이지. 하하하 - [이승한] ---- [데블스캠프2003/넷째날] [만년달력]