งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

C Programming Lecture no. 9 Structure.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "C Programming Lecture no. 9 Structure."— ใบสำเนางานนำเสนอ:

1 C Programming Lecture no. 9 Structure

2 ข้อมูลแบบโครงสร้าง (Structures)
Department of Computer Science C Programming

3 1. ความรู้ทั่วไปเกี่ยวกับโครงสร้าง
รูปแบบโดยทั่วๆ ไปของ structure คือ struct ชื่อข้อมูลแบบโครงสร้าง(structure-name) { ประเภทข้อมูล(field-type) ชื่อข้อมูล(field-name); ประเภทข้อมูล(field-type) ชื่อข้อมูล(field-name); } ชื่อตัวแปรข้อมูลแบบโครงสร้าง (variable-name); Department of Computer Science C Programming

4 ตัวอย่างเช่น ในการสร้าง structure ชื่อ complab ให้กับ variable ชื่อ com_part struct complab { char name [30]; int quantity; int cost; } comp_part; Department of Computer Science C Programming

5 สิ่งที่ได้รับจากการกำหนดข้อมูลแบบโครงสร้าง
การกำหนด structure ข้างต้น ให้ข้อมูล 2 อย่างกับ C นั่นคือ การระบุถึง struct complab ว่ามีโครงสร้างเช่นไร ซึ่งจะใช้ชื่อ structure นี้ เป็นชื่อ data type ชนิดใหม่ได้ และยังเป็นการประกาศ (declare) ให้รู้จัก variable ชื่อ comp_part ที่มีโครงสร้างเป็นแบบ complab นอกจากนี้ยังสามารถใช้ชื่อ structure ตัวที่ได้รับการสร้างขึ้นนี้ คือ complab ในการ declareให้กับ variable อื่นๆ ได้เช่น struct complab printer_part;   Department of Computer Science C Programming

6 ข้อสังเกตุ ในการกำหนด structure ถ้าไม่กำหนดชื่อ structure หมายถึงต้องการ declare ให้กับ variable อย่างเดียว ไม่ได้มีการกำหนด data type ชนิดใหม่ขึ้นมา เช่น struct { char name [30]; int quantity; int cost; } comp_part;   Department of Computer Science C Programming

7 ข้อสังเกตุ (2) ในทำนองเดียวกัน ถ้าไม่กำหนด variable name ก็คือการกำหนด data type ขึ้นใหม่ โดยไม่มีการ declare ตัว variable เช่น struct complab { char name [30]; int quantity; int cost; };   Department of Computer Science C Programming

8 ข้อสังเกตุ (3) ในกรณีที่กำหนด structure type ไว้แล้ว สามารถนำไปใช้ declare กับ variable name ต่างๆ ได้ เช่น structure complab monitor_part;   สำหรับ syntax ในการเรียกใช้งาน variable ที่มีการ declare ไว้แล้ว คือ variable.field ดังตัวอย่างการใช้งานต่อไปนี้ เมื่อต้องการกำหนดราคาของ monnitor สามารถทำได้ดังนี้ monitor_part.cost = 7500; Department of Computer Science C Programming

9 การกำหนดค่าเริ่มต้นของตัวแปรประเภทโครงสร้าง
ในการกำหนดค่าเริ่มต้นของตัวแปรประเภทโครงสร้าง (initialize structure variable) สามารถทำได้ในลักษณะดังนี้ /* * Monitor Parts */ struct complab { char name [30]; /* Name of the part */ int quantity; /* How many in the Lab */ int cost; /* The cost of each computer in Baht */ }; struct monitor_part = { "Monitor Samsung", /* Name of the part */ 30, /* There are 30 in the Lab */ /* The cost 7500 Baht */ };   Department of Computer Science C Programming

10 การประกาศตัวแปร pointer ไปยัง structure
struct address { int home_number; char street_name[50]; char city[50]; }; void printadd(struct address *addr); int main(int argc,char argv[ ]) { struct address home,office,*paddr; Department of Computer Science C Programming

11 การประกาศตัวแปร pointer ไปยัง structure (2)
ในที่นี้ home และ office เป็นตัวแปร structure ธรรมดา ซึ่งในการเข้าถึง (access) fields ต่างๆ ใน structure ทำได้ในลักษณะดังนี้ fprintf(stdout,"%d\t%s\t%s\n",office.home_number, office.street_name, office.city); ในขณะที่ paddr เป็นตัวแปร pointer ไปยัง structure ซึ่งเราสามารถที่จะกระทำการ access ได้ในลักษณะดังนี้ fprintf(stdout,"%d\t%s\t%s\n",paddr->home_number, paddr->street_name, paddr->city); Department of Computer Science C Programming

12 ลักษณะการใช้งานข้อมูลแบบโครงสร้าง
(0,0) (4,3) หากต้องการเก็บข้อมูลจุดบนแกนโคออดิเนท จะประกอบไปข้อมูลแกน x และ y เป็นข้อมูลจำนวนเต็มประเภท int ประเภทข้อมูลที่ใช้ได้แก่ประเภทข้อมูลแบบโครงสร้าง สามารถประกาศประเภทข้อมูลที่ใช้ดังนี้ Department of Computer Science C Programming

13 การประกาศประเภทข้อมูลแบบโครงสร้าง
struct point { int x; int y; }; Member หมายเหตุ การประกาศชื่อสมาชิกภายใน struct จะใช้ชื่อใดก็ได้ อาจจะซ้ำกับชื่อตัวแปรที่อยู่ภายนอก struct แต่ชื่อที่อยู่ภายใน struct เดียวกันห้ามประกาศชื่อซ้ำกัน Department of Computer Science C Programming

14 การประกาศตัวแปรข้อมูลแบบโครงสร้าง
แบบที่ 1 struct point { int x; int y; } x, y , z; หมายเหตุ จะเห็นว่าชื่อของ struct จะประกาศหรือไม่ก็ได้ หากไม่มีการประกาศจะไม่สามารถนำ struct นั้นกลับมาใช้ได้อีก Department of Computer Science C Programming

15 การประกาศตัวแปรข้อมูลแบบโครงสร้าง (2) แบบที่ 2
struct point { int x; int y; }; struct point x,y,z การประกาศแบบข้อมูลโครงสร้าง การประกาศตัวแปรข้อมูลแบบโครงสร้าง Department of Computer Science C Programming

16 struct point pt = {320,200}; struct_name.member
การกำหนดค่าเริ่มต้นและการอ้างถึงสมาชิก การกำหนดค่าเริ่มต้นให้กับตัวแปรข้อมูลแบบโครงสร้าง struct point pt = {320,200}; การอ้างถึงสมาชิกภายในตัวแปรข้อมูลแบบโครงสร้าง struct_name.member Department of Computer Science C Programming

17 printf ( “%d, %d”, pt.x, pt.y );
ตัวอย่าง เมื่อต้องการอ้างถึงสมาชิกภายใน struct ว่าอยู่ตรงกับจุดใดบนแกนโคออดิเนทจะใช้ printf ( “%d, %d”, pt.x, pt.y ); หรือหากต้องการคำนวณระยะทางจะว่าห่างจากจุดเริ่มต้น (0, 0) เท่าใดสามารถใช้ double dist, sqrt (double); dist =sqrt ((double)pt.x * pt.x +(double)pt.y * pt.y ); Department of Computer Science C Programming

18 ตัวอย่าง สมาชิกของข้อมูลประเภท struct
สมาชิกของข้อมูลประเภท struct อาจจะเป็นตัวแปรประเภทใดก็ได้ ทั้งข้อมูลพื้นฐานและประเภทข้อมูลอื่น ๆ เช่น อาเรย์ และยังประกาศตัวแปรของข้อมูลประเภท struct ได้อีกด้วย ตัวอย่าง pt2 pt1 หากต้องการเก็บข้อมูลของสี่เหลี่ยมดังรูปสามารถทำการประกาศตัวแปรได้ดังนี้ Department of Computer Science C Programming

19 การประกาศแบบข้อมูลโครงสร้าง การประกาศ ตัวแปรข้อมูลแบบโครงสร้าง
struct rect { struct point pt1; struct point pt2; }; struct rect screen; int co_x; co_x = screen.pt1.x การประกาศแบบข้อมูลโครงสร้าง การประกาศ ตัวแปรข้อมูลแบบโครงสร้าง การอ้างถึงสมาชิก Department of Computer Science C Programming

20 2. การเก็บข้อมูลแบบโครงสร้าง
2. การเก็บข้อมูลแบบโครงสร้าง การเก็บข้อมูลแบบโครงสร้างภายในหน่วยความจำจะเก็บตามลำดับที่มีการประกาศสมาชิกของข้อมูลนั้น โดยทั่วไปข้อมูลแบบโครงสร้างจะประกอบขึ้นจากข้อมูลหลาย ๆ ชนิด และข้อมูลแต่ละชนิดมักจะมีการจองพื้นที่ใช้งานแต่ต่างกัน การจองพื้นที่หน่วยความจำในระบบส่วนใหญ่จะจองที่แอดเดรสที่หารด้วย 2 หรือ 4 ลงตัว Department of Computer Science C Programming

21 ตัวอย่าง 2.1 struct alignment { int num1; char ch; int num2;
member struct alignment { int num1; char ch; int num2; } example; num1 ch num2 3 4 2 Byte Offset จะเห็นว่า num2 จะไม่สามารถใช้พื้นที่ที่ติดกับ ch ได้ เนื่องจาก num2 เป็นข้อมูลประเภทเลขจำนวนต้องใช้พื้นที่ที่มีแอดเดรสหารด้วย 2 หรือ 4 ลงตัว ทำให้เกิดที่ว่างที่ไม่สามารถนำมาใช้ประโยชน์ได้ เพราะฉะนั้นการประกาศสมาชิกของ โครงสร้างจะมีผลต่อการใช้พื้นที่ในหน่วยความจำด้วย Department of Computer Science C Programming

22 3. การใช้ข้อมูลแบบโครงสร้างกับฟังก์ชัน
3. การใช้ข้อมูลแบบโครงสร้างกับฟังก์ชัน การทำงานของตัวแปรที่เป็นประเภทโครงสร้างสามารถทำงานต่าง ๆ ได้เช่นเดียวกับตัวแปรอื่น ๆ ยกเว้นการเปรียบเทียบตัวแปร struct กับตัวแปร struct เนื่องจากข้อมูลของตัวแปร struct จะเก็บอยู่ในตัวแปรที่เป็นสมาชิกของ struct การเปรียบเทียบจึงต้องทำผ่านตัวแปรที่เป็นสมาชิกของ struct เท่านั้น การใช้งานตัวแปร struct กับฟังก์ชั่นสามารถทำได้หลายลักษณะ ทั้งการให้ฟังก์ชั่นคืนค่าเป็น struct การส่งอากิวเมนท์ให้ฟังก์ชั่นเป็นตัวแปร struct Department of Computer Science C Programming

23 ตัวอย่าง 3.1 ฟังก์ชั่นใช้ในการกำหนดค่าให้กับตัวแปร struct
struct point makepoint ( int x, int y ) { struct point temp; temp.x = x; temp.y = y; return temp; } หมายเหตุ ตัวอย่างนี้แสดงฟังก์ชันที่ทำการส่งค่ากลับเป็นรูปแบบโครงสร้าง Department of Computer Science C Programming

24 การเรียกใช้งานฟังก์ชัน
struct rect screen; struct point middle; struct point makepoint ( int, int ); screen.pt1 = makepoint ( 0, 0 ); screen.pt2 = makepoint ( XMAX, YMAX ); middle = makepoint ((screen.pt1.x + screen.pt2.x) / 2, (screen.pt1.y + screen.pt2.y) / 2 ); Department of Computer Science C Programming

25 ตัวอย่าง 5.2 struct point addpoint(struct point p1, struct point p2) {
ฟังก์ชันการบวก x และ y ของจุด 2 จุด และคืนค่าผลของการบวกเป็น struct struct point addpoint(struct point p1, struct point p2) { p1.x += p2.x; p1.y += p2.y; return p1; } หมายเหตุ ตัวอย่างนี้แสดงการส่งอาร์กิวเมนท์แบบ struct ให้กับฟังก์ชัน Department of Computer Science C Programming

26 ฟังก์ชันการหาว่าจุดอยู่ในพื้นที่สี่เหลี่ยมหรือไม่
ตัวอย่าง 5.3 ฟังก์ชันการหาว่าจุดอยู่ในพื้นที่สี่เหลี่ยมหรือไม่ int pinrect ( struct point p, struct rect r ) { return p.x >= r.pt1.x && p.x < r.pt2.x && p.y >= r.pt1.y && p.y < r.pt2.y; } หมายเหตุ ตัวอย่างนี้เป็นการหาว่าจุดที่ระบุอยู่ในพื้นที่สี่เหลี่ยมหรือไม่ โดยส่งค่าจุดและพื้นที่สี่เหลี่ยมเป็นอากิวเมนท์ให้กับฟังก์ชัน หากจุดอยู่ในพื้นที่สี่เหลี่ยมจะคืนค่า1 แต่หากจุดอยู่นอกพื้นที่สี่เหลี่ยมจะคืนค่าเป็น 0 Department of Computer Science C Programming


ดาวน์โหลด ppt C Programming Lecture no. 9 Structure.

งานนำเสนอที่คล้ายกัน


Ads by Google