520 likes | 530 Views
資料型態. Data Types Primitive Data Types Accumulated Data Types - Array - Structures - Union Abstract Data Types Examples. 00 ... 1100100. int i = 100;. 31. 0. char c = ‘A’;. 0100001. float f = 1.0;. 00 ... 110 ... 0. 31. 0. Data Types.
E N D
資料型態 • Data Types • Primitive Data Types • Accumulated Data Types - Array - Structures - Union • Abstract Data Types • Examples
00 ... 1100100 int i = 100; 31 0 char c = ‘A’; 0100001 float f = 1.0; 00 ... 110 ... 0 31 0 Data Types
A data type is a collection of objects and a set of operations that act on those objects. 定義:Data Type
表一:C 的基本資料型態 註:表中第二和第三欄的數值是 machine dependent。
Accumulated Data Type • Array • Struct • Union
Array(陣列) • 陣列是用來存放同樣型態的資料 • 陣列的大小必須在程式中預先設定 • 在程式執行中,陣列的大小無法改變 • 陣列中的資料是透過索引(index)來存取 const int ArraySize = 100; int iArray[ArraySize];
7 51 22 43 9 0 1 2 98 99 一維陣列的宣告 typearray_name[array_size]; 【範例】 int iArray[100]; /* an integer array with 100 elements */ char cArray[256]; /* a character array with 256 elements */ float fArray[10]; /* a floating-number array with 10 elements */
array_varaible_name[index] int iArray[100]; 7 51 22 43 9 0 1 2 98 99 存 取 一維陣列的資料存取 iArray[k] = 50; /* 將 50 存入索引 k 所指的位置 */ int temp; temp = iArray[k]; /* 將 k 所指位置中的資料值 存入變數 temp 內 */
7 51 22 43 9 0 1 2 98 99 Memory 一維陣列和記憶體間的對應 m m + 2 m + 4 m + 6 int iArray[100]; m + 198 假定 sizeof(int) = 2
多維陣列的宣告 typearray_name[arraySize1] ...... [arraySizen]; 【範例】 int mesh[7][11]; float cube[6][8][3]; 6 5 4 5 4 3 3 2 2 1 1 2 0 1 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 10
多維陣列的資料存取 int mesh[7][11]; mesh[4][2] 6 5 4 3 mesh[2][7] 2 1 0 0 1 2 3 4 5 6 7 8 9 10
多維陣列和記憶體間的對應 Memory m m + 22 6 m + 44 5 4 m + 66 3 m + 88 2 m + 110 1 0 m + 132 0 1 2 3 4 5 6 7 8 9 10 int mesh[7][11]; 假定 sizeof(int) = 2
5 4 3 2 1 2 0 1 0 0 1 2 3 4 5 6 7 float cube[6][8][3]; m m + 48 m + 96 m + 144 m + 192 m + 240 m + 288 假定 sizeof(int) = 2
65 15 8 1 24 17 16 14 7 5 23 22 20 13 6 4 3 21 19 12 10 9 2 25 18 11 65 65 65 Magic Square Problem A magic square is an n by n matrix of the integer from 1 to n2 such that the sum of each row and column and the two major diagonals is the same.
Coxeter Rule (valid for an odd n) Put a one in the middle box of the top row. Go up and left assigning numbers in increasing order to empty box. If your move causes you to jump off the square, figure out where you would be if you landed on a box on the opposite side of the square. Continue with this box. If a box is occupied, go down instead of up and continue. 15 8 1 24 17 16 14 7 5 23 22 20 13 6 4 3 21 19 12 10 9 2 25 18 11
0 1 2 3 4 15 8 1 24 17 15 8 1 24 17 0 4 16 14 7 5 23 16 14 7 5 23 1 3 22 20 13 6 4 22 20 13 6 4 2 2 3 21 19 12 10 3 21 19 12 10 3 1 9 2 25 18 11 9 2 25 18 11 4 0 0 1 2 3 4 Magic Square 的表示法 const int MAX_SIZE = 15; int square[MAX_SIZE][MAX_SIZE]; I II
0 1 2 3 4 0 1 2 3 4 const int EMPTY = 0; for (row = 0; row < size; row ++) for (col = 0; col < size; col ++) square[row ][col] = EMPTY ; row = 1; col = (size - 1) / 2 + 1; for (count = 1; count <= size * size; count++) { i = (row - 1) < 0 ? size - 1 : row - 1; /* up */ j = (col - 1) < 0 ? size - 1 : col - 1; /* left */ if (square[i][j] != EMPTY ) /* occupied */ row = (row + 1) % size; /* down */ else { row = i; col = j; } square[row][col] = count; }
struct { field1 declaration; field2 declaration; } variable_name; Structure(結構) 一個 structure 由若干資料欄(field)所組成。資料欄的 型態可以不同。藉由 struct 的宣告,我們可以將數種不 同型態的資料聚集在一起,以描述比較複雜的資料形態。
struct { field1 declaration; field2 declaration; } variable_name; typedef struct { field1 declaration; field2 declaration; } type_name; struct 的宣告 宣告變數 宣告型態 【範例】 struct { char name[20]; int ID; char address[40]; char phone[10]; } student; typedef struct { char name[20]; int ID; char address[40]; char phone[10]; } studentType;
typedef struct { int x; // X 座標 int y; // Y 座標 } point2DType; point2DType P, Q; const int MAX_STRING = 50; typedef struct { char Title[MAX_STRING ]; // 名稱 char ID[10]; // 編號 int Have; // 數量 int Want; // 需求量 } videoType; videoType Video1, Video2; typedef struct { char Name[20]; // 姓名 char Address[30]; // 地址 char PhoneNumber[10]; // 電話 int Age; // 年齡 char Department[4]; // 系別 int Year; // 年級 char Class; // 班級 } studentType; studentType Student1, Student2;
struct_variable_name.field_name struct 的資料存取 point2DType P, Q; P.x = 100; P.y = 200; Q.x = P.x + 1; Q.y = Q.x * 2; #include <string.h> videoType Video1; strcpy(Video1.Title, "Gone with Wind"); Video1.Have = 3; Video1.Want = 0; studentType Student1; strcpy(Student1.Name, "Michael Jordon"); Student1.age = 34; Student1.class = 'B';
struct 和記憶體間的對應 m struct studentType { char Name[20]; // 姓名 char Address[30]; // 地址 char PhoneNumber[10]; // 電話 int Age; // 年齡 char Department[4]; // 系別 int Year; // 年級 char Class; // 班級 }; studentType Student1, Student2; m + 20 m + 50 m + 60 m + 62 m + 66 m + 68 假定 sizeof(int) = 2
union { field1 declaration; field2 declaration; } variable_name; Union 一個 union 由若干資料欄(field)所組成。資料欄的 型態可以不同。與 struct 不同的是:這些資料欄共用 記憶體的空間。
union { field1 declaration; field2 declaration; } variable_name; typedef union { field1 declaration; field2 declaration; } type_name; union 的宣告 宣告變數 宣告型態 【範例】 union { char charValue; int intValue; float floatValue; } dataCell; typedef union { char charValue; int intValue; float floatValue; } dataCellType;
union 和記憶體間的對應 union { char charValue; /* 1 byte */ int intValue; /* 2 byte */ float floatValue; /* 4 byte */ } dataCell; m m + 4 typedef struct { char opcode; union { int intValue; char strValue[256]; } data; } instruction; m m+1 m+5 m + 257
union_variable_name.field_name m A dataCell.charValue = ‘A’; m + 4 m 100 dataCell.intValue = 100; m + 4 m 3.14 dataCell.floatValue = 3.14; m + 4 union 的資料存取
An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations. Abstract Data Type
【範例】Rectangle ADT OBJECTS: Rectangle(矩型) FUNCTIONS: Rectangle Create(); Width(Rectangle); Length(Rectangle); Area(Rectangle); Circumference(Rectangle);
y ymax ymin x xmin xmax Implementation of Rectangle ADT (I): typedef struct { int xmin, xmax; int ymin, ymax; } Rectangle;
Rectangle Create (int left, int right, int bottom, int upper) { Rectangle r; r.xmin = left; r.xmax = right; r.ymin = bottom; r.ymax = upper; return r; } int Width (Rectangle r) { return r.xmax - r.xmin; } int Height (Rectangle r) { return r.ymax - r.ymin; } int Area (Rectangle r) { return Width(r) * Height(r); } int Circumference (Rectangle r) { return 2 * (Width(r) + Height(r)); }
Implementation of Rectangle ADT (II): y x typedef struct { int x, y; } Point; typedef struct { Point bottomLeft, upperRight; } Rectangle;
Rectangle Create (point bl, point ur) { Rectangle r; r.bottomLeft = bl; r.upperRight = ur; return r; } int Width (Rectangle r) { return r.upperRight.x - r.bottomLeft.x; } int Height (Rectangle r) { return r.upperRight.y - r.bottomLeft.y; } int Area (Rectangle r) { return Width(r) * Height(r); } int Circumference (Rectangle r) { return 2 * (Width(r) + Height(r)); }a
Implementation of Rectangle ADT (III): y h w x typedef struct { int x, y; } Point; typedef struct { Point bottomLeft; int width, height; } Rectangle;
Rectangle Create (int left, int right, int bottom, int upper) { Rectangle r; r.bottomLeft.x = left; r. bottomLeft.y = bottom; r.width = right - left; r.height = upper - bottom; return r; } int Width (Rectangle r) { return r. width; } int Height (Rectangle r) { return r.height; } int Area (Rectangle r) { return r.width * r.height; } int Circumference (Rectangle r) { return 2 * (r.width + r.height); }
Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world. Q T implemen- tation 1 T implemen- tation 2
request to perform operations Implemen- tation of T programs use T result of operation
List(資料列) • List 是什麼? • List ADT • List Implementation Based on Array
資料列可視為一種資料容器(container)。 其中的資料具有某種的次序性。 List(資料列) data items 0 1 2 n-2 n-1
範例 Days of week: ( Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ) Values in a deck of cards: ( Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King )
List Operations • Finding the length, n, of a list. • Reading the items in a list from left to right. • Retrieving the ith item from a list, 0 <= i < n. • Replacing the item in the ith position of a list, 0 <= i < n. • Inserting a new item in the ith position of a lsit, 0 <= i < n. • The items previously numbered i, i+1, …, n-1 become • items numbered i+1, i+2,…, n. • Deleting an item from the ith position of a lsit, 0 <= i < n. • The items previously numbered i+1, i+2, …, n become • items numbered i, i+1,…, n-1.
List ADT OBJECTS: An ordered list that contains a sequence of data items. FUNCTIONS: voidCreateList (list L) boolean IsEmpty(list L) intListLength (list L) boolean ListRetrieve (list L, int i, itemType Data) boolean ListReplace (list L, int i, itemType Data) boolean ListInsert (list L, int i, ItemType Data) boolean ListDelete (list L, int i)
ListInsert(L, 0, milk) ListInsert(L, 1, eggs) ListInsert(L, 2, butter) ListInsert(L, 3, apples) L is a list object. CreateList(L) milk milk, eggs milk, eggs, butter milk, eggs, butter, apples
ListInsert(L, 4, bread) ListInsert(L, 5, chicken) ListInsert(L, 3, nuts) ListDelete(L, 4) milk, eggs, butter, apples milk, eggs, butter, apples, bread milk, eggs, butter, apples, bread, chicken milk, eggs, butter, nuts, apples, bread, chicken milk, eggs, butter, nuts, bread, chicken
n Size Items 45 21 33 16 ? ? 0 1 2 n-1 MAX_SIZE - 1 Items[i] stores the ith item of the list. #define MAX_ITEM 100 typedef int itemType; typedef struct { int Size; itemType Items[MAX_ITEM]; } list; List ADT Implementation Based on Array
/********************************************************** * FILE: listA.h -- header file for lists based on array. **********************************************************/ #ifndef LISTA_H_ #define LISTA_H_ #define MAX_ITEM 100 typedef enum {FALSE, TRUE} boolean; typedef int itemType; typedef struct { int Size; itemType Items[MAX_ITEM]; } list; /* function prototypes */ #endif
/***************************************************************/*************************************************************** * FILE: listA.c * implementation of lists based on array. ***************************************************************/ #include “listA.h” voidCreateList (list *L) /* create an empty list */ { L->Size = 0; } boolean IsEmpty(list *L) /* check if a list is empty */ { return (L->Size == 0) ? TRUE : FALSE; } intListLength (list *L) /* get the length of a list */ { return L->Size; }
n Size Items 45 21 33 16 ? ? 0 1 2 n-1 MAX_SIZE - 1 Items[i] stores the ith item of the list. /* Retrieve the i-th item from the list L. */ boolean ListRetrieve (list *L, int i, itemType *Data) { if (i < 0 || i >= L->Size) return FALSE; *Data = L->Items[i]; return TRUE; }
n Size Items 45 21 33 16 ? ? 0 1 2 n-1 MAX_SIZE - 1 Items[i] stores the ith item of the list. /* Replace the i-th item in the list L. */ boolean ListReplace (list *L, int i, itemType Data) { if (i < 0 || i >= L->Size) return FALSE; L->Items[i] = Data; return TRUE; }
ListInsert (&L, 2, 44) 0 1 2 k-1 MAX_LIST-1 k 12 3 19 10 18 ? ? 0 1 2 k MAX_LIST-1 k+1 12 3 5 10 18 ? 0 1 2 k MAX_LIST-1 k+1 12 3 44 5 10 18 ?
n Size Items 45 21 33 16 ? ? 0 1 2 n-1 MAX_SIZE - 1 Items[i] stores the ith item of the list. /* Insert a data item into the i-th place of the list L. */ boolean ListInsert (list *L, int i, itemType Data) { if (L->Size >= MAX_SIZE) return FALSE; /* the array is full */ if (i < 0 || i > L->Size) return FALSE; /* illegal position to insert */ for (k = L->Size; k > i; k--) /* make a space for storing the new data */ L->Items[k] = Items[k-1]; L ->Items[i] = Data; L->Size++; return TRUE; }