Student 1 name Somchai surname Jaidee age 18

  • Slides: 31
Download presentation

ทำไมตองศกษาตวแปรลกษณะโครงสราง ? Student 1 name = “Somchai” surname = “Jai-dee” age = 18 gender

ทำไมตองศกษาตวแปรลกษณะโครงสราง ? 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

รปแบบการประกาศโครงสรางขอมล ����� 1 struct type ……… type }; name { var_1; var_2; var_n; �����

รปแบบการประกาศโครงสรางขอมล ����� 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

ตวอยางการประกาศตวแปรชนดโครงสราง ����� 1 ����� 2 struct student { char name[15]; char surname[30]; int age;

ตวอยางการประกาศตวแปรชนดโครงสราง ����� 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

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

การอางถงตวแปรในโครงสราง ����������� 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

ตวอยางการกำหนดคาใหแกตวแปรในโ ครงสราง #include<stdio. h> salary bonus age #include<conio. h> people 1 16000 40000 23

ตวอยางการกำหนดคาใหแกตวแปรในโ ครงสราง #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

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

ตวอยางการกำหนดคาใหแกตวแปรในโ ครงสราง #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

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

โปรแกรมเกบขอมลหนงสอ 1 เลม #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

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

โปรแกรมเกบขอมลหนงสอ 1 เลม 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

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

ตวอยางอารเรยของตวแปรชนดโครงสร าง #include<stdio. h> #include<conio. h> int main() { struct profile{ char name[20]; int

ตวอยางอารเรยของตวแปรชนดโครงสร าง #include<stdio. h> #include<conio. h> int main() { struct profile{ char name[20]; int age; char grade; }; struct profile student[10]; return 0; } #include<stdio. h> #include<conio. h> int main() { struct profile{ char name[20]; int age; char grade; } student[10]; return 0; } 19

ตวอยางโปรแกรมเกบขอมลนกศกษา คน Student[0] name: joy age: 12 Student[1] name: boy age: 20 Student[2] name:

ตวอยางโปรแกรมเกบขอมลนกศกษา คน 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 10 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

โปรแกรมเกบขอมลนกศกษา 10 คน #include<stdio. h> #include<conio. h> #include<string. h> int main() { int i;

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

โปรแกรมเกบขอมลนกศกษา 10 คน for(i=0; i<10; i++) { printf("Student[%d]n", i); printf("t name: "); scanf("%s", s[i].

โปรแกรมเกบขอมลนกศกษา 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

สตรคเจอรซอนสตรคเจอร Structure) struct int char }; struct char }; name address{ num; moo; road[20];

สตรคเจอรซอนสตรคเจอร Structure) struct int char }; struct char }; name address{ num; moo; road[20]; district[20]; province[20]; (Nested 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

ตวอยางสตรคเจอรซอนสตรคเจอร #include<stdio. h> #include<conio. h> #include<string. h> int main() { struct address{ int add;

ตวอยางสตรคเจอรซอนสตรคเจอร #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

ตวอยางสตรคเจอรซอนสตรคเจอร strcpy (king. name, ”King Mongkut's Institute of Technology Ladkrabang”); king. place. add =

ตวอยางสตรคเจอรซอนสตรคเจอร 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

ตวอยางสตรคเจอรซอนสตรคเจอร King Mongkut's Institute of Technology Ladkrabang Address : 3 Moo 2, Chalongkrung Rd.

ตวอยางสตรคเจอรซอนสตรคเจอร King Mongkut's Institute of Technology Ladkrabang Address : 3 Moo 2, Chalongkrung Rd. Ladkrabang, Bangkok 28

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

คำถามทายบท 1. struct (1) 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