프로그래밍 기초와 실습 Chapter 7 Enumeration Types and typedef.

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Line Efficiency     Percentage Month Today’s Date
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
21 August (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 C++
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Enumerated Types Mr. Dave Clausen La Cañada High School.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
Jan 2016 Solar Lunar Data.
Structure, Unions, typedef and enumeration
Primary Longman Elect 3A Chapter 5 Asking about dates.
Enumeration Types and typedef
Payroll Calendar Fiscal Year
Enumerations.
Q1 Jan Feb Mar ENTER TEXT HERE Notes


Average Monthly Temperature and Rainfall
Enumerations.
MON TUE WED THU
GANTT CHARTS Example Text Text Here Text Here Text Here Text Here
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
2017 Jan Sun Mon Tue Wed Thu Fri Sat

Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
January Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Jan Sun Mon Tue Wed Thu Fri Sat
2008 Calendar.
January MON TUE WED THU FRI SAT SUN
Sun Mon Tue Wed Thu Fri Sat
C Structures, Unions, Bit Manipulations and Enumerations
Electricity Cost and Use – FY 2016 and FY 2017
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Sun Mon Tue Wed Thu Fri Sat
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE

Text for section 1 1 Text for section 2 2 Text for section 3 3
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Text for section 1 1 Text for section 2 2 Text for section 3 3
Sun Mon Tue Wed Thu Fri Sat
Text for section 1 1 Text for section 2 2 Text for section 3 3
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
SEESIM 14 Timeline Jan Feb Mar Apr May Jun Jul Aug Sep Oct
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
2008 Calendar.
Presentation transcript:

프로그래밍 기초와 실습 Chapter 7 Enumeration Types and typedef

Chapter 7 Emulation Types and typedef 2 Contents  Enumeration Types  The Use of typedef

Chapter 7 Emulation Types and typedef 3 Enumeration Types  enumeration Types – 열거형을 선언하는데 사용 –int 형의 상수 [Ex] enum day { sun, mon, tue, wed, thu, fri, sat }; enum day d1, d2; d1 = fri; tag name 첫번째 원소인 sun 은 기본적으로 0 의 값을 갖고, 각 열거된 순서에 따라 1,2,.. 등의 정수 값을 가진다. 변수 d1, d2 를 enum day 형으로 선언. d1, d2 는 집합내의 원소만을 값으로 가짐 d1 에 fri 의 값을 배정하게 된다.

Chapter 7 Emulation Types and typedef 4 Enumeration Types  enum type 의 사용 예 – 참 (true) 과 거짓 (false) 으로 나타낼 수 있는 boolean type –month 이름을 표시해주는 변수 : “January”, … “December” – 카드게임 할때의 각 패를 나타내는 변수 : “clubs”, “diamonds”, “hearts”, “spades” [Ex] enum suit { clubs = 1, diamonds, hearts, spades } a, b, c; clubs 가 1 로 초기화 되었으므로, diamonds, hearts, spades 는 각각 2, 3, 4 의 값을 가진다. 선언된 변수들

Chapter 7 Emulation Types and typedef 5 Enumeration Types  enum type 의 특징 –enumeration 에서는 변수와 상수들을 integer type 로 사용한 다. [Ex] int i; enum { CLUBS, DIAMONDS, HEARTS, SPADES } s; i = DIAMONDS; /* i = 1 */ s = 0; /* s = CLUBS */ s++; /* s = 1 */ i = s + 2; /* i =3 */

Chapter 7 Emulation Types and typedef 6 Enumeration Types  정수값은 임의로 지정 가능.  중복된 정수값 지정 [Ex] enum fruit { apple = 7, pear, orange = 3, lemon } frt; [Ex] enum veg { beet = 17, carrot = 17, corn = 17 } vega1, vega2; apple 가 7 로 초기화, pear 는 8 로 초기화. orange 는 3 으로 초기화, lemon 은 4 로 초기화 된다. 식별자에게 여러 개의 값이 허용될 수 있지만, 식별자 자체는 중복되면 안되고 유일 해야한다.

Chapter 7 Emulation Types and typedef 7 Enumeration Types  macro 와 enumeration 의 비교 [Ex] macro 의 사용 #define SUIT int #define CLUBS 0 #define DIAMONDS 1 #define HEARTS 2 #define SPADES 3 SUIT s1, s2; s1 = HEARTS; s2 =DIAMONDS; [Ex] enumeration 의 사용 enum {CLUBS, DIAMONDS, HEARTS, SPADES} s1, s2; s1 = HEARTS; s2 = DIAMONDS;

Chapter 7 Emulation Types and typedef 8 The Use of typedef  typedef 는 키워드 enum 을 대신하는데 사용된다.  typedef 로 정의된 type 은 변수나 함수 선언시 보통의 type 과 똑같이 사용될 수 있다. [Ex] typedef int color; color red, blue, green; [Ex] typedef char uppercase; typedef int INCHES, FEET; typedef unsigned long size_t; /* found in stddef.h */ uppercae u; INCHES length, width; color type 을 int type 으로 지정 u 는 uppercase type 이고, 이는 char type 과 같다. length 와 width 는 INCHES type 이고, 이는 int type 과 같다.

Chapter 7 Emulation Types and typedef 9 The Use of typedef  typedef 를 사용하는 목적 1. 긴 선언문을 축약해 사용할 수 있다. 2. 사용 목적에 맞게 type 이름을 결정하여, readability 를 향상 시킬 수 있다. 3. 사용하는 컴퓨터에 따라 int 의 크기가 2 또는 4 byte 가 되는 데, 이러한 경우 typedef 를 사용함으로써 프로그램의 이식을 쉽게 할 수 있다.

Chapter 7 Emulation Types and typedef 10 The Use of typedef  enumeration tags 와 enumeration types 의 비교 [Ex] enumeration tags enum suit { CLUBS, DIAMONDS, HEARTS, SPADES}; enum suit s1, s2; [Ex] enumeration types typedef enum {CLUBS, DIAMONDS, HEARTS, SPADES} enumT; enumT s1, s2;

Chapter 7 Emulation Types and typedef 11 The Use of typedef  Compute the next day [Ex] 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 tue; next_day = wed; break; case wed; next_day = thu; break; case thu; next_day = fri; break; case fri; next_day = sat; break; case sat; next_day = sun; break; } return next_day; }

Chapter 7 Emulation Types and typedef 12 The Use of typedef  앞의 예제를 cast 를 사용하여 나타낸 code [Ex] enum day { sun, mon, tue, wed, thu, fri, sat }; typedef enum day day; day find_next_day(day d) { day next_day; next_day = (day) ( ( (int) d + 1 ) % 7 ) ; return next_day; }

Chapter 7 Emulation Types and typedef 13 Enumeration  Enumberation 과 typedef 를 이용한 다음 날짜를 구하 는 프로그램 작성예제 –enum 으로 month 를 입력한다. – 윤년은 계산하지 않고 2 월 28 일 까지만 있다고 가정한다. –31 일까지 있는 달은 1, 3, 5, 7, 8, 10, 12 월 –30 일까지 있는 달은 4, 6, 9, 11 월 –28 일까지 있는 달은 2 월

Chapter 7 Emulation Types and typedef 14 Enumeration [Ex] enum month { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; typedef enum month month; void next_day(month month, int day) { int nextday = day, nextmonth = month, last_day; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: last_day = 31 ; break ; case 4: case 6: case 9: case 11: last_day = 30 ; break ; case 2: last_day = 28 ; break ; } 다음 날짜를 구하는 함수 case Apr: case Jun: case Sep: case Nov:

Chapter 7 Emulation Types and typedef 15 Enumeration if ( day == last_day ) { nextday = 1 ; nextmonth++ ; } else nextday++ ; if (month==12 && day ==31) nextmonth=1 ; printf("The next day is %d/%d\n",nextmonth, nextday); }

Chapter 7 Emulation Types and typedef 16 오늘 날짜를 입력하시오. 월 : 11 일 : 5 내일은 11 월 6 일 입니다.

Chapter 7 Emulation Types and typedef 17 Style  다음과 같은 구조적인 code 는 읽기 쉽다. [Ex] enum bool { false, true }; enum off_on { off, on }; enum no_yes { no, yes }; enum speed { slow, fast }; [Ex] enum veg { beet, carrot, corn } veg; /* poor style */ typedef enum { beet, carrot, corn } veg; /* good style */

수고하셨습니다 Enumeration Types and typedef