Ch 111 12 Ch 111 1 includestdio h

  • Slides: 25
Download presentation

範例 Ch 11_1 (1/2) Ch 11_1 分離型宣告 1 #include<stdio. h> 2 void main( )

範例 Ch 11_1 (1/2) Ch 11_1 分離型宣告 1 #include<stdio. h> 2 void main( ) 3 { 4 struct family /*此結構型態名稱為family */ 5 { 6 char name[20]; 7 int age; 8 }; 9 struct family people; /*結構型態family的變數名稱為people */ 10 strcpy(people. name, “John Wang”); /*指定結構的初始值 */ 11 people. age = 18; 12 printf(“Name = %sn”, people. name); /* people. name,指出people的成員 */ 13 printf(“Age = %d”, people. age); /* people. age 以『. 』指出people的成員 */ 14 } 5

Ch 11_1 輸出結果 (2/2) • 程式執行結果 Name = John Wang Age = 18 6

Ch 11_1 輸出結果 (2/2) • 程式執行結果 Name = John Wang Age = 18 6

範例 Ch 11_2 (1/2) Ch 11_2 結合型宣告 1 #include<stdio. h> 2 void main( )

範例 Ch 11_2 (1/2) Ch 11_2 結合型宣告 1 #include<stdio. h> 2 void main( ) 3 { 4 struct family /*此結構型態名稱為family */ 5 { 6 char name[20]; 7 int age; 8 }people; /*結構型態family的變數名稱為people */ 9 strcpy(people. name, “John Wang”); /*指定結構的初始值 */ 10 people. age = 18; 11 printf(“Name = %sn”, people. name); /* people. name,指出people的成員 */ 12 printf(“Age = %d”, people. age); /* people. age 以『. 』指出people的成員 */ 13 } 8

Ch 11_2 輸出結果 (2/2) • 程式執行結果 Name = John Wang Age = 18 9

Ch 11_2 輸出結果 (2/2) • 程式執行結果 Name = John Wang Age = 18 9

11 -3 結構陣列(2/2) struct family { char name[10]; int age; }; struct family people[2]

11 -3 結構陣列(2/2) struct family { char name[10]; int age; }; struct family people[2] = {{“John”, 18}, {“Mary”, 20}} • 則其結構陣列可表示如下: char int people[0] name[10] age people[1] name[10] age 佔 10 bytes 佔 2 bytes 11

範例 Ch 11_3 (1/3) Ch 11_3 結構陣列 1 #include<stdio. h> 2 void main( )

範例 Ch 11_3 (1/3) Ch 11_3 結構陣列 1 #include<stdio. h> 2 void main( ) 3 { 4 struct company 5 { 6 char name[10]; /* 宣告結構陣列char name[10],然後 7 int salary; 再宣告salary,prize,total為整數型態*/ 8 int prize; 9 int total; 10 }; 12

範例 Ch 11_3 (2/3) 11 struct company person[ ] = {{"Mary", 20000, 1500}, 12

範例 Ch 11_3 (2/3) 11 struct company person[ ] = {{"Mary", 20000, 1500}, 12 {"Jack", 30000, 2000}, 13 {"John", 15000, 5000}, 14 {"Tony", 25000, 2500}}; 15 int i; 16 for(i = 0; i < 4; i++) 17 { 18 person[i]. total = person[i]. salary + person[i]. prize; 19 printf("%s", person[i]. name); 20 printf("%8 d", person[i]. salary); 21 printf("%8 d", person[i]. prize); 22 printf("%8 dn", person[i]. total); 23 } 24 } 13

Ch 11_3輸出結果 (3/3) • 程式執行結果 Mary 20000 1500 21500 Jack 30000 2000 32000 John

Ch 11_3輸出結果 (3/3) • 程式執行結果 Mary 20000 1500 21500 Jack 30000 2000 32000 John 15000 20000 Tony 25000 2500 27500 14

11 -4 巢狀結構 • 結構中可以包含結構,而形成一個巢狀結構。如下所 示: struct person { char name[20]; int age; }

11 -4 巢狀結構 • 結構中可以包含結構,而形成一個巢狀結構。如下所 示: struct person { char name[20]; int age; } struct family /*結構family中包含結構person*/ { struct person brother; 則 group. brother. name = Tony struct person sister; group. brother. age = 22 } group. sister. name = Mary struct family group = {{“Tony”, 22}, group. sister. age = 20 {“Mary”, 20}}; 15

範例 Ch 11_4 (1/2) Ch 11_4 巢狀結構 1 #include<stdio. h> 2 main() 3 {

範例 Ch 11_4 (1/2) Ch 11_4 巢狀結構 1 #include<stdio. h> 2 main() 3 { 4 struct company 5 { 6 int salary; 7 int prize; 8 }; 9 struct class 10 { 11 struct company person; 12 int total; 13 }p; 16

範例 Ch 11_4 (2/2) 14 p. person. salary = 28000; 15 p. person. prize

範例 Ch 11_4 (2/2) 14 p. person. salary = 28000; 15 p. person. prize = 7000; 16 p. total = p. person. salary + p. person. prize; 17 printf("total = %dn", p. total); 18 } • 程式執行結果 total = 35000 17

範例 Ch 11_5 (1/2) Ch 11_5 結構與函數之傳值法 1 #include<stdio. h> 2 struct add {

範例 Ch 11_5 (1/2) Ch 11_5 結構與函數之傳值法 1 #include<stdio. h> 2 struct add { 3 int a, b; 4 }number; 5 main() 6 { 7 int total; 8 static struct add number = {50, 70}; 9 total = sum(number. a, number. b); 10 printf("The result : a + b = %dn", total); 11 } 12 sum(int m, int n) 13 { 14 return(m + n); 15 } 19

範例 Ch 11_6 (1/2) Ch 11_6 結構與函數之傳址法 1 #include<stdio. h> 2 struct add {

範例 Ch 11_6 (1/2) Ch 11_6 結構與函數之傳址法 1 #include<stdio. h> 2 struct add { 3 int a, b; 4 }number; 5 main() 6 { 7 int total; 8 static struct add number = {50, 70}; 9 total = sum(&number); 10 printf("The result : a + b = %dn", total); 11 } 12 sum(ab) 13 struct add *ab; 14 { 15 return(ab -> a + ab -> b); 16 } 21

範例 Ch 11_8 (1/2) Ch 11_8 聯合型態 1 #include<stdio. h> 2 void main() 3

範例 Ch 11_8 (1/2) Ch 11_8 聯合型態 1 #include<stdio. h> 2 void main() 3 { 4 struct class 5 { 6 int i; 7 char c; 8 }; 9 union score 10 { 11 float b; 12 struct class student; 13 }; 24

範例 Ch 11_8 (2/2) 14 union score a; 15 printf(“size = %dn”, sizeof(union score));

範例 Ch 11_8 (2/2) 14 union score a; 15 printf(“size = %dn”, sizeof(union score)); 16 printf(“a. student. i = %dn”, a. student. i = 75); 17 printf(“a. student. c = %cn”, a. student. c = ‘K’); 18 printf(“a. b = %. 1 fn”, a. b = 60. 0); 19 } • 程式執行結果 size = 4 a. student. i = 75 a. student. c = K a. b = 60. 0 25