1 / 29

STRUCTURE

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

reilly
Download Presentation

STRUCTURE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 : 平面座標點 struct point { int x; int y; }; . B(5,12) .C(16,8) . A(2,3)

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

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

  13. Homework : 複數 • 請定義一名為complex的複數結構,此結構裡有兩個浮點數 • r, v; • 試定義出下列四個函示 • 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. 10 20 30 40 串列 串列的操作 . Add node . Delete node . Show list

  19. 10 20 30 40 串列 .佇列 (FIFO、LIFO) .雙向鏈結DOUBLE LINK 10 20 30 40

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

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

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

  23. 10 20 30 40 10 20 30 40 佇列(LIFO) void delete(NODE**); head head *list= (*list)->next;

  24. head 10 20 30 40 printf("(%x:%d)->", ptr, ptr->data); ptr = ptr->next; 佇列(LIFO) showlist(NODE*); ptr ptr

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

  26. 10 20 30 40 10 20 30 40 佇列(FIFO) tail head tail head

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

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

  29. 雙向鏈結 head 10 20 30 40 head 10 20 30 40

More Related