1 / 11

Enumeration Types and typedef

7. chapter. Enumeration Types and typedef. 내 용. Enumeration Types The Use of typedef An Example : The Game of Paper, Rock, Scissors. Enumeration Types. 열거형 열거형을 선언하는데 사용 int 형의 상수 예. 태그이름 (tag name). enum day { sun, mon, tue, wed, thu, fri, sat }; enum day d1, d2 ; d1 = fri;.

zeus-davis
Télécharger la présentation

Enumeration Types and typedef

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 7 chapter Enumeration Types and typedef

  2. 내 용 • Enumeration Types • The Use of typedef • An Example : The Game of Paper, Rock, Scissors

  3. Enumeration Types • 열거형 • 열거형을 선언하는데 사용 • int 형의 상수 • 예 태그이름(tag name) enum day { sun, mon, tue, wed, thu, fri, sat }; enum day d1, d2; d1 = fri; 첫번째 원소인 sun은 기본적으로 0의 값을 갖고, 각 열거된 순서에 따라 1,2,.. 등의 정수 값을 가진다. 변수 d1, d2를 enum day 형으로 선언.d1, d2는 집합내의 원소만을 값으로 가짐

  4. Enumeration Types • 열거자 • 초기화 가능 • 선언된 후 바로 이여 변수들을 선언 가능 • 예 1 • 예 2 enum suit {clubs = 1, diamonds, hearts, spades} a, b, c; diamonds=2, hearts=3, spades=4 enum fruit {apple = 7, pear, orange = 3, lemon} frt; pear=8, lemon=4

  5. Enumeration Types • 열거자 • 태그 이름이 없을 수도 있다. • 한 함수내의 변수와 열거자는 서로 다른 유일한 식별자를 가져야 한다. enum {fir, pine} tree;

  6. The Use of typedef • typedef • 특정한 응용에 적합한 형의 이름을 사용할 수 있도록 한다. • 열거형 및 구조형과 같은 복잡하고 긴 사용자 정의 형을 프로그래머가 만들 때, 그 복잡성을 제어하는 데 도움이 된다. • typedef로 정의된 type은 변수나 함수 선언시 보통의 type과 똑같이 사용될 수 있다. • 예 color를 int와 동일한 형으로 만듬 Typedef int color; Color red, blue, green; 다른 보통의 형이 사용되는 것과 똑같은 방법으로 color 형을 선언에서 사용

  7. The Use of typedef • typedef를 사용하는 목적 • 긴 선언문을 축약해서 사용 • 사용 목적에 맞게 type이름을 결정하여, readability를 향상 • 사용하는 컴퓨터에 따라 int 의 크기가2또는 4 byte가 되는데, 이러한 경우 typedef를 사용함으로써 프로그램의 이식을 쉽게 함

  8. The Use of typedef • 예제 : 다음 날을 계산하는 프로그램 enum day {sun, mon, tue, wed, thu, fri, sat}; typedef enum day day; day find_next_day(day d) { day next_day; switch (d) { case sun: next_day = mon; break; case mon: next_day = tue; break; case sat: next_day = sun; break; } return next_day; } 캐스트 연산자를 사용한 경우 enum day {sun, man, tue, wed, thu, fri, sat}; typedef enum day day; day find_next_day(day d) { return ((day) (((int) d + 1) % 7)); }

  9. An Example : The Game of Paper, Rock, Scissors • 가위,바위,보 게임- In file p_r_s.h: #include <ctype.h>      /* for isspace() */ #include <stdio.h>      /* for printf(), etc */ #include <stdlib.h>     /* for rand() and srand() */ #include <time.h>      /* for time() */ enum p_r_s {paper, rock, scissors, game, help, instructions, quit}; enum outcome {win, lose, tie, error}; typedef enum p_r_s p_r_s; typedef enum outcome outcome; outcome compare(p_r_s player_choice, p_r_s machine_choice); void prn_final_status(int win_cnt, int lose_cnt); void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt); void prn_help(void); void prn_instructions(void); void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr); p_r_s selection_by_machine(void); p_r_s selection_by_player(void);

  10. An Example : The Game of Paper, Rock, Scissors • 가위,바위,보 게임-In file main.c: #incude p_r_s.h int main(void) { int   win_cnt = 0, lose_cnt = 0, tie_cnt = 0; outcomeresult; p_r_s   player_choice, machine_choice; . srand(time(NULL));  /* seed the random number generator */ prn_instructions(); enum outcome {win, lose, tie, error}; enum p_r_s {paper, rock, scissors, game, help, instructions, quit}; 사용자에게 게임 방법을 알려줌

  11. An Example : The Game of Paper, Rock, Scissors • 가위,바위,보 게임-In file main.c: while ((player_choice = selection_by_player()) != quit) switch (player_choice) { case paper: case rock: case scissors: machine_choice = selection_by_machine(); result = compare(player_choice, machine_choice); report(result, &win_cnt, &lose_cnt, &tie_cnt); break; case game: prn_game_status(win_cnt, lose_cnt, tie_cnt); break; case instructions: prn_instructions(); break; case help: prn_help(); break; default: printf("PROGRAMMER ERROR: Cannot get to here!\n\n"); exit(l); } 게임을 하기 위해 컴퓨터와 사용자는 가위, 바위, 보 중 하나를 선택 게임을 하기 위해 컴퓨터와 사용자는 가위, 바위, 보 중 하나를 선택 컴퓨터와 사용자의 가위, 바위, 보 중 하나를 선택 사용자에게 한 회 경기의 결과를 알려주고, 승, 패, 무승부의 수를 증가

More Related