[rp]structure

12
Structure 1 Structure ข้อมูลชนิดโครงสร้าง รู้จักตัวแปรข้อมูลชนิดโครงสร้าง (Struct) Struct คือ ตัวแปรข้อมูลที่มีการรวมตัวแปรหลายชนิดเข้าไว้ด้วยกันภายใต้ชื่อสาหรับใช้อ้างอิงเพียง ชื่อเดียว ทาให้สะดวกในกรณีที่ต้องการจัดเก็บข้อมูลที่มีความสัมพันธ์กันเอาไว้ด้วยกัน เหมาะสาหรับใช้ในการ เขียนโปรแกรมเพื่อเก็บข้อมูลแบบรายการข้อมูลที่มีชนิดข้อมูลแต่ละตัวแตกต่างกัน เช่น ข้อมูลนักเรียนทีประกอบด้วย รหัสประจาตัว, ชื่อ, สาขาวิชา หรือรายการสินค้าที่ประกอบด้วย ชื่อสินค้า, ราคา, จานวน เป็นต้น ซึ่งเราเรียกรายการข้อมูลแต่ละรายการว่า เร็คคอร์ด (Record) การกาหนดรูปแบบของตัวแปรข้อมูลชนิดโครงสร้าง (Struct) ในภาษาซีใช้คีย์เวิร์ดคาว่า struct การกาหนดรูปแบบของตัวแปร structure เป็นการสร้างแบบ (Template) ที่จะสามารถนาไปใช้ในการสร้างตัวแปรแบบ structure ขึ้น ตัวแปรย่อย ๆ ที่รวมกันเป็น structure นั้นเรียกว่าเป็นสมาชิก (Elements) ของ structure การกาหนดรูปแบบของ structure (structure template) มี 3 รูปแบบ ดังนีแบบที่ 1 ตัวอย่าง ข้อมูลของนักศึกษา(รหัสนักศึกษา, ชื่อนักศึกษา, อายุ, สาขาวิชา, เกรดเฉลี่ย) struct student { char stdcode[4]; //รหัสนักศึกษา char stdname[20]; //ชื่อนักศึกษา int age; //อายุ char major[30]; //สาขาวิชา float GPA; //เกรดเฉลี่ย }; struct ชื่อของ struct { type ชื่อของตัวแปรสมาชิก 1; type ชื่อของตัวแปรสมาชิก 2; type ชื่อของตัวแปรสมาชิก N; } ;

Upload: phiokham-suriya

Post on 01-Oct-2014

68 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: [RP]Structure

Structure 1

Structure

ขอมลชนดโครงสราง รจกตวแปรขอมลชนดโครงสราง (Struct)

Struct คอ ตวแปรขอมลทมการรวมตวแปรหลายชนดเขาไวดวยกนภายใตชอส าหรบใชอางองเพยง ชอเดยว ท าใหสะดวกในกรณทตองการจดเกบขอมลทมความสมพนธกนเอาไวดวยกน เหมาะส าหรบใชในการเขยนโปรแกรมเพอเกบขอมลแบบรายการขอมลทมชนดขอมลแตละตวแตกตางกน เชน ขอมลนกเรยนทประกอบดวย รหสประจ าตว, ชอ, สาขาวชา หรอรายการสนคาทประกอบดวย ชอสนคา, ราคา, จ านวน เปนตน ซงเราเรยกรายการขอมลแตละรายการวา เรคคอรด (Record)

การก าหนดรปแบบของตวแปรขอมลชนดโครงสราง (Struct)

ในภาษาซใชคยเวรดค าวา struct การก าหนดรปแบบของตวแปร structure เปนการสรางแบบ (Template) ทจะสามารถน าไปใชในการสรางตวแปรแบบ structure ขน ตวแปรยอย ๆ ทรวมกนเปน structure นนเรยกวาเปนสมาชก (Elements) ของ structure

การก าหนดรปแบบของ structure (structure template) ม 3 รปแบบ ดงน แบบท 1

ตวอยาง ขอมลของนกศกษา(รหสนกศกษา, ชอนกศกษา, อาย, สาขาวชา, เกรดเฉลย)

struct student {

char stdcode[4]; //รหสนกศกษา char stdname[20]; //ชอนกศกษา int age; //อาย char major[30]; //สาขาวชา float GPA; //เกรดเฉลย

};

struct ชอของ struct

{

type ชอของตวแปรสมาชก 1;

type ชอของตวแปรสมาชก 2;

type ชอของตวแปรสมาชก N;

} ;

Page 2: [RP]Structure

Structure 2

แบบท 2

ตวอยาง ขอมลของนกศกษา(รหสนกศกษา, ชอนกศกษา, อาย, สาขาวชา, เกรดเฉลย)

struct {

char stdcode[3]; //รหสนกศกษา char stdname[20]; //ชอนกศกษา int age; //อาย char major[30]; //สาขาวชา float GPA; //เกรดเฉลย

} student;

แบบท 3 เปนการก าหนดรปแบบของ Structure ดวยค าสง typedef โดยประกาศไวตรงสวนถดจาก preprocessing directive (#include)

หรอ

struct {

type ชอของตวแปรสมาชก 1;

type ชอของตวแปรสมาชก 2;

type ชอของตวแปรสมาชก N;

} ชอของ struct ;

typedef struct ชอของ struct

{

type ชอของตวแปรสมาชก 1;

type ชอของตวแปรสมาชก 2;

type ชอของตวแปรสมาชก N;

};

typedef struct {

type ชอของตวแปรสมาชก 1;

type ชอของตวแปรสมาชก 2;

type ชอของตวแปรสมาชก N;

} ชอของ struct ;

Page 3: [RP]Structure

Structure 3

ตวอยาง ขอมลของนกศกษา(รหสนกศกษา, ชอนกศกษา, อาย, สาขาวชา, เกรดเฉลย)

typedef struct {

char stdcode[4]; //รหสนกศกษา char stdname[20]; //ชอนกศกษา int age; //อาย char major[30]; //สาขาวชา float GPA; //เกรดเฉลย

} student;

การประกาศใชตวแปรขอมลชนดโครงสราง (structure)

เมอเราไดรปแบบของตวแปรขอมลชนดโครงสราง(Structure template) ตามทเราตองการแลว เราจะตองท าการประกาศตวแปรชนดโครงสรางขอมลดงกลาวขนมาใชงาน โดยมรปแบบการประกาศตวแปรดงน

ส าหรบการก าหนดรปแบบตวแปรขอมลชนดโครงสราง(Structure template) แบบท 1 และ แบบท 2

เชน struct student mystudent;

โดยท name of struct คอ ชอของ struct ทก าหนดไวในขนตอนการก าหนดรปแบบ (Structure

template)

Mystudent คอ ชอตวแปรทเราตองการก าหนดใหเปนแบบ struct

ก าหนดรปแบบตวแปรขอมลชนดโครงสราง(Structure template) แบบท 3 (typedef)

เชน student mystudent; ** สงเกตวา เราจะไมคยเวรดค าวา struct น าหนา **

โดยท name of struct คอ ชอของ struct ทก าหนดไวในขนตอนการก าหนดรปแบบ(Structure

template)

Mystudent คอ ชอตวแปรทเราตองการก าหนดใหเปนแบบ struct

การเรยกใชงานตวแปรขอมลชนดโครงสรา (Structure)

ในหวขอทผานมาเราไดเรยนรวธการสราง Structure template และการประกาศใชตวแปร struct

ไปแลว ตอไปเราจะไดเรยนรการเรยกใชงานตวแปร struct ทเราสรางขนมา วามการก าหนดคาขอมลอยางไร มการอานคาขอมลอยางไร ซงมรปแบบการเรยกใชงานดงน

รปแบบ : struct name of struct ชอตวแปร struct;

รปแบบ : name of struct ชอตวแปร struct;

รปแบบ : ชอตวแปร struct.element name

Page 4: [RP]Structure

Structure 4

โดยท element name คอ ชอของตวแปรสมาชกใด ๆ ของ struct ทก าหนดไวในขนตอนการก าหนด

รปแบบ (Structure template) ทเราตองการเรยกใช เชน mystudent.stdcode //เรยกใชตวแปรสมาชกชอ stdcode ทมชนดตวแปรเปนแบบ string

การก าหนดคาขอมลใหกบ struct

เมอประกาศตวแปร struct ขนมาใชงานแลว สมาชกแตละตวจะยงคงไมมคาขอมล ดงนนกอนใชงานตวแปร struct จะตองก าหนดคาขอมลใหกบตวแปร struct กอน ซงศกษาไดจากตวอยางตอไปน

ค าอธบายการท างาน

จากซอสโคด ในตวอยางดานบนมกระบวนการท างานดงน 1. สรางและก าหนด structure template

#include<stdio.h> #include<conio.h> #include<string.h>

//ก าหนด structure template

typedef struct { char stdcode[4]; char stdname[20]; int age; char major[30]; float GPA; }student; int main() {

student mystudent; //ประกาศใชงานตวแปร struct

//ก าหนดคาใหกบสมาชกแตละตวของตวแปร struct ชอ mystudent

strcpy(mystudent.stdcode , "001"); strcpy(mystudent.stdname ,"Suriya Phiokham"); mystudent.age = 23; strcpy(mystudent.major ,"Computer Engineering"); mystudent.GPA = 3.00;

return 0; }

Structure Template student

stdcode Null

Stdname Null

age Null

GPA Null

Page 5: [RP]Structure

Structure 5

2. ประกาศใชงานตวแปร struct ชอวา mystudent

3. ก าหนดคาใชกบสมาชกแตละตวของตวแปร struct

- stdcode = 001 - stdname = Suriya Phiokham - age = 23 - major = Computer Engineering - GPA = 3.00

ผลการท างาน

การอานคาขอมลจากตวแปร struct

เมอตวแปร struct มคาขอมลแลว ตอไปกเปนเรยกใชงานขอมลทเกบอยในตวแปร struct หรอกคอ การอานคาขอมลจากตวแปร struct นนเอง ซงศกษาไดจากตวอยางตอไปน

student

mystudent

stdcode Null

Stdname Null

age Null

GPA Null

student

mystudent

stdcode 001

Stdname Suriya Phiokham

age 23

GPA 3.00

#include<stdio.h> #include<conio.h> #include<string.h>

//ก าหนด structure template

typedef struct { char stdcode[4]; char stdname[20]; int age; char major[30]; float GPA; }student;

Page 6: [RP]Structure

Structure 6

ผลการรนโปรแกรม จากซอสโคดโปรแกรมดานบน ไดผลการรนโปรแกรมดงน

โครงสรางขอมลแบบอาเรย (Array Structure)

จากหวขอทผานมา เราทราบแลววาตวแปรขอมลชนดโครงสรางขอมล structure เหมาะส าหรบการเกบรายการขอมล ซงในความเปนจรงแลว ขอมลตาง ๆ อาจจะมมากกวา 1 รายการ เชน รายชอนกศกษาในชนเรยน รายการสนคาในคลง เปนตน ดงนน เราสามารถประกาศตวแปร struct ขนมาใชในแบบอาเรย เพอใหสามารถเกบขอมลไดมากกวา 1 รายการ (หลายเรคคอรด) ซงรปแบบกเหมอนกบการประกาศตวแปรอาเรยทวไป ตวอยางเชน

ตวอยางขางตน เปนการประกาศ structure แบบ array ขนาด 6 รายการ

int main() {

student mystudent; //ประกาศใชงานตวแปร struct

//ก าหนดคาใหกบสมาชกแตละตวของตวแปร struct ชอ mystudent

strcpy(mystudent.stdcode , "001"); strcpy(mystudent.stdname ,"Suriya Phiokham"); mystudent.age = 23; strcpy(mystudent.major ,"Computer Engineering"); mystudent.GPA = 3.00;

//อานคาขอมลและแสดงผลออกทางหนาจอ

printf("Student code : %s\n",mystudent.stdcode); printf("Student Name : %s\n",mystudent.stdname); printf("Student Age : %d\n",mystudent.age); printf("Student Major : %s\n",mystudent.major); printf("Student GPA : %.2f\n",mystudent.GPA); getch(); return 0; }

รปแบบ : student mystudent[6];

Page 7: [RP]Structure

Structure 7

ผลลพธทได คอ

......................

การก าหนดคาตวแปร struct เราสามารถก าหนดใหขณะประกาศตวแปรไดเลย ซงศกษาไดจากตวอยางโคดโปรแกรมตอไปน

student

Mystudent[1]

stdcode Null

Stdname Null

age Null

GPA Null

student

Mystudent[6]

stdcode Null

Stdname Null

age Null

GPA Null

#include<stdio.h> #include<conio.h> #include<string.h> typedef struct { char stdcode[4];

char stdname[21];

int age; char major[30]; float GPA; }student; int main()

{ //การประกาศตวแปร struct แบบอาเรย พรอมกบก าหนดคาขอมล student mystudent[6]={ {"001","Suriya Phiokham",23,"Computer Engineering",3.00}, {"002","Kanyarat Phasertnok",23,"Accounting",3.50}, {"003","Sittinun Jangnok",23,"Management",3.45}, {"004","Atchara Phapsooknern",23,"Information System",3.50}, {"005","Kotchakon Sangmueang",23,"Finance",3.70}, {"006","Kitiya Atsiri",23,"Computer science",3.20} };

//แสดงคาขอมลในตวแปร struct แบบอาเรย for(int i=0; i<6; i++){ printf("Student code : %s\n",mystudent[i].stdcode); printf("Student Name : %s\n",mystudent[i].stdname); printf("Student Age : %d\n",mystudent[i].age); printf("Student Major : %s\n",mystudent[i].major); printf("Student GPA : %.2f\n",mystudent[i].GPA); printf("\n"); } getch(); return 0; }

Page 8: [RP]Structure

Structure 8

ผลการรนโปรแกรม

จากซอสโคดตวอยาง ไดผลการรนโปรแกรมดงน

โครงสรางขอมลแบบพอยเตอร (Pointer Structure)

การใชงานตวแปรขอมลชนดโครงสรางขอมล structure ในแบบพอยเตอรนน จะมลกษณะการใชงานเหมอนกบการใชงานตวแปรพอยเตอรทวไป สามารถศกษาไดจากตวอยางโคดโปรแกรมตอไปน

#include<stdio.h> #include<conio.h> #include<string.h>

//ก าหนด structure template

typedef struct { char stdcode[4]; char stdname[21]; int age; char major[30]; float GPA; }student;

Page 9: [RP]Structure

Structure 9

ตวอยางโปรแกรมขางตน ใชวธการสงผานขอมลใหกบฟงกชน showdata() ในแบบพอยเตอร สงเกตวารปแบบการใชงานเหมอนกบการสงผานขอมลดวยตวแปรพอยเตอรทวไป

void showdata(student *p); int main()

{ //การประกาศตวแปร struct แบบอาเรย พรอมกบก าหนดคาขอมล student mystudent[6]={ {"001","Suriya Phiokham",23,"Computer Engineering",3.00}, {"002","Kanyarat Phasertnok",23,"Accounting",3.50}, {"003","Sittinun Jangnok",23,"Management",3.45}, {"004","Atchara Phapsooknern",23,"Information System",3.50}, {"005","Kotchakon Sangmueang",23,"Finance",3.70}, {"006","Kitiya Atsiri",23,"Computer science",3.20} };

showdata(mystudent); //เรยกใชฟงกชนแสดงคาขอมล getch(); return 0; }

//ฟงกชนแสดงคาขอมลในตวแปร struct แบบอาเรย void showdata(student *p){ int i; for(i=0;i<6;i++){ printf("Student code : %s\n",p[i].stdcode); printf("Student Name : %s\n",p[i].stdname); printf("Student Age : %d\n",p[i].age); printf("Student Major : %s\n",p[i].major); printf("Student GPA : %.2f\n",p[i].GPA); printf("\n"); } }

Page 10: [RP]Structure

Structure 10

ผลการรนโปรแกรม

จากซอสโคดตวอยาง ไดผลการรนโปรแกรมดงน

โครงสรางขอมลซอนโครงสรางขอมล ในบางครงโครงสรางขอมลทเราตองการจดเกบนน มโครงสรางทซบซอนมากกวา 1 ระดบ ดงนน เราอาจจะจดการขอมลดงกลาวโดยใชตวแปรชนดโครงสรางขอมล structure มากวา 1 ตวซอนกน หมายความวาในตวแปรชนดโครงสรางขอมล struct ตวหนง มสมาชก(Element) เปน ตวแปรชนดโครงสรางขอมล struct

อกตวหนง ตวอยางโคดตอไปน เปนโครงสรางขอมลสนคาทมสมาชกประกอบดวย รหสสนคา , ชอสนคา, ประเภท, ราคา, และคณลกษณะ โดยทคณลกษณะนนเปนตวแปรชนดโครงสรางขอมลทมสมาชกประกอบดวย

ส และน าหนกสทธ

สนคา {รหสสนคา,ชอสนคา, ประเภท, ราคา, คณลกษณะ(ส,น าหนกสทธ)}

Page 11: [RP]Structure

Structure 11

ตวอยางโคดโปรแกรม : โครงสรางขอมลสนคา

#include<stdio.h> #include<conio.h> #include<string.h> typedef struct { char color[10]; float netW; }property; typedef struct { char proId[4]; char proName[20]; char kind[20]; float price; property PProper; }product; void showlist(product *prod); int main() { product mystock[3]={ {"A01","Coca-Cola","beverage",27.05,{"Black",12.25}}, {"A02","Twelve Plus”, “Powder ",98.00,{"Pink",150.00}}, {"A03","NIVEA ","Body cream",195.00,{"White",400.00}} };

showlist(mystock); //เรยกใชฟงกชนแสดงรายการสนคา getch(); return 0; }

//ฟงกชนแสดงรายการสนคา

void showlist(product *prod){ int i; clrscr(); printf("Stock List:\n"); printf("ProID \tName \t\tKind \t\tPrice:Unit \tColor \tNet weight\n"); printf("===================================================================== ==========="); for(i=0;i<3;i++){ printf("%s \t%s \t%s \t%.2f \t\t%s \t%.2f",prod[i].proId,prod[i].proName, prod[i].kind,prod[i].price,prod[i].PProper.color,prod[i].PProper.netW); printf("\n"); } }

Page 12: [RP]Structure

Structure 12

ผลการรนโปรแกรม : โครงสรางขอมลสนคา

By : Suriya Phiokham

Computer Engineering RMUTI