Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. ## 40
  2.  
  3. 시작 년월일, 종료 년월일을 입력받아 날짜 수를 세는 프로그램을 작성하시오.
  4.  
  5. 실행 예>
  6.  
  7. ```
  8. $ make
  9. $ ./count_days
  10. start year: 1999
  11. start month: 3
  12. start day: 6
  13. end year: 2003
  14. end day: 6
  15.  
  16. 1999/3/6~2003/5/6: 1522day(s)
  17. ```
  18.  
  19. ### NAME
  20.  
  21. #### count_days
  22.  
  23. ### SYNOPSIS
  24.  
  25. #### memset
  26.  
  27. ```
  28. #include <string.h>
  29.  
  30. void * memset(void *b, int c, size_t len);
  31. ```
  32.  
  33. #### mktime
  34.  
  35. ```
  36. #include <time.h>
  37. time_t mktime(struct tm *timeptr);
  38. ```
  39.  
  40. #### difftime
  41.  
  42. ```
  43. #include <time.h>
  44. double difftime(time_t time1, time_t time0);
  45. ```
  46.  
  47. ### cunt_days.c Code
  48.  
  49. `difftime()`을 사용하기 위해서는 비교할 `time_t` 구조의 `time0`, `time1`이 필요합니다.
  50. `difftime()`의 반환값으로 `sec` 단위의 `double`형 값을 반환하기 때문에 이값을 출력전에
  51. `diff_day` 변수에 `sec`->`day` 단위로 변환후 출력값으로 전달했습니다.
  52.  
  53. ```
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <time.h>
  57.  
  58. int
  59. main(int argc,
  60. char *argv[])
  61. {
  62. int start_year, start_mon, start_day;
  63. int end_year, end_mon, end_day;
  64. int diff_day;
  65. double diff_sec;
  66. struct tm user_time;
  67. time_t start_time;
  68. time_t end_time;
  69.  
  70. memset(&user_time, 0, sizeof(user_time));
  71. printf("start year: ");
  72. scanf("%d", &start_year);
  73. printf("start month: ");
  74. scanf("%d", &start_mon);
  75. printf("start day: ");
  76. scanf("%d", &start_day);
  77. user_time.tm_year = start_year - 1900;
  78. user_time.tm_mon = start_mon - 1;
  79. user_time.tm_mday = start_day;
  80.  
  81. start_time = mktime( &user_time);
  82.  
  83. memset(&user_time, 0, sizeof(user_time));
  84. printf("end year: ");
  85. scanf("%d", &end_year);
  86. printf("end mon: ");
  87. scanf("%d", &end_mon);
  88. printf("end day: ");
  89. scanf("%d", &end_day);
  90. user_time.tm_year = end_year - 1900;
  91. user_time.tm_mon = end_mon - 1;
  92. user_time.tm_mday = end_day;
  93.  
  94. end_time = mktime( &user_time);
  95.  
  96. diff_sec = difftime(end_time, start_time);
  97. diff_day = diff_sec / (60 * 60 * 24);
  98.  
  99. printf("%4d/%2d/%2d ~ %4d/%2d/%2d: %d day(s)\n", start_year, start_mon, start_day, end_year, end_mon, end_day, diff_day);
  100.  
  101. return 0;
  102. }
  103.  
  104. ```
  105.  
  106. ## 41
  107.  
  108. 년월일을 입력받아 그 날짜에 100일 지난 년, 월, 일을 출력하는 프로그램을 작성하시오.
  109.  
  110. 실행예>
  111.  
  112. ```
  113. $ make
  114. $ ./add_100
  115. year : 1999
  116. month : 12
  117. day : 10
  118. 1999/12/10 + 100 = 2000/3/19
  119. ```
  120.  
  121. ### NAME
  122.  
  123. #### add_100
  124.  
  125. ### SYNOPSIS
  126.  
  127. #### mktime
  128.  
  129. ```
  130. #include <time.h>
  131. time_t mktime(struct tm *timeptr);
  132. ```
  133.  
  134. ### add_100.c Code
  135.  
  136. `struct tm user_time` 선언후 입력 받는 년, 월, 일 저장합니다.
  137. 저장된 `tm_mday`에 `100`을 더한후 `mktime()` 사용시 추가된 `100`일만큼 년, 월, 일이
  138. 자동 계산 됩니다.
  139.  
  140. ```
  141. #include <stdio.h>
  142. #include <string.h>
  143. #include <time.h>
  144.  
  145. int
  146. main(int argc,
  147. char *argv[])
  148. {
  149. int input_year, input_mon, input_day;
  150. struct tm user_time;
  151.  
  152. memset(&user_time, 0, sizeof(user_time));
  153.  
  154. printf("year: ");
  155. scanf("%d", &input_year);
  156. printf("month: ");
  157. scanf("%d", &input_mon);
  158. printf("start day: ");
  159. scanf("%d", &input_day);
  160.  
  161. user_time.tm_year = input_year - 1900;
  162. user_time.tm_mon = input_mon - 1;
  163. user_time.tm_mday = input_day;
  164.  
  165. user_time.tm_mday += 100;
  166. mktime(&user_time);
  167.  
  168. printf("%4d/%2d/%2d + 100 = %4d/%2d/%2d\n",
  169. input_year,
  170. input_mon,
  171. input_day,
  172. user_time.tm_year + 1900,
  173. user_time.tm_mon + 1,
  174. user_time.tm_mday
  175. );
  176.  
  177. return 0;
  178. }
  179. ```
  180.  
  181. ## 42
  182.  
  183. 일수를 입력받아 1월 1일 이후에서의 해당하는 날짜를 출력하는 프로그램을 작성하시오.
  184.  
  185. 실행예>
  186.  
  187. ```
  188. $ make
  189. $ select_day
  190. day : 100
  191. 1/1 + 100 : 4/10
  192. day : 1
  193. 1/1 + 1 : 1/2
  194. ```
  195.  
  196. ### NAME
  197.  
  198. #### select_day
  199.  
  200. ### SYNOPSIS
  201.  
  202. #### time
  203.  
  204. ```
  205. #include <time.h>
  206. char * time(time_t *t);
  207. ```
  208.  
  209. #### localtime
  210.  
  211. ```
  212. #include <time.h>
  213. struct tm * localtime(const time_t *clock);
  214. ```
  215.  
  216. #### mktime
  217.  
  218. ```
  219. #include <time.h>
  220. time_t mktime(struct tm *timeptr);
  221. ```
  222.  
  223. ### select_day.c Code
  224.  
  225. `time`과 `localtime` 을 사용해서 현재 년도에 해당하는 `struct tm *local_time`을 만듭니다.
  226. 입력받는 일수를 `local_time->tm_mday += input_day` 이후 `mktime`를 사용해서 입력받은
  227. 날짜만큼 현재년도 `1/1` 추가된 일자를 출력하도록 했습니다.
  228.  
  229. ```
  230. #include <stdio.h>
  231. #include <string.h>
  232. #include <time.h>
  233.  
  234. int
  235. main (int argc,
  236. char *argv[])
  237. {
  238. int input_day;
  239.  
  240. time_t current_time;
  241. time(&current_time);
  242.  
  243. struct tm *local_time;
  244. local_time = localtime(&current_time);
  245. local_time->tm_year += 1900;
  246. local_time->tm_mon = 0;
  247. local_time->tm_mday = 1;
  248. local_time->tm_hour = 0;
  249. local_time->tm_min = 0;
  250. local_time->tm_sec = 0;
  251.  
  252. printf("day: ");
  253. scanf("%d", &input_day);
  254. local_time->tm_mday += input_day;
  255. mktime(local_time);
  256.  
  257. printf("1/1 + %d: %2d/%2d\n", input_day, local_time->tm_mon + 1, local_time->tm_mday);
  258.  
  259. return 0;
  260. }
  261. ```
  262.  
  263. ## 43
  264.  
  265. 문장을 입력 받아 거꾸로 출력하는 reverse() 함수를 작성하시오.
  266.  
  267. 실행 예>
  268.  
  269. ```
  270. input string : hello
  271. <reverse>
  272. o l l e h
  273. ```
  274.  
  275. ### NAME
  276.  
  277. #### reverse_change
  278.  
  279. ### SYNOPSIS
  280.  
  281. #### strlen
  282.  
  283. ```
  284. #include <string.h>
  285. size_t strlen(const char *s);
  286. ```
  287.  
  288.  
  289. ### reverse_change.c Code
  290.  
  291. 43번과 44번 문제의 차이점을 모르겠습니다.
  292. 문장(스트링)을 입력받아 거꾸로(내용을 뒤집어) 출력하는 것의 차이점이
  293. 무엇인지 몰라서 하나만 작성했습니다.
  294.  
  295. 문장을 입력받아 `reverse_change()`에 문자열 주소를 전달하고 문장의 길이를 `strlen()`으로 구한후
  296. 뒤에서 부터 출력하도록 했습니다.
  297.  
  298. ```
  299. #include <stdio.h>
  300. #include <string.h>
  301.  
  302. #define BUF_SIZE 1023
  303.  
  304. void
  305. reverse_change (char *str)
  306. {
  307. int i;
  308. int str_len = strlen(str);
  309.  
  310. puts("<after reverse..>");
  311. printf("Output string: ");
  312.  
  313. for (i=(str_len - 1); i>=0; i--) {
  314. //printf("[%c]", str[i]); //DEBUG
  315. printf("%c", str[i]);
  316. }
  317. puts("");
  318.  
  319. }
  320.  
  321. int
  322. main (int argc,
  323. char *argv[])
  324. {
  325. char buf[BUF_SIZE];
  326. printf("input string: ");
  327. scanf("%s", buf);
  328.  
  329. reverse_change(buf);
  330.  
  331. return 0;
  332. }
  333. ```
  334.  
  335. ## 45
  336.  
  337. 한 문장을 입력받아 정해진 step만큼 소스를 바꾸는 프로그램을 작성하시오.
  338.  
  339. 정해진 `step`이 무엇을 의미하는지 파악 못해서 작성하지 못했습니다.
  340. (문자를 ENCODER 시키는건데 어떤 형식인지 모릅니다.)
  341.  
  342. 실행 예 >
  343.  
  344. ```
  345. Input step : 2
  346. Input string : I LOVE YOU
  347. SOURCE STRING : I LOVE YOU
  348. ENCODER STRING : K NQXG AQW
  349. DECODER STRING : I LOVE YOU
  350. ```
  351.  
  352. ## 46
  353.  
  354. 파일에서 총 문자수, 대문자, 소문자, 모음의 개수, 알파벳의 빈도수를 세는 프로그램을 작성하시오.
  355.  
  356. 실행 예>
  357.  
  358. ```
  359. total = 1189
  360. big = 14
  361. small = 890
  362. vowel = 342
  363. A : 64 B : 23 C : 9 D : 37 E : 132
  364. F : 18 G : 14 H : 49 I : 69 J : 0
  365. K : 2 L : 45 M : 15 N : 58 O : 63
  366. P : 24 Q : 1 R : 49 S : 72 T : 105
  367. U : 14 V : 8 W : 21 X : 0 Y : 10
  368. Z : 2
  369. ```
  370.  
  371. ### NAME
  372.  
  373. #### count
  374.  
  375. ### SYNOPSIS
  376.  
  377. #### fopen
  378.  
  379. ```
  380. #include <stdio.h>
  381.  
  382. FILE * fopen(const char *restrict filename, const char *restrict mode);
  383. ```
  384.  
  385. #### fgetc
  386.  
  387. ```
  388. #include <stdio.h>
  389.  
  390. int fgetc(FILE *stream);
  391. ```
  392.  
  393. #### fclose
  394.  
  395. ```
  396. #include <stdio.h>
  397.  
  398. int fclose(FILE *stream);
  399. ```
  400.  
  401. ### count.c Code
  402.  
  403. 겨울 왕국 대본인 `winter.txt`를 `fopen`으로 연후 `fgetc`으로 한글자씩 비교하며
  404. 총문자수, 대소문자, 모음의 개수, 알파벳의 빈도수를 세아렸습니다.
  405.  
  406. ```
  407. #include <stdio.h>
  408. #include <stdlib.h>
  409.  
  410. typedef struct _dic {
  411. int total;
  412. int big;
  413. int small;
  414. int vowel;
  415. int etc;
  416. int count[26];
  417. } Dic;
  418.  
  419. int
  420. main (int argc,
  421. char *argv[])
  422. {
  423.  
  424. FILE *fp;
  425. int i;
  426. char ch;
  427. Dic data;
  428.  
  429. for (i = 0; i <= 25; i++)
  430. {
  431. data.count[i] = 0;
  432. }
  433.  
  434. if ((fp = fopen ("./winter.txt", "r")))
  435. {
  436. while(EOF != (ch = fgetc(fp)))
  437. if ('a' <= ch && ch <= 'z')
  438. {
  439. if ('a' == ch || 'e' == ch || 'i' == ch || 'o' == ch || 'u' == ch)
  440. data.vowel++;
  441.  
  442. if (97 <= ch && ch <= 122)
  443. data.count[ch - 97]++;
  444.  
  445. data.total++;
  446. data.small++;
  447. }
  448. else if ('A' <= ch && ch <= 'Z') {
  449. if ('A' == ch || 'E' == ch || 'I' == ch || 'O' == ch || 'U' == ch)
  450. data.vowel++;
  451.  
  452. if (65 <= ch && ch <= 90)
  453. data.count[ch - 65]++;
  454.  
  455. data.total++;
  456. data.big++;
  457. }
  458. else if ('0' <= ch && ch <= '9') {
  459. data.total++;
  460. data.small++;
  461. }
  462. else {
  463. data.total++;
  464. data.etc++;
  465. }
  466. }
  467. else {
  468. printf("Open error\n");
  469. exit(-1);
  470. }
  471.  
  472. printf("total = %d\n", data.total);
  473. printf("big = %d\n", data.big);
  474. printf("small = %d\n", data.small);
  475. printf("vowel = %d\n", data.vowel);
  476. printf("etc = %d\n", data.etc);
  477.  
  478. int count = 0;
  479.  
  480. for (i=0; i<=25; i++) {
  481. if (0 <= i && i <= 24) {
  482. printf("%c: %5d ",(65 + i), data.count[i]);
  483. }
  484. else {
  485. printf("%c: %5d\n",(65 + i), data.count[i]);
  486. }
  487.  
  488. if (count == 4) {
  489. printf("\n");
  490. count = 0;
  491. }
  492. else count++;
  493. }
  494. printf("\n");
  495. fclose(fp);
  496.  
  497. return 0;
  498. }
  499. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement