8 0 Student 1 name Somchai surname Jaidee

  • Slides: 31
Download presentation

8. 0 ตวแปรลกษณะโครงสราง ? Student 1 name = “Somchai” surname = “Jai-dee” age =

8. 0 ตวแปรลกษณะโครงสราง ? Student 1 name = “Somchai” surname = “Jai-dee” age = 18 gender = “Male” Student 2 name = “Sompong” surname = “Nong. Som. Chai” age = 17 gender = “Male” Student 3 name = “Somying” surname = “Na-rak” age = 18 gender = “Female” 4

8. 1 การประกาศตวแปรโครงสราง ����� 1 struct type ……… type }; name { var_1; var_2;

8. 1 การประกาศตวแปรโครงสราง ����� 1 struct type ……… type }; name { var_1; var_2; var_n; ����� 2 struct name { type var_1; type var_2; ……… type var_n; } struct_var; struct name struct_var; 7

8. 1 การประกาศตวแปรโครงสราง (ตวอยาง ) ����� 1 ����� 2 struct student { char name[15];

8. 1 การประกาศตวแปรโครงสราง (ตวอยาง ) ����� 1 ����� 2 struct student { char name[15]; char surname[30]; int age; char gender[7]; }; struct student st 1, st 2; struct student { char name[15]; char surname[30]; int age; char gender[7]; } st 1, st 2; 8

8. 2 การอางถงตวแปรในโครงสราง ����������� struct product { ��������� char name[15]; � int count; float

8. 2 การอางถงตวแปรในโครงสราง ����������� struct product { ��������� char name[15]; � int count; float price; ������ }; struct_var. var_name struct product pr 1, pr 2; ��������� name count ������ ". " pr 1. name pr 1. count ������ pr 2. name pr 2. count ��������� pr 1. name pr 1. count price pr 1. price pr 2. price 9

โปรแกรม 8. 1 ประกาศตววแปรในโครงสราง #include<stdio. h> salary bonus age #include<conio. h> people 1 16000

โปรแกรม 8. 1 ประกาศตววแปรในโครงสราง #include<stdio. h> salary bonus age #include<conio. h> people 1 16000 40000 23 int main() { struct income { float salary; float bonus; int age; }; struct income people 1; people 1. salary = 16000; people 1. bonus = 40000; people 1. age = 23; return 0; } 11

โปรแกรม 8. 2 กำหนดคาใหตวแปรโครงสราง #include<stdio. h> #include<conio. h> #include<string. h> int main() { struct

โปรแกรม 8. 2 กำหนดคาใหตวแปรโครงสราง #include<stdio. h> #include<conio. h> #include<string. h> int main() { struct letter { char name[20]; char surname[30]; char message[50]; } first; printf scanf strcpy printf return ("Enter name : "); ("%s", first. name); ("Enter surname : "); ("%s", first. surname); (first. message, "Hi"); ("%s", first. message); ("n%s", first. name); (" %s", first. surname); 0; } 12

โปรแกรม 8. 3 โปรแกรมเกบขอมลหนงสอ #include<stdio. h> #include<conio. h> int main() { struct book {

โปรแกรม 8. 3 โปรแกรมเกบขอมลหนงสอ #include<stdio. h> #include<conio. h> int main() { struct book { char name[50]; float price; float discount; } book 1; printf ("Enter book name : "); gets (book 1. name); printf ("Enter book price : "); scanf ("%f", &book 1. price); book 1. discount = 0. 1*book 1. price; 14

โปรแกรม 8. 3 โปรแกรมเกบขอมลหนงสอ printf ("nn. Book : %sn", book 1. name); printf ("Price

โปรแกรม 8. 3 โปรแกรมเกบขอมลหนงสอ printf ("nn. Book : %sn", book 1. name); printf ("Price : %. 2 fn", book 1. price); printf ("Discount 10 percent : %. 2 fn" "Total price : %. 2 f", book 1. discount, book 1. price - book 1. discount); return 0; } 15

8. 4 การกำหนดคาเรมตนใหโครงสราง ����� #include<stdio. h> #include<conio. h> ����� int main() ����� { �����

8. 4 การกำหนดคาเรมตนใหโครงสราง ����� #include<stdio. h> #include<conio. h> ����� int main() ����� { ����� struct book { char name[50]; ����� float price; ����� } b 1 = {"harry", 120}; struct book b 2 = {"Using C", 150}; ����� struct book b 3 = {150, "Pascal" }; Error ����� return 0; ����� } � ����� name char[50] price float b 1 harry b 2 Using C 150 b 3 ��������� 150 16 ����� Pascal 120

struct profile student 1, student 2, student 3, student 4, student 5, student 6,

struct profile student 1, student 2, student 3, student 4, student 5, student 6, student 7, student 8, student 9, student 10; student 2 student 10 name[20] age age grade struct profile student[10]; student[0] student[1] student[9] name[20] age age grade 18

8. 5 ประกาศตวแปรโครงสรางแบบอารเรย #include<stdio. h> int main() { struct profile{ char name[20]; int age;

8. 5 ประกาศตวแปรโครงสรางแบบอารเรย #include<stdio. h> int main() { struct profile{ char name[20]; int age; char grade; }; struct profile student[10]; return 0; } #include<stdio. h> int main() { struct profile{ char name[20]; int age; char grade; } student[10]; return 0; } 19

โปรแกรม 8. 4 เกบขอมลนศ. จำนวน 10 คน Student[0] name: joy age: 12 Student[1] name:

โปรแกรม 8. 4 เกบขอมลนศ. จำนวน 10 คน Student[0] name: joy age: 12 Student[1] name: boy age: 20 Student[2] name: jo age: 23 Student[3] name: pat age: 21 Student[4] name: ple age: 13 Student[5] name: tom age: 11 Student[6] name: tu age: 25 Student[7] name: tee age: 34 Student[8] name: bat age: 44 Student[9] name: phon age: 33 jo, 23 pat, 21 tu, 25 tee, 34 bat, 44 phon, 33 21

โปรแกรม 8. 4 เกบขอมลนศ. จำนวน 10 คน #include<stdio. h> #include<conio. h> #include<string. h> int

โปรแกรม 8. 4 เกบขอมลนศ. จำนวน 10 คน #include<stdio. h> #include<conio. h> #include<string. h> int main() { int i; struct profile{ char name[20]; int age; } s[10]; 22

โปรแกรม 8. 4 เกบขอมลนศ. จำนวน 10 คน for(i=0; i<10; i++) { printf("Student[%d]n", i); printf("t

โปรแกรม 8. 4 เกบขอมลนศ. จำนวน 10 คน for(i=0; i<10; i++) { printf("Student[%d]n", i); printf("t name: "); scanf("%s", s[i]. name); printf("t age: "); scanf("%d", &s[i]. age); } for(i=0; i<10; i++) if(s[i]. age > 20) printf("n %s, %d", s[i]. name, s[i]. age); return 0; } 23

8. 6 โครงสรางซอนโครงสราง struct int char }; struct char }; name address{ num; moo;

8. 6 โครงสรางซอนโครงสราง struct int char }; struct char }; name address{ num; moo; road[20]; district[20]; province[20]; (Nest structure) struct student{ char name[20]; char surname[20]; char id[9]; sturct address add; struct phone tel; }; phone{ home[10]; mobile[10]; surname id add num moo road district province tel home mobile 25

8. 6 โครงสรางซอนโครงสราง ตวอยางโปรแกรม (Nest structure) #include<stdio. h> #include<conio. h> #include<string. h> int main()

8. 6 โครงสรางซอนโครงสราง ตวอยางโปรแกรม (Nest structure) #include<stdio. h> #include<conio. h> #include<string. h> int main() { struct address{ int add; int moo; char road[20]; char district[20]; char province[20]; }; struct university{ char name[70]; struct address place; }; struct university king; 26

8. 6 โครงสรางซอนโครงสราง (Nest structure) ตวอยางโปรแกรม strcpy (king. name, ”King Mongkut's Institute of Technology

8. 6 โครงสรางซอนโครงสราง (Nest structure) ตวอยางโปรแกรม strcpy (king. name, ”King Mongkut's Institute of Technology Ladkrabang”); king. place. add = 3; king. place. moo = 2; strcpy(king. place. road, ”Chalongkrung”); strcpy(king. place. district, ”Ladkrabang”); strcpy(king. place. province, ”Bangkok”); printf ("%sn", king. name); printf ("Address : %d Moo %d, %s Rd. n", king. place. add, king. place. moo, king. place. road); printf (" %s, %s", king. place. district, king. place. province); return 0; } 27

8. 6 โครงสรางซอนโครงสราง (Nest structure) ตวอยางโปรแกรม King Mongkut's Institute of Technology Ladkrabang Address :

8. 6 โครงสรางซอนโครงสราง (Nest structure) ตวอยางโปรแกรม King Mongkut's Institute of Technology Ladkrabang Address : 3 Moo 2, Chalongkrung Rd. Ladkrabang, Bangkok 28

8. 7 คำถามทายบท 1. struct product { char name[15]; int count; float price; };

8. 7 คำถามทายบท 1. struct product { char name[15]; int count; float price; }; struct product pr 1, pr 2; คำสงขอใดไมถกตอง 1. 3. 5. pr 1. count = 10; 2. pr 1. price = 130. 5; 4. pr 1. name = “Programming”; pr 2. count = 30; pr 2. price = 240. 75; 29