STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel namecard : char name[10]; int age; char tel[10] char [60]
結構的定義 struct namecard { char name[20]; int age; char tel[10]; char [60]; };
結構的變數 struct namecard mycard; typedef struct namecard NAMECARD; NAMECARD mycard;
結構的變數初始值、輸入、輸出 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);
結構陣列 Mike 30 Rose 24 Lily 28 Jack 27 Tom 19 cardbook[] cardbook[]={{“Mike”,30}, {“Rose”,24}, {“Lily”,28}, {“Jack”,27}, {“Tom”, 19}};
結構陣列的輸入、輸出 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);
結構指標 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};
結構指標與結構陣列 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);
結構的動態配置 NAMECARD *cptr; clrscr(); printf("Enter the number of namecard:\n"); scanf("%d", &n); cptr=(NAMECARD*) malloc(sizeof(NAMECARD)*n);
Case Study : 平面座標點. A(2,3). B(5,12). B(16,8) struct point { int x; int y; };
Case Study : 兩點距離. A(2,3). B(5,12) float distance(POINT, POINT);
Case Study : 求重心. A(2,3). B(5,12). B(16,8).G.G
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. 寫一主程式輸入兩個複數,計算兩複數之和、差、乘積及商
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"};
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; } 製造一副撲克牌
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; } 洗牌
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");
串列 串列的操作. Add node. Delete node. Show list
串列. 佇列 ( FIFO 、 LIFO ). 雙向鏈結 DOUBLE LINK
佇列 struct node { int data; struct node * next; }; typedef struct node NODE;
佇列 newnode = (NODE*) malloc(sizeof(NODE)); newnode->data = data; newnode->next = NULL; NODE *newnode;
佇列( LIFO ) insert(&list, newnode); NODE *list=NULL; list 10 list newnode->next = *head; *head = newnode;
佇列( LIFO ) list list *list= (*list)->next;
佇列( LIFO ) list ptr ptr = ptr->next; printf("(%x:%d)->", ptr, ptr->data);
佇列( FIFO ) head 10 tail head 10 tail head 10 tail 20 head 10 tail 20 tail 1 2
佇列( FIFO ) head 10 tail head tail
雙向鏈結 10 struct node{ int data; struct node* right; struct node* left; }; typedef struct node NODE;
雙向鏈結 10 head 10 head 10 head head 2 1 3
雙向鏈結 head head