Presentation is loading. Please wait.

Presentation is loading. Please wait.

STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard : char name[10]; int age; char tel[10] char e-mail[60]

Similar presentations


Presentation on theme: "STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard : char name[10]; int age; char tel[10] char e-mail[60]"— Presentation transcript:

1 STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard : char name[10]; int age; char tel[10] char e-mail[60]

2 結構的定義 struct namecard { char name[20]; int age; char tel[10]; char e-mail[60]; };

3 結構的變數 struct namecard mycard; typedef struct namecard NAMECARD; NAMECARD mycard;

4 結構的變數初始值、輸入、輸出 NAMECARD A = {“mike”, 30}; printf("Enter you name:\n"); scanf("%s", A.name); printf("Enter you age:\n"); scanf("%d", &A.age); printf("You name is %s\n", A.name); printf("You are %d years old\n", A.age);

5 結構陣列 Mike 30 Rose 24 Lily 28 Jack 27 Tom 19 cardbook[] 0 1 2 3 4 cardbook[]={{“Mike”,30}, {“Rose”,24}, {“Lily”,28}, {“Jack”,27}, {“Tom”, 19}};

6 結構陣列的輸入、輸出 NAMECARD cardbook[5]; for(i=0 ; i<5 ; i++) { printf("Enter you name:\n"); scanf("%s", cardbook[i].name); printf("Enter you age:\n"); scanf("%d", &cardbook[i].age); } for(i=0 ; i<5 ; i++) printf("%s,%d\n", cardbook[i].name, cardbook[i].age);

7 結構指標 NAMECARD A={“Mike”, 30}, B={“Rose”,28}; NAMECARD *cptr; cptr = &A; printf(“nanme:%s, age:%d\n”, cptr->name, cptr->age}; cptr =&B; printf(“nanme:%s, age:%d\n”, cptr->name, cptr->age};

8 結構指標與結構陣列 NAMECARD cardbook[3]={{"MIKE", 29}, {"COCO", 30}, {"John", 25}}; NAMECARD *cptr; cptr=cardbook; for(i=0 ; i<3 ; i++) printf("%s,%d\n", cptr[i].name, cptr[i].age);

9 結構的動態配置 NAMECARD *cptr; clrscr(); printf("Enter the number of namecard:\n"); scanf("%d", &n); cptr=(NAMECARD*) malloc(sizeof(NAMECARD)*n);

10 Case Study : 平面座標點. A(2,3). B(5,12). B(16,8) struct point { int x; int y; };

11 Case Study : 兩點距離. A(2,3). B(5,12) float distance(POINT, POINT);

12 Case Study : 求重心. A(2,3). B(5,12). B(16,8).G.G

13 Homework : 複數 1. 請定義一名為 complex 的複數結構,此結構裡有兩個浮點數 r, v; 2. 試定義出下列四個函示 CP complex_add(CP, CP); CP complex_sub(CP, CP); CP complex_mul(CP, CP); CP complex_sub(CP, CP); 3. 寫一主程式輸入兩個複數,計算兩複數之和、差、乘積及商

14 Case Study : Poker Game struct card { int point; int type; int status; }; typedef struct card CARD; int type[4]={5,4,3,6}; /* , ,,  */ char* point[13]={"2", "3", "4", "5", "6", "7", "8", "9", "10", "J","Q", "K", "A"};

15 Case Study : Poker Game poker = (CARD*) malloc(sizeof(CARD)*52); for(i=0 ; i<52 ; i++) { poker[i].type = i/13; poker[i].point = i%13; } 製造一副撲克牌

16 Case Study : Poker Game CARD temp; srand(time(NULL)); for(i=0 ; i<6000 ; i++) { r = rand()%52; while((l=rand()%52)==r); temp = p[r]; p[r] = p[l]; p[l] = temp; } 洗牌

17 Case Study : Poker Game printf("Enter the number of your card:"); scanf("%d", &u); printf("%c%s\n", type[poker[u].type], point[poker[u].point]); printf("I choose :\n"); scanf("%d", &i); printf("%c%s\n", type[poker[i].type], point[poker[i].point]); if(poker[u].point < poker[i].point) printf("You lose !!\n"); else if( (poker[u].point == poker[i].point) && (poker[u].type<poker[i].type) ) printf("You lose !!\n"); else printf("You Win !!\n");

18 串列 串列的操作. Add node. Delete node. Show list 10203040

19 串列. 佇列 ( FIFO 、 LIFO ). 雙向鏈結 DOUBLE LINK 10203040 10203040

20 佇列 struct node { int data; struct node * next; }; typedef struct node NODE;

21 佇列 newnode = (NODE*) malloc(sizeof(NODE)); newnode->data = data; newnode->next = NULL; NODE *newnode;

22 佇列( LIFO ) insert(&list, newnode); NODE *list=NULL; list 10 list 2010 2010 1 2 newnode->next = *head; *head = newnode;

23 佇列( LIFO ) 10203040 list 10203040 list *list= (*list)->next;

24 佇列( LIFO ) 10203040 list ptr ptr = ptr->next; printf("(%x:%d)->", ptr, ptr->data);

25 佇列( FIFO ) head 10 tail head 10 tail head 10 tail 20 head 10 tail 20 tail 1 2

26 佇列( FIFO ) head 10 tail 203040 head 10203040 tail

27 雙向鏈結 10 struct node{ int data; struct node* right; struct node* left; }; typedef struct node NODE;

28 雙向鏈結 10 head 10 head 10 head 20 10 head 2 1 3

29 雙向鏈結 10203040 head 10203040 head


Download ppt "STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard : char name[10]; int age; char tel[10] char e-mail[60]"

Similar presentations


Ads by Google