Advertisement
arter97

Untitled

Dec 21st, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. const char *dayname[7] = { "일", "월", "화", "수", "목", "금", "토" };
  7. char print_string[300];
  8.  
  9. // tf(true/false) 정의
  10. typedef int tf;
  11. #define true 1
  12. #define false 0
  13.  
  14. // 달 출력(달(0~11), 시작요일:0~6), 반환값은 다음 달 시작요일
  15. // tf yoon_year로 윤년인지 판단
  16. // tf print가 false일 경우, 계산만 하고 리턴
  17. // today를 받아, 향후 7일 간의 일정을 표시
  18. int draw_month(int month, int start_day, int today, tf yoon_year, tf print);
  19.  
  20. int main(void)
  21. {
  22. // 유저가 종료할 때 까지 루프
  23. while (1) {
  24. int year_int, month_int, start_day, today;
  25. tf yoon_year = false;
  26.  
  27. // time.h의 strftime 함수 이용하여 현재 년/월/일 가져오기
  28. // https://www.quora.com/How-can-I-get-system-day-month-and-year-easily-by-using-C-programming-language
  29. time_t timer;
  30. struct tm* tm_info;
  31. char day[3];
  32. char month[3];
  33. char year[5];
  34. time(&timer);
  35. tm_info = localtime(&timer);
  36. strftime(day, 3, "%d", tm_info);
  37. strftime(month, 3, "%m", tm_info);
  38. strftime(year, 5, "%Y", tm_info);
  39.  
  40. // char에서 int로 변환
  41. year_int = atoi(year);
  42. month_int = atoi(month);
  43. today = atoi(day);
  44.  
  45. // 올해 1월 1일의 요일 가져오기
  46. tm_info->tm_mday = 1;
  47. tm_info->tm_mon = 1 - 1; // struct tm의 month는 0부터 시작
  48. tm_info->tm_year = year_int - 1900; // struct tm의 year는 1900부터 시작
  49. mktime(tm_info);
  50.  
  51. start_day = tm_info->tm_wday;
  52.  
  53. // 윤년 판단
  54. if ( (year_int % 4 == 0) &&
  55. ((year_int % 100 != 0) || (year_int % 400 == 0)))
  56. {
  57. yoon_year = true;
  58. }
  59.  
  60. printf("\n[ %d년 %d월 %d일 ]\n\n", year_int, month_int, today);
  61.  
  62. for (int i = 0; i < 12; i++) {
  63. //start_day = draw_month(i, start_day, today, yoon_year, true); // 모든 달 출력
  64. start_day = draw_month(i, start_day, today, yoon_year, (i + 1 == month_int));
  65. }
  66.  
  67. if (strlen(print_string) == 0) {
  68. printf("\n < 7일간 일정 없음! >\n");
  69. } else {
  70. printf("\n < 7일 일정 >\n\n");
  71. printf("%s", print_string);
  72. }
  73.  
  74. // 캘린더/일정 출력 끝, 일정 입력 시작
  75. printf("\n");
  76.  
  77. int user_day;
  78. printf("(0을 입력하면 종료)\n");
  79. printf("일정을 추가할 날을 선택해주세요 : ");
  80. scanf("%d", &user_day);
  81. if (user_day == 0)
  82. return 0;
  83.  
  84. char buffer[100];
  85. printf("메시지를 입력해주세요 : ");
  86. gets(buffer);
  87.  
  88. // 파일로 저장
  89. FILE *fp_out;
  90. char filename[9]; // 0000.txt + '\0'
  91.  
  92. filename[0] = '0' + (month_int < 10 ? 0 : month_int / 10);
  93. filename[1] = '0' + (month_int % 10);
  94. filename[2] = '0' + (today < 10 ? 0 : today / 10);
  95. filename[3] = '0' + (today % 10);
  96. filename[4] = '.';
  97. filename[5] = 't';
  98. filename[6] = 'x';
  99. filename[7] = 't';
  100. filename[8] = '\0';
  101.  
  102. fp_out = fopen(filename, "w");
  103. fputs(buffer, fp_out);
  104. fclose(fp_out);
  105. }
  106.  
  107. return 0;
  108. }
  109.  
  110. int draw_month(int month, int start_day, int today, tf yoon_year, tf print)
  111. {
  112. int i;
  113.  
  114. if (print) {
  115. // 달 출력
  116. printf(" < %d 월 >\n", month + 1);
  117.  
  118. // 구분선
  119. for (i = 0; i < 55; i++) {
  120. printf("%c", '=');
  121. }
  122. printf("\n");
  123.  
  124. // 요일 출력
  125. for (i = 0; i < 7; i++)
  126. {
  127. // 폭을 7로 설정
  128. printf("%7s", dayname[i]);
  129. }
  130. printf("\n");
  131.  
  132. // 구분선
  133. for (i = 0; i < 55; i++) {
  134. printf("%c", '-');
  135. }
  136. printf("\n");
  137.  
  138. // 첫 주 시작일까지 공백 출력
  139. for (i = 0; i < start_day; i++)
  140. {
  141. // 폭을 7로 설정
  142. printf("%7s", " ");
  143. }
  144. }
  145.  
  146. for ( i = 0;
  147. i < (
  148. (month + 1) % 2 == 0 ?
  149. // 짝수 달
  150. month + 1 == 2 ?
  151. // 2월
  152. yoon_year ?
  153. // 윤년
  154. 29
  155. :
  156. // !윤년
  157. 28
  158. :
  159. // 2월을 제외한 짝수 달
  160. month + 1 >= 8 ? 31 :30
  161. :
  162. // 홀수 달
  163. month + 1 >= 8 ? 30 : 31
  164. );
  165. i++) //일 출력
  166. {
  167. if (print) {
  168. FILE *fp_in;
  169. char buffer[100];
  170. char filename[9]; // 0000.txt + '\0'
  171.  
  172. filename[0] = '0' + (month + 1 < 10 ? 0 : (month + 1) / 10);
  173. filename[1] = '0' + (month + 1) % 10;
  174. filename[2] = '0' + (i + 1 < 10 ? 0 : (i + 1) / 10);
  175. filename[3] = '0' + (i + 1) % 10;
  176. filename[4] = '.';
  177. filename[5] = 't';
  178. filename[6] = 'x';
  179. filename[7] = 't';
  180. filename[8] = '\0';
  181.  
  182. fp_in = fopen(filename, "r");
  183. if (fp_in != NULL) {
  184. // 파일이 존재함, 일정 "유"로 표시
  185. char tmp[4];
  186. tmp[0] = (i + 1 < 10 ? ' ' : '*');
  187. tmp[1] = (i + 1 < 10 ? '*' : '0' + (i + 1) / 10);
  188. tmp[2] = '0' + (i + 1) % 10;
  189. tmp[3] = '\0';
  190. printf("%7s", tmp); //폭을 7로 설정
  191.  
  192. // 향후 7일 간의 일정 표시
  193. if (today <= i + 1 && i + 1 <= today + 7) {
  194. // 마지막 출력을 위해 저장
  195. fgets(buffer, 100, fp_in);
  196. // append
  197. strncat(print_string, filename, 2); // 월
  198. strcat(print_string, "/");
  199. strncat(print_string, filename + 2, 2); //일
  200. strcat(print_string, " : ");
  201. strcat(print_string, buffer);
  202. strcat(print_string, "\n");
  203. }
  204.  
  205. // 파일 닫기
  206. fclose(fp_in);
  207. } else {
  208. printf("%7d", i + 1); //폭을 7로 설정
  209. }
  210. }
  211.  
  212. start_day++;
  213. if (start_day == 7) //요일이 7이면
  214. {
  215. start_day = 0; //0으로 변경
  216. if (print)
  217. printf("\n");
  218. }
  219. }
  220. if (print) {
  221. printf("\n");
  222.  
  223. for (i = 0; i < 55; i++) {
  224. printf("%c", '=');
  225. }
  226. printf("\n");
  227. }
  228.  
  229. return start_day; //다음 달 시작 요일 반환
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement