Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 複合資料型態.

Similar presentations


Presentation on theme: "Chapter 5 複合資料型態."— Presentation transcript:

1 Chapter 5 複合資料型態

2 簡介 陣列 字串 結構

3 陣列 這是最經典的資料結構 相同資料型別, 固定長度的序列 使用[]運算子對陣列做隨機存取 存取區間介於 0 ~ size-1
對於任何資料型別都適用 使用[]運算子對陣列做隨機存取 存取區間介於 0 ~ size-1

4 陣列內容倒轉輸出 #include <stdio.h> #include <stdlib.h>
#define SIZE 10 int main(int argc, char *argv[]) { int i, n, input; int ns[SIZE]; for (n = 0; n < SIZE; n++) { scanf("%d", &input); if (input == 0) break; ns[n] = input; } for (i = n - 1; i >= 0; i--) printf("%d ", ns[i]); system("pause"); return 0;

5 陣列的初始化 可以使用列表初始化: int a[] = {1O,20,30,40,50}; 可以選擇給定維度 如果給定,必須要大於列表
通常用於零初始化 如果不給定,維度自動依列表初始化決定

6 多維度陣列 C並不存在多維度陣列 C,C++,JAVA 都允許陣列中有陣列 必須使用一個陣列來容納其他陣列 可以使用巢狀列表初始化

7 Example 2維陣列 3 x 2 : C 儲存線性: 編譯器解釋為3個2元素陣列

8 Example #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int a[][2] = {{1, 2}, {3, 4}, {5, 6}}; int i, j; for (i = 0; i < 3; ++i) { for (j = 0; j < 2; ++j) printf("%d ", a[i][j]); printf(“\n”); } system("pause"); return 0; /* */

9 3維陣列 例子將示範如何建立以下3維列陣: == a[0] 9 0 == a[1]

10 Example #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int a[][3][2] = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 0}, {1, 2}}}; int i, j, k; for (i = 0; i < 2; ++i){ for (j = 0; j < 3; ++j){ for (k = 0; k < 2; ++k) printf("%d ", a[i][j][k]); printf("\n"); } system("pause"); return 0;

11 字串 字元陣列 字串結束將跟隨 null byte ('\0') C++ 和 Java 具有較佳的字串處理能力
字串將自動增加'\0': “hello” 變為: 'h' 'e' 'l' 'l' 'o' '\0'

12 字串範例 #include <stdio.h> #include <string.h>
#include <stdlib.h> int main(int argc, char *argv[]) { char s1[] = {'c', 'h', 'r', 'i', 's', '\0'}; char s2[] = "rebots"; printf("s1 == %s\n", s1); printf("s2 == %s\n", s2); printf("s1 has %d chars\n", strlen(s1)); printf("s2 has %d chars\n", strlen(s2)); system("pause"); return 0; }/* s1 == chris s2 == rebots s1 has 5 chars s2 has 6 chars */

13 <string.h> 本函式庫包含常用的字串指令 多數假設字串尾部跟隨 NULL strcpy, strcat, memcpy
strcmp, memcmp strchr, memchr, strrchr, strstr, strtok

14 例子 #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int n = 1; float x = 2.0; char s[] = "chris"; char string[BUFSIZ]; // BUFSIZ=512 sprintf(string, "%d %f j%s", n + 1, x * 2, s + 1); printf("%s\n", string); sscanf(string, "%d %f %s", &n, &x, s); printf("n == %d, x == %f, s == %s\n", n, x, s); system("pause"); return 0; }/* jhris n == 2, x == , s == jhris */

15 結構 用來記錄資料 使用 struct 當作關鍵字 收錄任何可定義的資料 存取成員需要利用 . 運算子 是把物件和資料抽象化的方法
另稱 成員 存取成員需要利用 . 運算子 是把物件和資料抽象化的方法 資料成員是物件的抽象概念

16 Structure Example #include <stdio.h> #include <string.h>
#include <stdlib.h> struct maker { char last[16], first[11]; int score; }; int main(int argc, char *argv[]) { struct maker h1 = {"Bill", "Gates", 80 }; struct maker h2; strcpy(h2.last, "Steve"); strcpy(h2.first, "Jobs"); h2.score = h1.score - 5; printf("#1 == { %s, %s, %d }\n", h1.last, h1.first, h1.score); printf("#2 == { %s, %s, %d }\n", h2.last, h2.first, h2.score); system("pause"); return 0; }/* #1 == { Bill, Gates, 80 } #2 == { Steve, Jobs, 75 } */

17 Another Structure Example
" MoneyMaker" struct struct 成員可以是任何型別 將示範結構內有結構

18 Example #include <stdio.h> #include <stdlib.h>
#include <string.h> struct maker { char last[16], first[11]; int money, year; }; struct MoneyMaker { struct maker Persons[10]; int nPerson; int main(int argc, char *argv[]) { struct MoneyMaker mm; int i; mm.nPerson = 0;

19 Example continued strcpy(mm.Persons[mm.nPerson].last, "Bill");
strcpy(mm.Persons[mm.nPerson].first, "Gates"); mm.Persons[mm.nPerson].money = 56; mm.Persons[mm.nPerson++].year = 1955; strcpy(mm.Persons[mm.nPerson].last, "Steve"); strcpy(mm.Persons[mm.nPerson].first, "Jobs"); mm.Persons[mm.nPerson].money = 6; for (i = 0; i < mm.nPerson; ++i) printf("%d: {%s, %s, %d}\n", mm.Persons[i].year, mm.Persons[i].last, mm.Persons[i].first, mm.Persons[i].money); system("pause"); return 0; }

20 Output 1955: {Bill, Gates, 56} 1955: {Steve, Jobs, 6}

21 總結 複合資料型態 陣列索引起始 0 多維陣列是單維陣列的陣列 字串是字元的陣列 由一個空字元來結束字串陣列 結構是成員的集合
陣列和成員都支援列表初始化


Download ppt "Chapter 5 複合資料型態."

Similar presentations


Ads by Google