컴퓨터 엑셀 워드 포토샵 구글어스 WINDOWS JAVASCRIPT JAVA C++

 
Wednesday, January 31, 2007

C언어] 올해가 윤년인지 평년인지 판단 예제; Current Year is Leap Year?


이번 해가 윤년인지 아닌지 알아내는 방법입니다.

여기에 있는 isLeapYear 함수를 사용합니다: ▶▶ C언어] 특정 연도, 윤년 여부 판단 함수; is Leap Year Function

그리고 getCurrentYear() 함수로, 이번 년도를 4자리로 구합니다.

단, 서기 1582년이 되기 전의 윤년은 구할 수 없습니다. 즉, 현행 그레고리력의 윤년만 구할 수 있고, 그 전의 율리우스력의 윤년은 구하지 못합니다.

이번 해가 윤년인지 평년인지 알아내기 예제


소스 파일명: example.cpp
#include <stdio.h>
#include <time.h>

int isLeapYear(int year);
int getCurrentYear(void);


int main(void) {

  int cyear = getCurrentYear();

  printf("올해는 %d 년입니다.\n", cyear);

  if (isLeapYear(cyear)) {
    puts("윤년입니다");
  } else {
    puts("평년입니다");
  }

  return 0;
}




int isLeapYear(int year) {
  if (year % 4)   return 0;
  if (year % 100) return 1;
  if (year % 400) return 0;

  return 1;
}


int getCurrentYear(void) {
  time_t timer;
  struct tm *t;

  timer = time(NULL);
  t = localtime(&timer);

  return t->tm_year + 1900;
}



컴파일 및 실행 결과 화면:
(컴퓨터 시간을 바꾸어 가며 실행시킨 결과임)
D:\Z>cl /nologo example.cpp && example.exe
example.cpp
올해는 2007 년입니다.
평년입니다

D:\Z>cl /nologo example.cpp && example.exe
example.cpp
올해는 2008 년입니다.
윤년입니다

D:\Z>cl /nologo example.cpp && example.exe
example.cpp
올해는 2004 년입니다.
윤년입니다

D:\Z>cl /nologo example.cpp && example.exe
example.cpp
올해는 2001 년입니다.
평년입니다

D:\Z>cl /nologo example.cpp && example.exe
example.cpp
올해는 2000 년입니다.
윤년입니다

D:\Z>





tag: cpp
C언어 | C/C++ (Visual C++)

0 Comments:

Post a Comment

<< Home RSS 2.0 feed

구글 Google 에서 제공하는 무료 블로그 서비스인 블로거 Blogger 의 인터넷 주소는 www.blogger.com 입니다. Blogger 에 블로그를 만들면, blogspot.com 이라는 주소에 블로그가 생성됩니다.
블로그를 직접 방문하지 않고도 최신 게시물을 구독하려면 RSS 2.0 feed 주소를 리더기에 등록하시면 됩니다.
Previous Posts
Monthly Archives
Top