3 - 1
資料型態
• Data Types
• Primitive Data Types
• Accumulated Data Types- Array- Structures- Union
• Abstract Data Types
• Examples
3 - 2
Data Types
int i = 100;
char c = ‘A’;
float f = 1.0;
00 ... 1100100
031
0100001
00 ... 110 ... 0
031
3 - 3
定義:定義: Data TypeData Type
A data type is a collection of objects and a set of operations that act on those objects.
3 - 4
Name of Type # of bits Range Type
char 8 -128 ~ 127 字元
unsigned char 8 0 ~ 255 字元
short 16 -215 ~ 215 - 1 短整數
unsigned short 16 0 ~ 216 - 1 正短整數
int 32 -231 ~ 231 - 1 整數
unsigned 32 0 ~ 231 - 1 正整數
long 32 -231 ~ 231 - 1 長整數
unsigned long 32 0 ~ 231 - 1 正長整數
float 32 -1037 ~ 1037 浮點數
double 64 -10308 ~ 10308 倍準浮點數
表一: C 的基本資料型態
註:表中第二和第三欄的數值是 machine dependent 。
3 - 5
Accumulated Data Type
• Array
• Struct
• Union
3 - 6
Array (陣列)
陣列是用來存放同樣型態的資料
陣列的大小必須在程式中預先設定
在程式執行中,陣列的大小無法改變
陣列中的資料是透過索引( index )來存取
const int ArraySize = 100;
int iArray[ArraySize];
3 - 7
一維陣列的宣告
typetype array_name[array_size];array_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 */
7 51 22 43 9
0 1 2 98 99
3 - 8
一維陣列的資料存取
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 內 */
array_varaible_name[index]
3 - 9
一維陣列和記憶體間的對應
int iArray[100];
7 51 22 43 9
0 1 2 98 99
m
m + 2
m + 4
m + 6
m + 198
假定 sizeof(int) = 2
Memory
3 - 10
多維陣列的宣告
typetype array_name[arraySizearray_name[arraySize11] ...... [arraySize] ...... [arraySizenn];];
【範例】
int mesh[7][11];
0123456
0 1 2 3 4 5 6 7 8 9 10
float cube[6][8][3];
0 1 2 3 4 5 6 7012345
01
2
3 - 11
多維陣列的資料存取int mesh[7][11];
0
1
2
3
4
5
6
0 1 2 3 4 5 6 7 8 9 10
mesh[4][2]
mesh[2][7]
3 - 12
多維陣列和記憶體間的對應
0
1
2
3
4
5
6
0 1 2 3 4 5 6 7 8 9 10
int mesh[7][11];
m
m + 22
m + 44
m + 66
m + 88
m + 110
m + 132
假定 sizeof(int) = 2
Memory
mesh[i][j] = mesh +11 i + j
3 - 13
float cube[6][8][3];
0 1 2 3 4 5 6 70
123
4
5
01
2
m
m + 48
m + 96
m + 144
m + 192
m + 240
m + 288
假定 sizeof(int) = 2cube[i][j][k] = cube + 24i + 8j+ k
3 - 14
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.
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 6565
3 - 15
Coxeter Rule (valid for an odd n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.
3 - 16
Magic Square 的表示法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
1
2
3
4
0 1 2 3 4
const int MAX_SIZE = 15;
int square[MAX_SIZE][MAX_SIZE];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
4
3
2
1
0
0 1 2 3 4
II IIII
3 - 17
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;}
0
1
2
3
4
0 1 2 3 4
O( )
( )
( )
n
n
n
2
2
2
3 - 18
Structure (結構)
一個 structure 由若干資料欄( field )所組成。資料欄的
型態可以不同。藉由 struct 的宣告,我們可以將數種不
同型態的資料聚集在一起,以描述比較複雜的資料形態。
struct { field1 declaration; field2 declaration;
} variable_name;
3 - 19
struct 的宣告
struct { field1 declaration; field2 declaration;
} variable_name;
typedef struct { field1 declaration; field2 declaration;
} type_name;
【範例】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;
宣告變數 宣告型態
3 - 20
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;
3 - 21
struct 的資料存取 struct_variable_name.field_name
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';
3 - 22
struct 和記憶體間的對應
struct studentType { char Name[20]; // 姓名 char Address[30]; // 地址 char PhoneNumber[10]; // 電話 int Age; // 年齡 char Department[4]; // 系別 int Year; // 年級 char Class; // 班級};
studentType Student1, Student2;
m
m + 20
m + 50
m + 66
m + 60
m + 62
m + 68
假定 sizeof(int) = 2
3 - 23
Union
一個 union 由若干資料欄( field )所組成。資料欄的
型態可以不同。與 struct 不同的是:這些資料欄共用
記憶體的空間。
union { field1 declaration; field2 declaration;
} variable_name;
3 - 24
union 的宣告
union { field1 declaration; field2 declaration;
} variable_name;
typedef union { field1 declaration; field2 declaration;
} type_name;
【範例】union { char charValue; int intValue; float floatValue;} dataCell;
typedef union { char charValue; int intValue; float floatValue;} dataCellType;
宣告變數 宣告型態
3 - 25
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 + 257
m+1
m+5
3 - 26
union 的資料存取 union_variable_name.field_name
dataCell.charValue = ‘A’;
m
m + 4
A
dataCell.intValue = 100;
m
m + 4
100
dataCell.floatValue = 3.14;
m
m + 4
3.14
3 - 27
Abstract Data Type
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.
3 - 28
【範例】 Rectangle ADT
OBJECTS:Rectangle (矩型)
FUNCTIONS:
Rectangle Create();
Width(Rectangle);
Length(Rectangle);
Area(Rectangle);
Circumference(Rectangle);
3 - 29
Implementation of Rectangle ADT (I):
xmin xmax
ymin
ymax
x
y
typedef struct { int xmin, xmax; int ymin, ymax;} Rectangle;
3 - 30
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));}
3 - 31
Implementation of Rectangle ADT (II):
typedef struct { int x, y; } Point;
typedef struct { Point bottomLeft, upperRight;} Rectangle;
x
y
3 - 32
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
3 - 33
Implementation of Rectangle ADT (III):
typedef struct { int x, y; } Point;
typedef struct { Point bottomLeft; int width, height;} Rectangle;
x
y
w
h
3 - 34
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);}
3 - 35
Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world.
QT
T
implemen-tation 1
implemen-tation 2
3 - 36
Implemen-tation ofT
programsuse T
request to perform operations
result of operation
3 - 37
List (資料列)
List 是什麼? List ADT
List Implementation Based on Array
3 - 38
List (資料列)
0 1 2 n-2 n-1
data items
資料列可視為一種資料容器( container )。其中的資料具有某種的次序性。
3 - 39
範例
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 )
3 - 40
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 Operations
3 - 41
List ADT
OBJECTS:An ordered list that contains a sequence of data items.
FUNCTIONS:
void CreateList (list L)
boolean IsEmpty(list L)
int ListLength (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)
3 - 42
CreateList(L)
ListInsert(L, 0, milk)
milk
ListInsert(L, 1, eggs)
milk, eggs
ListInsert(L, 2, butter)
milk, eggs, butter
ListInsert(L, 3, apples)
L is a list object.
milk, eggs, butter, apples
3 - 43
milk, eggs, butter, apples
ListInsert(L, 4, bread)
ListInsert(L, 5, chicken)
milk, eggs, butter, apples, bread
milk, eggs, butter, apples, bread, chicken
ListInsert(L, 3, nuts)
milk, eggs, butter, nuts, apples, bread, chicken
ListDelete(L, 4)
milk, eggs, butter, nuts, bread, chicken
3 - 44
List ADT Implementation Based on Array
Size
Items
0 1 2 n-1 MAX_SIZE - 1
n
45 21 33 16 ? ?
Items[i] stores the ith item of the list.
#define MAX_ITEM 100typedef int itemType;typedef struct { int Size; itemType Items[MAX_ITEM];} list;
3 - 45
/********************************************************** * 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
3 - 46
/*************************************************************** * FILE: listA.c * implementation of lists based on array. ***************************************************************/#include “listA.h”void CreateList (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;}int ListLength (list *L) /* get the length of a list */{ return L->Size;}
3 - 47
/* 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;}
Size
Items
0 1 2 n-1 MAX_SIZE - 1
n
45 21 33 16 ? ?
Items[i] stores the ith item of the list.
3 - 48
/* 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;}
Size
Items
0 1 2 n-1 MAX_SIZE - 1
n
45 21 33 16 ? ?
Items[i] stores the ith item of the list.
3 - 49
k
0 1 2 k-1 MAX_LIST-1
12 3 19 ?1810 ?
k+1
0 1 2 k MAX_LIST-1
12 3 18105 ?
k+1
0 1 2 k MAX_LIST-1
12 3 44 18105 ?
ListInsert (&L, 2, 44)
3 - 50
/* 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;}
Size
Items
0 1 2 n-1 MAX_SIZE - 1
n
45 21 33 16 ? ?
Items[i] stores the ith item of the list.
3 - 51
k
0 1 2 k-1 MAX_LIST-1
12 3 19 ?1810 ?
Delete 19
k
0 1 2 k-1 MAX_LIST-1
12 3 ?1810 ?
k-1
0 1 2 k-1 MAX_LIST-1
12 3 ??18 ?
k-2
34
ListDelete(&L, 2)
3 - 52
/* Delete the i-th data item from the list L. */
boolean ListDelete (list *L, int i){ if (i < 0 || i >= L->Size) return FALSE; /* illegal position to remove */ /* fill the space where the deleted item left */ for (k = i; k < L->Size-1; k++) L->Items[k] = Items[k+1]; L->Size--; return TRUE;}
Size
Items
0 1 2 n-1 MAX_SIZE - 1
n
45 21 33 16 ? ?
Items[i] stores the ith item of the list.