วันพุธที่ 24 มิถุนายน พ.ศ. 2552

DTS02-23-06-2552

ความหมายของ Structure
หรือโครงสร้าง คือ กลุ่มของข้อมูลที่มีชนิดเหมือนกัน
หรือต่างกันก็ได้ซึ่งนำมารวมกลุ่มแล้วเรียกเป็นชื่อเดียวกัน
• Structure มีประโยชน์มากในการสร้างและจัดการโครงสร้างข้อมูลที่ซับซ้อน
ประเภทของตัวแปร
• ตัวแปรเดี่ยว คือตัวแปรที่ขณะใดขณะหนึ่ง จะเก็บข้อมูลได้ค่าเดียว เช่น
– char ch1;
– int x;
• ตัวแปรชุด คือตัวแปรที่เก็บข้อมูลประเภทเดียวกัน ได้หลายค่า เช่น
– int num[ ] = {5, 7, 1, 18, 20};
– float f[10];
• ตัวแปรชนิด Structure คือตัวแปรที่สามารถเก็บข้อมูลหลายประเภท ภายใต้ชื่อเดียวกัน
การประกาศชนิดข้อมูลแบบ Structure

รูปแบบของ structure
struct struct_name
{
type1 variable1;
type2 variable2;
...
typeN variableN;
} ;

ตัวอย่าง structure
struct Camera {
char series[20];
char brand[10];
float Price;
char type[20];
float effective;
float weight;
char type_picture[10];
char Memory_type[10];

จากตัวอย่าง เป็นการสร้างโครงสร้างข้อมูล ชื่อว่า Camera ซึ่งประกอบด้วยข้อมูล 8 ตัว คือ
series (char)
brand (char)
Price (float)
type (char)
effective (float)
weight (float)
type_picture (char)
Memory_type (char)

การประกาศตัวแปรแบบ Structure
struct Camera {
char series[20];
char brand[10];
float Price;
char type[20];
float effective;
float weight;
char type_picture[10];
char Memory_type[10];} ;
struct Camera product={"DST","SONY",5990.00,"compact",
10,135,"JPEG","Memory stick"};

การประกาศตัวแปรเป็นตัวแปรโครงสร้างชนิด Camera
จากรูปตัวอย่าง เป็นตัวแปรซึ่งมีชนิดข้อมูลเป็นข้อมูลแบบโครงสร้างชนิดใหม่ที่เรากำหนดขึ้นเองโดยใน 1 ตัวแปร สามารถเก็บข้อมูล series (รุ่น) , brand (ยี่ห้อ) ,Price (ราคา), type (ชนิดของกล้อง), effective (ความละเอียด), weight (น้ำหนัก), type_picture (ชนิดของรูปภาพ), Memory_type (ชนิดของความจำ)

การกำหนดค่าเริ่มต้นให้กับตัวแปรแบบ Structure
หลักการคล้ายกับการกำหนดค่าเริ่มต้นให้กับอาร์เรย์
• จะทำการกำหนดค่าเริ่มต้นไว้ในเครื่องหมาย { }และแยกแต่ละค่าออกจากกันด้วยเครื่องหมาย ,

การอ้างถึงตัวแปรที่เป็นสมาชิกของตัวแปรชนิด Structureการอ้างถึงตัวแปรที่เป็นสมาชิกของตัวแปรชนิดโครงสร้าง ทำได้โดยใช้
Struct Camera.product
โดย Struct Camera ชื่อตัวแปรชนิด Structure
product ชื่อตัวแปรที่อยู่ภายใน Structure

ตัวอย่าง
struct Camera {
char series[20];
char brand[10];
float Price;
char type[20];
float effective;
float weight;
char type_picture[10];
char Memory_type[10];
} ; struct Camera product

จากตัวอย่าง ถ้าต้องการนำตัวแปร Memory_type มาใช้งาน
จะอ้างถึงตัวแปร Memory_type ได้โดยใช้ product .Memory_type

โปรแกรม
#include
#include
int main(void)
{
struct Camera {
char series[20];
char brand[10];
float Price;
char type[20];
float effective;
float weight;
char type_picture[10];
char Memory_type[10];
} product;
strcpy(product .series,"DSC");
strcpy(product .brand,"SONY");
product.Price = 5990;
strcpy(product .type,"Compact");
product.effective = 10;
product.weight = 135;
strcpy(product .type_picture,"JPEG");
strcpy(product .Memory_type,"Memory stick");
printf("Series: %s\n”);
printf(“Brand: %s\n”);
printf(“Price: %f\n type: %s\n”);
printf(“effective pixel: %f\n”);
printf(“weight: %f\n”);
printf(“type picture :%s\n”);
printf(“Memory type:%s\n”);
product.series,product.brand,product.Price,
product.type,product.effective,product.weight,product.type_picture,
product.Memory_type);
return 0;
}
การแสดงผล
Series=DSC
Brand =SONY
Price =5990.00
effective pixel=10.00
weight=135.00
type picture= JPEG
Memory type= Memory stick

ไม่มีความคิดเห็น:

แสดงความคิดเห็น