본문 바로가기

과제모음

[컴프2] C언어 윤년, 일수계산

반응형

400 년마다 윤년 100년마다 윤년아님 4년마다 윤년

#include <stdio.h>

void main(){
 
 int year = -1;
 int month = 0;
 while(year !=0){
  printf("년 :");
  scanf("%d", &year);
  
  printf("월 :");
  scanf("%d", &month);
  if(month <= 0 || month > 12 || year < 0){
   printf("wrong month\n");
   return ;
  }
  if(year ==0){
   break;
  }
  else if(year%4 == 0){
   if(year % 100 == 0 && year % 400 != 0){
    printf("평년\n");
    if(month == 2){
     printf("%d 월 : 28일\n", month);
    }
    else{
     switch(month){
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12:
      printf("%d 월 : 31일\n", month);
      break;
     default:
      printf("%d 월 : 30일\n", month);
     }
    }
   }
   else{
    printf("윤년\n");
    if(month == 2){
     printf("29일\n");
    }
    else{
     switch(month){
     case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12:
      printf("%d 월 : 31일\n", month);
      break;
     default:
      printf("%d 월 : 30일\n", month);
     }
    }
   }
  }
  else{
   printf("평년\n");
   if(month == 2){
    printf("%d 월 : 28일\n", month);
   }
   else{
    switch(month){
    case 1:
     case 3:
     case 5:
     case 7:
     case 8:
     case 10:
     case 12:
     printf("%d 월 : 31일\n", month);
     break;
    default:
     printf("%d 월 : 30일\n", month);
    }
   }
  }
 }
}

내가 짜놓은 코드지만 참 더러운 코드


반응형