Chapter 10 unions
This presentation is the property of its rightful owner.
Sponsored Links
1 / 47

Chapter 10 結構 , Unions, 位元處理與列舉型別 PowerPoint PPT Presentation


  • 41 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 10 結構 , Unions, 位元處理與列舉型別. 大綱 10.1 簡介 10.2 結構定義 10.3 結構初始值設定 10.4 存取結構成員 10.5 使用結構與函數 10.6 typedef 10.7Example: 高效率的洗牌與發牌 10.8Unions 10.9 位元運算子 10.10 位元欄位 10.11 列舉型別常數. 10.1 簡介. 結構 聚合型的資料型態,由其他型態的元素組成,不同型態資料的集合。 彼此相關的變數結合成 ( 混合體 ) 相同的名稱 包含不同型態資料

Download Presentation

Chapter 10 結構 , Unions, 位元處理與列舉型別

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 10 unions

Chapter 10 結構, Unions, 位元處理與列舉型別

大綱

10.1簡介

10.2結構定義

10.3結構初始值設定

10.4存取結構成員

10.5使用結構與函數

10.6typedef

10.7Example:高效率的洗牌與發牌

10.8Unions

10.9位元運算子

10.10位元欄位

10.11列舉型別常數


Chapter 10 unions

10.1 簡介

  • 結構

    • 聚合型的資料型態,由其他型態的元素組成,不同型態資料的集合。

    • 彼此相關的變數結合成 (混合體) 相同的名稱

      • 包含不同型態資料

    • 通常來定義儲存在檔案內的記錄(records)

    • 指標和結構可以構成複雜的資料結構,如:

      • 鏈結串列 linked lists,

      • 佇列 queues,

      • 堆疊 stacks 和

      • 樹 trees


Chapter 10 unions

結構標籤(tag)

結構成員

struct card

*face

*suit

10.2 結構定義

  • Example

    struct card {

    char *face;

    char *suit;};

    • struct 定義結構 card

    • card為結構的名稱,可和struct一起用來宣告該結構型別的變數

    • card包含兩個型別為 char * 的成員變數

      • 成員變數為 face和 suit

    • 同一個結構中的成員名稱不可相同

    • 不同結構中可以有相同名稱的成員

    • 每個結構定義必須由分號結束

    • 結構的成員可以是自己之外的任何型態


Chapter 10 unions

struct employee

firstname[20]

lastname[20]

age

gender

hourlySalary

*rPtr

10.2 結構定義

  • Examples:

    struct employee {

    char firstname[20];

    char lastname[20];

    int age;

    char gender;

    double hourlySalary;

    struct employee2 person; /*Error */

    struct employee2 *rPtr;

    };


Chapter 10 unions

aCard

*face

*suit

Deck[52]

*face

*suit

*face

*suit

*face

*cardPtr

*suit

*face

*suit

10.2 結構定義

  • 自我參照的結構 self-referential structure

    • 結構中任一個成員的資料型態都不能為該結構本身。

    • 成員中有指向結構本身的指標

    • 一個結構定義並沒有佔用任何記憶體

      • 建立一個新的資料型別,以供變數宣告使用

  • 定義

    • 結構變數的宣告方式與宣告其他變數類似:

      struct card aCard, deck[ 52 ], *cardPtr;

    • 也可以直接放在結構定義的右大括號之後:

      struct card {

      char *face;

      char *suit;

      } aCard, deck[ 52 ], *cardPtr;


Chapter 10 unions

10.2 結構定義

  • 合法的運算

    • 設定結構給型態相同的結構變數

    • 取得結構變數的位址 (&)

    • 存取結構變數的成員

    • 使用 sizeof運算子來計算結構變數的大小


Chapter 10 unions

aCard

“Three”

*face

*suit

“Hearts”

ThreeHearts

*face

“Three”

*suit

“Hearts”

10.3結構初始值設定

  • 初始值串列

    • Example:

      struct card aCard = { "Three", "Hearts" };

  • 設定

    • 定義並設定初始值 threeHearts :

      struct card threeHearts;

      threeHearts.face = “Three”;

      threeHearts.suit = “Hearts”;


Chapter 10 unions

aCard

“Three”

*face

*suit

“Hearts”

*cardPtr

10.4 存取結構成員

  • 存取結構成員

    • 結構成員運算子(點運算子)(.): 存取結構成員

      struct card aCard;

      printf( "%s", aCard.suit );

    • 結構指標運算子(箭號運算子)(->): 透過指向結構的指標來存取結構成員

      struct card *CardPtr = &aCard;

      printf( "%s", CardPtr->suit );

    • CardPtr->suit相同於

      ( *CardPtr ).suit


Chapter 10 unions

a

“Ace”

*face

*aPtr

*suit

“Spades”

struct card

*face

*suit


Chapter 10 unions

a

“Ace”

*face

*aPtr

*suit

“Spades”

Ace of Spades

Ace of Spades

Ace of Spades


Chapter 10 unions

10.5 使用結構與函數

  • 將結構傳給函數

    • 傳遞整個結構

    • 傳遞個別的結構成員

    • 均為 call-by-value (傳值呼叫)

  • 利用 call-by-reference (傳參照呼叫)傳遞結構

    • 傳遞位址(參照)

  • 利用 call-by-value傳遞陣列

    • 產生一個包含陣列成員的結構

    • 然後傳遞結構


10 6 typedef

10.6typedef

  • typedef

    • 為之前定義的資料型別定義別名

    • 使用 typedef建立較短的型別名稱

    • Example:

      typedef struct card Card;

    • 定義新的資料名稱 Card為 struct card 的別名

    • typedef並不建立一個新的資料型別


10 7 example

10.7 Example:高效率的洗牌與發牌

  • Pseudocode:

    • Create an array of card structures

    • Put cards in the deck

    • Shuffle the deck(洗牌)

    • Deal the cards(發牌)


Chapter 10 unions

struct card

*face

*suit


Chapter 10 unions

0

1

deck[52]

*face

*suit

*face

51

*suit

*face

*suit


Chapter 10 unions

0

0

1

1

deck[52]

deck[52]

*face

*face

Ace

Four

*suit

*suit

Hearts

Clubs

*face

*face

Deuce

Threee

51

51

*suit

*suit

Hearts

Heart

*face

*face

King

King

*suit

*suit

Spades

Diamonds

wDeck

const

wDeck


Chapter 10 unions

Four of Clubs Three of Hearts

Three of Diamonds Three of Spades

Four of Diamonds Ace of Diamonds

Nine of Hearts Ten of Clubs

Three of Clubs Four of Hearts

Eight of Clubs Nine of Diamonds

Deuce of Clubs Queen of Clubs

Seven of Clubs Jack of Spades

Ace of Clubs Five of Diamonds

Ace of Spades Five of Clubs

Seven of Diamonds Six of Spades

Eight of Spades Queen of Hearts

Five of Spades Deuce of Diamonds

Queen of Spades Six of Hearts

Queen of Diamonds Seven of Hearts

Jack of Diamonds Nine of Spades

Eight of Hearts Five of Hearts

King of Spades Six of Clubs

Eight of Diamonds Ten of Spades

Ace of Hearts King of Hearts

Four of Spades Jack of Hearts

Deuce of Hearts Jack of Clubs

Deuce of Spades Ten of Diamonds

Seven of Spades Nine of Clubs

King of Clubs Six of Diamonds

Ten of Hearts King of Diamonds


10 8 unions

union Number

value

X

(或y)

X

(或y)

10.8 Unions

  • union

    • 在不同時間下,含有多個資料型別

    • 但在同一時間,只有一個成員可進行參照(被指定值)

    • union的成員共用相同的儲存體空間

    • 可以節省儲存空間

  • 定義union

    • 類似結構

      union Number {

      int x;

      float y;

      };

      union Number value;


10 8 unions1

10.8 Unions

  • 合法的union運算

    • 設定給型態相同的union變數 : =

    • 取得union變數的位址 : &

    • 存取union變數的成員: .

    • 使用指標存取union變數的成員: ->


Chapter 10 unions

union Number

value

value

X

(或y)

X

(或y)

X

100


Chapter 10 unions

Put a value in the integer member

and print both members.

int: 100

double:

-92559592117433136000000000000000000000000000000000000000000000.000000

Put a value in the floating member

and print both members.

int: 0

double:

100.000000


Chapter 10 unions

10.9 位元運算子

  • 所有資料在電腦的內部均是以一連串的位元(bit)來加以表示

    • 每個位元的值可以是0或1

    • 位元運算子都會將整數運算元以二進位制加以處理


Chapter 10 unions

Enter an unsigned integer: 65000

65000 = 00000000 00000000 11111101 11101000


Chapter 10 unions

10.9 位元運算子


Chapter 10 unions

The result of combining the following

65535 = 00000000 00000000 11111111 11111111

1 = 00000000 00000000 00000000 00000001

using the bitwise AND operator & is

1 = 00000000 00000000 00000000 00000001

The result of combining the following

15 = 00000000 00000000 00000000 00001111

241 = 00000000 00000000 00000000 11110001

using the bitwise inclusive OR operator | is

255 = 00000000 00000000 00000000 11111111

The result of combining the following

139 = 00000000 00000000 00000000 10001011

199 = 00000000 00000000 00000000 11000111

using the bitwise exclusive OR operator ^ is

76 = 00000000 00000000 00000000 01001100

The one's complement of

21845 = 00000000 00000000 01010101 01010101

is

4294945450 = 11111111 11111111 10101010 10101010


Chapter 10 unions

10.9 位元運算子


Chapter 10 unions

10.9 位元運算子


Chapter 10 unions

The result of left shifting

960 = 00000000 00000000 00000011 11000000

8 bit positions using the left shift operator << is

245760 = 00000000 00000011 11000000 00000000

The result of right shifting

960 = 00000000 00000000 00000011 11000000

8 bit positions using the right shift operator >> is

3 = 00000000 00000000 00000000 00000011


Chapter 10 unions

10.9 位元運算子


Chapter 10 unions

10.9 位元運算子


10 10

10.10 位元欄位

  • Bit field

    • Member of a structure whose size (in bits) has been specified

    • Enable better memory utilization

    • Must be defined as int or unsigned

    • Cannot access individual bits

  • Defining bit fields

    • Follow unsigned or int member with a colon (:) and an integer constant representing the width of the field

    • Example:

      struct BitCard {

      unsigned face : 4;

      unsigned suit : 2;

      unsigned color : 1;

      };


10 101

10.10 位元欄位

  • Unnamed bit field

    • Field used as padding in the structure

    • Nothing may be stored in the bits

      struct Example {

      unsigned a : 13;

      unsigned : 3;

      unsigned b : 4;

      }

    • Unnamed bit field with zero width aligns next bit field to a new storage unit boundary


Chapter 10 unions

Card: 0 Suit: 0 Color: 0 Card: 0 Suit: 2 Color: 1

Card: 1 Suit: 0 Color: 0 Card: 1 Suit: 2 Color: 1

Card: 2 Suit: 0 Color: 0 Card: 2 Suit: 2 Color: 1

Card: 3 Suit: 0 Color: 0 Card: 3 Suit: 2 Color: 1

Card: 4 Suit: 0 Color: 0 Card: 4 Suit: 2 Color: 1

Card: 5 Suit: 0 Color: 0 Card: 5 Suit: 2 Color: 1

Card: 6 Suit: 0 Color: 0 Card: 6 Suit: 2 Color: 1

Card: 7 Suit: 0 Color: 0 Card: 7 Suit: 2 Color: 1

Card: 8 Suit: 0 Color: 0 Card: 8 Suit: 2 Color: 1

Card: 9 Suit: 0 Color: 0 Card: 9 Suit: 2 Color: 1

Card: 10 Suit: 0 Color: 0 Card: 10 Suit: 2 Color: 1

Card: 11 Suit: 0 Color: 0 Card: 11 Suit: 2 Color: 1

Card: 12 Suit: 0 Color: 0 Card: 12 Suit: 2 Color: 1

Card: 0 Suit: 1 Color: 0 Card: 0 Suit: 3 Color: 1

Card: 1 Suit: 1 Color: 0 Card: 1 Suit: 3 Color: 1

Card: 2 Suit: 1 Color: 0 Card: 2 Suit: 3 Color: 1

Card: 3 Suit: 1 Color: 0 Card: 3 Suit: 3 Color: 1

Card: 4 Suit: 1 Color: 0 Card: 4 Suit: 3 Color: 1

Card: 5 Suit: 1 Color: 0 Card: 5 Suit: 3 Color: 1

Card: 6 Suit: 1 Color: 0 Card: 6 Suit: 3 Color: 1

Card: 7 Suit: 1 Color: 0 Card: 7 Suit: 3 Color: 1

Card: 8 Suit: 1 Color: 0 Card: 8 Suit: 3 Color: 1

Card: 9 Suit: 1 Color: 0 Card: 9 Suit: 3 Color: 1

Card: 10 Suit: 1 Color: 0 Card: 10 Suit: 3 Color: 1

Card: 11 Suit: 1 Color: 0 Card: 11 Suit: 3 Color: 1

Card: 12 Suit: 1 Color: 0 Card: 12 Suit: 3 Color: 1


10 11

10.11 列舉型別常數

  • 列舉型別

    • 一組由識別字所代表的整數常數

    • 列舉型別常數事實上便是一些會自動設定數值的符號常數

      • 數值從0開始,然後逐漸遞增1

      • 數值也可使使用等號(=)來設定

      • 列舉型別內的常數識別字必須是唯一的

    • Example1:

      enum months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

      • 產生一個新的型別 enum months ,其內的識別字分別會設定為0到 11的整數


10 111

10.11 列舉型別常數

  • Example 2:

    enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

    • 產生一個新的型別 enum months ,其內的識別字分別會設定為1到 12的整數


Chapter 10 unions

1 January

2 February

3 March

4 April

5 May

6 June

7 July

8 August

9 September

10 October

11 November

12 December


  • Login