본문 바로가기

과제모음

[컴프2]C언어 입력받은 연도,달의 달력출력

반응형
#include <stdio.h>
// 입력한 연도와 달의 달력을 출력하는 프로그램

void main(){
	int i;
	int iY, iM;
	int ptday,flag;
	int cntday =0;
	iY = -1;
	while(iY != 0){
		printf("\n\nWhen Year = 0 -> program end\n");
		printf("Year : ");
		scanf("%d", &iY);
		if(iY == 0){
			break;
		}
		printf("Month : ");
		scanf("%d", &iM); 

		if(iY < 1978 || iM <= 0 || iM > 12){
			printf("Wrong Input!!!!\n");
		}
		else{
			for(i = 1978; i<iY; i++){
if(i%4 == 0 && i%100 != 0 || i%400 ==0){ // 윤년이면
	cntday += 366; 
}
else{ // 윤년아니면
	cntday += 365;
}
}
for(i = 1; i<iM; i++){
	switch(i){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		cntday += 31;
		break;
		case 2:
if(iY%4 == 0 && iY%100 != 0 || iY%400 ==0){ // 윤년이면
	cntday += 29; 
}
else{
	cntday += 28;
}
break;
default:
cntday += 30;
}
}

cntday %= 7;
printf("일\t월\t화\t수\t목\t금\t토\n"); // 요일출력
switch(cntday){ // 요일에 따라 플래그 줌(배열을 사용 안하므로)
	case 0:
	flag = 0;
	break;
	case 1:
	printf("\t");
	flag = 1;
	break;
	case 2:
	flag = 2;
	printf("\t\t");
	break;
	case 3:
	flag = 3;
	printf("\t\t\t");
	break;
	case 4:
	flag = 4;
	printf("\t\t\t\t");
	break;
	case 5:
	flag = 5;
	printf("\t\t\t\t\t");
	break;
	default :
	flag = 6;
	printf("\t\t\t\t\t\t");
}

switch(iM){ // 월에따른 날짜
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	ptday = 31;
	break;
	case 2:
if(iY%4 == 0 && iY%100 != 0 || iY%400 ==0){ // 윤년이면
	ptday = 29; 
}
else{
	ptday = 28;
}
break;
default:
ptday = 30;
}

for(i=1; i<=ptday; i++){ // 날짜출력
	printf("%d\t", i);
	flag++;
if(flag == 7){ // 토요일이면 줄바꿈
	printf("\n");
flag = 0; // 플래그 초기화
}
}
printf("\n");
}
}
} 
반응형