8 0 01006012 Computer Programming Student 1 name

  • Slides: 30
Download presentation

8. 0 ตวแปรลกษณะโครงสราง ? 01006012 Computer Programming Student 1 name = “Somchai” surname =

8. 0 ตวแปรลกษณะโครงสราง ? 01006012 Computer Programming 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 การประกาศตวแปรโครงสราง 01006012 Computer Programming ����� 1 struct type ……… type }; name

8. 1 การประกาศตวแปรโครงสราง 01006012 Computer Programming ����� 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 การประกาศตวแปรโครงสราง (ตวอยาง ) 01006012 Computer Programming ����� 1 ����� 2 struct student

8. 1 การประกาศตวแปรโครงสราง (ตวอยาง ) 01006012 Computer Programming ����� 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 การอางถงตวแปรในโครงสราง 01006012 Computer Programming ����������� struct product { ��������� char name[15]; �

8. 2 การอางถงตวแปรในโครงสราง 01006012 Computer Programming ����������� 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 ประกาศตววแปรในโครงสราง 01006012 Computer Programming #include<stdio. h> salary bonus age #include<conio. h>

โปรแกรม 8. 1 ประกาศตววแปรในโครงสราง 01006012 Computer Programming #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 โปรแกรมเกบขอมลหนงสอ 01006012 Computer Programming #include<stdio. h> #include<conio. h> int main() {

โปรแกรม 8. 2 โปรแกรมเกบขอมลหนงสอ 01006012 Computer Programming #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; 13

โปรแกรม 8. 2 โปรแกรมเกบขอมลหนงสอ 01006012 Computer Programming printf ("nn. Book : %sn", book 1.

โปรแกรม 8. 2 โปรแกรมเกบขอมลหนงสอ 01006012 Computer Programming 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; } 14

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

8. 4 การกำหนดคาเรมตนใหโครงสราง 01006012 Computer Programming ����� #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 15 ����� 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, 01006012 Computer Programming 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 17

8. 5 ประกาศตวแปรโครงสรางแบบอารเรย 01006012 Computer Programming #include<stdio. h> int main() { struct profile{ char

8. 5 ประกาศตวแปรโครงสรางแบบอารเรย 01006012 Computer Programming #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; } 18

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

โปรแกรม 8. 3 เกบขอมลนศ. จำนวน 10 คน 01006012 Computer Programming 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 20

โปรแกรม 8. 3 เกบขอมลนศ. จำนวน 10 คน 01006012 Computer Programming #include<stdio. h> #include<conio. h>

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

โปรแกรม 8. 3 เกบขอมลนศ. จำนวน 10 คน 01006012 Computer Programming for(i=0; i<10; i++) {

โปรแกรม 8. 3 เกบขอมลนศ. จำนวน 10 คน 01006012 Computer Programming 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; } 22

8. 6 โครงสรางซอนโครงสราง (Nest structure) 01006012 Computer Programming ����������� Nest structure ������� C ���������

8. 6 โครงสรางซอนโครงสราง (Nest structure) 01006012 Computer Programming ����������� Nest structure ������� C ��������� C ������ A struct A{ ��� B �������. . . Struct A Struct B Struct C }; struct B{. . . }; struct C{ struct A data_a; struct B data_b; . . . }; 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) 01006012 Computer Programming 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 24

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; 01006012 Computer Programming 25

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

8. 6 โครงสรางซอนโครงสราง (Nest structure) ตวอยางโปรแกรม 01006012 Computer Programming 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; } 26

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

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

8. 7 คำถามทายบท 1. struct 01006012 Computer Programming product { char name[15]; int count;

8. 7 คำถามทายบท 1. struct 01006012 Computer Programming 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; 28

8. 7 คำถามทายบท 01006012 Computer Programming 3. การประกาศตวแปรขอใดใชพนทรวมไดหนวยความจำ 12ไบท . 1 struct data{int item;

8. 7 คำถามทายบท 01006012 Computer Programming 3. การประกาศตวแปรขอใดใชพนทรวมไดหนวยความจำ 12ไบท . 1 struct data{int item; double price[3]} A; . 2 char i[3][3]; int a_str; . 3 struct data{char name[8]; float. 4 int i[3][4]; char a_str; . 5ไมมขอใดถกตอง money; } A; 30