STRUCTURE name age tel email namecard char name10
![STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard: char name[10]; int age; char STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard: char name[10]; int age; char](https://slidetodoc.com/presentation_image_h2/ed6eda46bfe2531360e6261feb29a9b0/image-1.jpg)
![結構的定義 struct namecard { char name[20]; int age; char tel[10]; char e-mail[60]; }; 結構的定義 struct namecard { char name[20]; int age; char tel[10]; char e-mail[60]; };](https://slidetodoc.com/presentation_image_h2/ed6eda46bfe2531360e6261feb29a9b0/image-2.jpg)


![結構陣列 cardbook[] Mike 30 Rose 24 Lily 28 Jack 27 Tom 19 0 1 結構陣列 cardbook[] Mike 30 Rose 24 Lily 28 Jack 27 Tom 19 0 1](https://slidetodoc.com/presentation_image_h2/ed6eda46bfe2531360e6261feb29a9b0/image-5.jpg)
![結構陣列的輸入、輸出 NAMECARD cardbook[5]; for(i=0 ; i<5 ; i++) { printf("Enter you name: n"); scanf("%s", 結構陣列的輸入、輸出 NAMECARD cardbook[5]; for(i=0 ; i<5 ; i++) { printf("Enter you name: n"); scanf("%s",](https://slidetodoc.com/presentation_image_h2/ed6eda46bfe2531360e6261feb29a9b0/image-6.jpg)

![結構指標與結構陣列 NAMECARD cardbook[3]={{"MIKE", 29}, {"COCO", 30}, {"John", 25}}; NAMECARD *cptr; cptr=cardbook; for(i=0 ; i<3 結構指標與結構陣列 NAMECARD cardbook[3]={{"MIKE", 29}, {"COCO", 30}, {"John", 25}}; NAMECARD *cptr; cptr=cardbook; for(i=0 ; i<3](https://slidetodoc.com/presentation_image_h2/ed6eda46bfe2531360e6261feb29a9b0/image-8.jpg)





















- Slides: 29
STRUCTURE 為一種複合式的資料型態,可用來把相關的資料組織 成為有意義的資料單元。 名片: name age tel e-mail namecard: char name[10]; int age; char tel[10] char e-mail[60]
結構的定義 struct namecard { char name[20]; int age; char tel[10]; char e-mail[60]; };
結構的變數 struct namecard mycard; typedef struct namecard NAMECARD mycard; NAMECARD;
結構的變數初始值、輸入、輸出 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 %sn", A. name); printf("You are %d years oldn", A. age);
結構陣列 cardbook[] Mike 30 Rose 24 Lily 28 Jack 27 Tom 19 0 1 2 3 4 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, %dn", cardbook[i]. name, cardbook[i]. age);
結構指標 NAMECARD A={“Mike”, 30}, B={“Rose”, 28}; NAMECARD *cptr; cptr = &A; printf(“nanme: %s, age: %dn”, cptr->name, cptr->age}; cptr =&B; printf(“nanme: %s, age: %dn”, 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, %dn", 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 : 平面座標點 struct point { int x; int y; }; . B(5, 12) . A(2, 3) . C(16, 8)
Case Study : 兩點距離 float distance(POINT, POINT); . B(5, 12) . A(2, 3)
Case Study : 求重心 . B(5, 12) . . A(2, 3) G . B(16, 8)
Case Study : Poker Game struct int int }; card { point; type; 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%sn", type[poker[u]. type], point[poker[u]. point]); printf("I choose : n"); scanf("%d", &i); printf("%c%sn", 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 10 20 30 40
串列 . 佇列 (FIFO、LIFO). 雙向鏈結DOUBLE LINK 10 10 20 30 20 40 30 40
佇列 struct node { int data; struct node * next; }; typedef struct node NODE;
佇列 NODE *newnode; newnode = (NODE*) malloc(sizeof(NODE)); newnode->data = data; newnode->next = NULL;
佇列(LIFO) NODE *head=NULL; void insert(NODE**, NODE*); insert(&head, newnode); 20 head 10 newnode->next = *head; *head = newnode; head 10 *head = newnode; 2 20 10 1
佇列(LIFO) void delete(NODE**); head 10 20 30 40 head *list= (*list)->next;
佇列(LIFO) showlist(NODE*); ptr head 10 20 30 printf("(%x: %d)->", ptr->data); ptr = ptr->next; 40
佇列(FIFO) tail head 10 10 20 tail head 2 head 10 10 1 20
佇列(FIFO) tail head 10 20 30 40
雙向鏈結 struct node{ int data; struct node* right; struct node* left; }; typedef struct node NODE; 10
雙向鏈結 head 10 10 3 head 2 20 1 10
雙向鏈結 head 10 20 30 40 head