1 / 14

ตัวแปรชนิดโครงสร้าง (Structure)

ตัวแปรชนิดโครงสร้าง (Structure). http://www.renvi.src.ku.ac.th. QUIZ. 4. Today Agenda. struct ?. 1. 2. การใช้งานตัวแปรชนิดโครงสร้าง. ตัวแปรชนิดโครงสร้างกับอาร์เรย์. 3. เกริ่นนำเกี่ยวกับ สตรัคเจอร์.

etoile
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) http://www.renvi.src.ku.ac.th

  2. QUIZ 4 Today Agenda struct ? 1 2 การใช้งานตัวแปรชนิดโครงสร้าง ตัวแปรชนิดโครงสร้างกับอาร์เรย์ 3

  3. เกริ่นนำเกี่ยวกับสตรัคเจอร์เกริ่นนำเกี่ยวกับสตรัคเจอร์ • สตรัคเจอร์(Structure) คือ โครงสร้างข้อมูลที่นำเอาข้อมูลที่มีประเภทข้อมูลชนิดพื้นฐานที่แตกต่างกันแต่มีความสัมพันธ์กันมารวมกันเป็นโครงสร้างข้อมูลชนิดใหม่ เช่น การเก็บข้อมูลนิสิต (student) จะต้องประกอบไปด้วยข้อมูลรหัสนิสิต, ชื่อ (name), นามสกุล (surname), สาขาวิชา (major), คณะ (faculty) เป็นต้น

  4. การใช้งานตัวแปรชนิดโครงสร้างการใช้งานตัวแปรชนิดโครงสร้าง

  5. การกำหนดโครงสร้างตัวแปรชนิดโครงสร้างการกำหนดโครงสร้างตัวแปรชนิดโครงสร้าง • ก่อนจะสร้างตัวแปรชนิดสตรัคเจอร์มาใช้งานได้นั้น เราต้องกำหนดโครงสร้างข้อมูลสำหรับตัวแปรนั้นก่อน ดังนี้ รูปแบบที่ 1 structชื่อของโครงสร้างที่จะสร้าง{ ชนิดข้อมูลพื้นฐานชื่อตัวแปร; ชนิดข้อมูลพื้นฐาน ชื่อตัวแปร; … ชนิดข้อมูลพื้นฐาน ชื่อตัวแปร; }; ตัวอย่างการใช้งาน structstudent { int stu_id; charstu_name[20]; charstu_surname[20]; charmajor[20]; charfaculty[20]; };

  6. การกำหนดโครงสร้างตัวแปรชนิดโครงสร้าง (ต่อ) รูปแบบที่ 2 typedefstruct { ชนิดข้อมูลพื้นฐานชื่อตัวแปร; ชนิดข้อมูลพื้นฐาน ชื่อตัวแปร; … ชนิดข้อมูลพื้นฐาน ชื่อตัวแปร; } ชื่อของโครงสร้างที่จะสร้าง; ตัวอย่างการใช้งาน typedefstruct { int stu_id; charstu_name[20]; charstu_surname[20]; charmajor[20]; charfaculty[20]; }STUDENT;

  7. การสร้างตัวแปรชนิดโครงสร้างการสร้างตัวแปรชนิดโครงสร้าง • เมื่อกำหนดโครงสร้างข้อมูลให้กับตัวแปรตามต้องการแล้ว ต่อไปคือขั้นตอนที่เราจะสร้างตัวแปรจากโครงสร้างที่เราได้กำหนดไว้ โดยมีรูปแบบดังนี้ หากกำหนดโครงสร้างในรูปแบบที่ 1 structชนิดของข้อมูลชนิดโครงสร้าง ชื่อตัวแปร; เช่น structstudentstu1, stu2; หากกำหนดโครงสร้างในรูปแบบที่ 2 ชนิดของข้อมูลชนิดโครงสร้าง ชื่อตัวแปร; เช่น STUDENTstu1, stu2;

  8. การกำหนดค่าเริ่มต้นให้กับตัวแปรชนิดโครงสร้างการกำหนดค่าเริ่มต้นให้กับตัวแปรชนิดโครงสร้าง • คล้ายกับการกำหนดค่าเริ่มต้นให้กับอาร์เรย์ กำหนดค่าไว้ใน { } และแยกค่าแต่ละค่าด้วยเครื่องหมาย ,(comma) เช่น หากกำหนดโครงสร้างในรูปแบบที่ 1 struct student stu = {101, “Steve”, “Josh”, “CS”, “RENVI”}; หากกำหนดโครงสร้างในรูปแบบที่ 2 STUDENT stu = {101, “David”, “Paul”, “IT”, “RENVI”};

  9. การอ้างถึงสมาชิกในตัวแปรชนิดโครงสร้างการอ้างถึงสมาชิกในตัวแปรชนิดโครงสร้าง • การอ้างถึงตัวแปรหรือสมาชิกแต่ละตัวในตัวแปรประเภท struct สามารถทำได้ด้วยรูปแบบดังนี้ ชื่อตัวแปรชนิดโครงสร้าง.ชื่อตัวแปรสมาชิก

  10. การกำหนดข้อมูลให้กับตัวแปรชนิดโครงสร้างการกำหนดข้อมูลให้กับตัวแปรชนิดโครงสร้าง • การกำหนดค่าต้องกำหนดให้ถูกต้องตามชนิดข้อมูลของสมาชิก • typedefstruct { • int stu_id; • char stu_name[20]; • char stu_surname[20]; • }STUDENT; • STUDENT stu; • printf("Enter student ID: "); • scanf(“____", __________); • printf("Enter name: "); • scanf(“____", __________); • printf("Enter surname: "); • scanf(“____", ___________); • printf("ID: %d, Name: %s, Surname: %s", stu.stu_id, stu.stu_name, stu.stu_surname);}

  11. การแสดงผลสมาชิกในตัวแปรชนิดโครงสร้าง (ต่อ) • สมาชิกของตัวแปรชนิดโครงสร้างเป็นตัวแปรชนิดพื้นฐานเราจึงใช้คำสั่ง printf() ตามปกติ โดยใช้รหัสควบคุมการแสดงผลให้ถูกต้องตามชนิดข้อมูลสมาชิก • typedefstruct { • int stu_id; • char stu_name[20]; • char stu_surname[20]; • }STUDENT; • STUDENT stu = {101, "Steve", "Jobs"}; • printf("----- Student Info -----\n\n"); • printf("Student ID: ____\n", __________); • printf("Name: ____\n", __________); • printf("Surname: ____\n", __________);

  12. ตัวแปรชนิดโครงสร้างกับการใช้อาร์เรย์ตัวแปรชนิดโครงสร้างกับการใช้อาร์เรย์ • เราสามารถสร้างตัวแปรอาร์เรย์ของตัวแปรชนิดโครงสร้างได้ ในกรณีที่มีความจำเป็นต้องใช้ตัวแปรชนิดโครงสร้างตัวเดียวกันหลาย ๆ ตัว ตัวอย่างการใช้งาน typedefstruct { int stu_id; charstu_name[20]; charstu_surname[20]; charmajor[20]; charfaculty[20]; }; STUDENT stu[10]; …. ….

  13. ตัวแปรชนิดโครงสร้างกับการใช้อาร์เรย์ (ต่อ) • typedefstruct { • int stu_id; • char stu_name[20]; • char stu_surname[20]; • }STUDENT; • STUDENT stu[5]; • for(i=0; i<5; i++){ • printf("Enter student ID: "); • scanf(“____", __________); • printf("Enter name: "); • scanf(“____", __________); • printf("Enter surname: "); • scanf(“____", ___________); • printf("Added !\n\n"); • } • for(i=0; i<5; i++){ • printf("%d. ID: ____\tName: ____\tSurname: ____\n", i+1,__________, __________, __________); • }

  14. It’s Time to do your QUIZ :) http://www.renvi.src.ku.ac.th

More Related