)기능 추가 + code refactoring
1) 달력 좌, 우 방향키로 원하는 달로 이동할 수 있음.
2) 기존 코드를 refactoring 해보았음.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
#include <windows.h>
#define LEFT 75 //ASCII코드 좌(←) 방항키
#define RIGHT 77 //ASCII코드 우(→) 방항키
#define YEAR "YEAR"
#define MONTH "MONTH"
int Input(char str[]);
void DrawCalender(int year,int month);
void MoveCalender(int year,int month);
bool Leap_year(int year);
int main(void)
{
int year = Input(YEAR);
int month = Input(MONTH);
DrawCalender(year,month);
MoveCalender(year,month);
return 0;
}
//월, 달 입력 함수
int Input(char str[])
{
int time;
printf("Please enter the desired %s: ",str); // 원하는 년도를 입력하시오.
scanf("%d",&time);
return time;
}
//달력 출력
void DrawCalender(int year,int month)
{
int sum = 365; //총 날짜 수
int start_date = 0; //1일이 시작되는 요일을 정하는 변수
int total_days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; // 1~12월까지 날짜 수
char *print_month[12] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
system("cls");
//윤년일 경우 2월에 날짜 수 1일을 더해준다.
if (Leap_year(year))
{
total_days[1]++;
}
//0년부터 입력한 년도까지 날짜를 모두 더함
for (int i = 1; i < year; i++)
{
if (Leap_year(i))
sum += 366;
else
sum += 365;
}
//남은 달의 날짜를 모두 더함
for (int i = 0; i < month - 1; i++)
{
sum += total_days[i];
}
start_date = sum % 7; //모두 더한 날짜를 7일단위로 나누어 나머지를 구함
//현재 월, 달 정보 출력
printf("\nYear : %d Month: %d\n",year,month);
printf("\n\t\t < %s >\n",print_month[month-1]);
//달력 그리기
printf("\nSUN\tMON\tTUS\tWED\tTHU\tFRI\tSAT\n");
printf("===================================================\n");
for (int i = 0; i < start_date; i++) //어디서부터 1일을 시작할지 결정
printf("\t");
for (int i = 1; i <= total_days[month - 1]; i++) //날짜 나열
{
printf("%d\t",i);
if (start_date == 6)
{
printf("\n"); // start_date가 6이 되면 7일이 한 줄에 모두 채워지기 때문에 그다음 줄로 개행시켜줘야 함
start_date = 0; // 0으로 초기화해서 다음 줄부터 다시 시작
}
else
start_date++;
}
printf("\n");
}
//달력 이동
void MoveCalender(int year, int month)
{
int chr; //키보드 입력받는 변수
while (1)
{
chr = _getch();
if (chr == 0 || chr == 0xe0)
{
chr = _getch();
switch (chr)
{
case LEFT:
system("cls");
month--;
if (month < 1)
{
month = 12;
year--;
}
DrawCalender(year,month);
break;
case RIGHT:
system("cls");
month++;
if (month > 12)
{
month = 1;
year++;
}
DrawCalender(year,month);
break;
}
}
}
}
//윤년 확인
bool Leap_year(int year)
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
|
cs |
보완할 점이나 틀린 부분 있으면 피드백 부탁드립니다. 좋은 하루 보내세요!
'Programming > C' 카테고리의 다른 글
C언어) 간단한 미로찾기 게임 (0) | 2020.06.02 |
---|---|
C언어) 환율 계산기 프로그램(달러, 원) (0) | 2020.05.02 |
C언어) 순환을 이용한 최댓값 구하기 (0) | 2020.04.29 |
C언어) 달력 만들기(1/2) 기본 (1) | 2020.03.23 |