24

Lab Chapter7 Structure And Function

  • Upload
    -

  • View
    2.107

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Lab Chapter7 Structure And Function
Page 2: Lab Chapter7 Structure And Function

ตวแปรแบบ Structure

structure เปนกลมขอมลซงประกอบดวยขอมลชนดเดยวกนหรอหลายชนดกไดขอมลแตละรายการใน structure เรยกวา สมาชกของสตรกเจอร (members of structure)

Page 3: Lab Chapter7 Structure And Function

โครงสรางตวแปรแบบ Structure

struct StructName{ DataType

FieldName ; DataType

FieldName ;DataType FieldName ;DataType FieldName ;

}StructVariable ;

ชอ Struct

ชนดขอมล

ชอ รายการ

ชอตวแปร Struct

Page 4: Lab Chapter7 Structure And Function

ตวอยางโครงสรางตวแปรแบบ Structure

struct STD{ string ID; string Name;

int ThaiScore ;int SocialScore ;int MathScore ;

}StudentScore ;

Page 5: Lab Chapter7 Structure And Function

การเรยกใช Structure และ สมาชกของ Structure

struct STD{ string ID; string

Name;int ThaiScore ;int SocialScore ;int MathScore ;

}StudentScore ;

สามารถเรยกใชไดโดย ชอตวแปร เครองหมายจด ชอรายการ ของ Struct

StudentScore.IDStudentScore.NameStudentScore.ThaiScoreStudentScore.SocialScoreStudentScore.MathScore

Page 6: Lab Chapter7 Structure And Function

ลองทำาด

เขยนโปรแกรม ในรปแบบ Structure เพอเกบขอมลดงตอไปน

ID //รหสนกศกษาMidtermScore //คะแนนกลางภาคFinalScore //คะแนนปลายภาคAnotherScore //คะแนนอนๆระหวางภาค

จากนนใหคำานวณเกรด เกบไวในตวแปร Grade แลวแสดงผลลพธทงหมดออกสหนาจอ

Page 7: Lab Chapter7 Structure And Function
Page 8: Lab Chapter7 Structure And Function

ฟงกชนคออะไร ?

คอ ชดคำำสงทเขยนขนเพอกำรทำำงำนเฉพำะอยำง และสำมำรถเรยกใชงำนไดหลำยๆ ครง โดยไมตองเขยนชดคำำสงดงกลำวใหม

ฟงกชนในภำษำ C++ สำมำรถแบงเปน 2 ประเภท คอ ฟงกชนมำตรฐำน หรอ ไลบรำรฟงกชน

(Library Function) ฟงกชนทผใชสรำงขนเอง (User Defined

Function)

Page 9: Lab Chapter7 Structure And Function

ฟงกชนมำตรฐำน หรอ ไลบรำรฟงกชน (Library Function)

คอ ฟงกชนมำตรฐำนทบรษทผสรำง Compiler ภำษำ C++ ได สรำงรวบรวมไวในคลง (Library) ผเขยนโปรแกรมสำมำรถเรยกใชไดทนทไดแก ฟงกชนทประกอบอยใน header file ตำง ๆ ขณะเรยกใช ตอง #include ชอไฟลทรวบรวมฟงกชนนนไวกอน เชน ฟงกชน pow( , ) ตอง include Library File

math.h

Page 10: Lab Chapter7 Structure And Function

User Defined Function (ฟงกชนทผใชสรำงขนเอง)

คอ ฟงกชนทผเขยนโปรแกรมสรำงขนหรอกำำหนดขนเอง ตำมรปแบบกำรสรำงฟงกชนของ C++ เพอนำำมำใชในกำรเขยนโปรแกรมของตนเอง

Page 11: Lab Chapter7 Structure And Function

รปแบบกำรเขยน Function

ประกำศ Prototype

สรำง function ประกำศสวนหวของฟงกชน เขยนคำำสงตำงๆ ทตองทำำในฟงกชนนนๆ ถำมกำรสงคำกลบมำยงฟงกชนทเรยก ใหเพมคำำ

สง return (ชอตวแปร);

เรยกใชฟงกชน

Page 12: Lab Chapter7 Structure And Function

กำรประกำศ Prototype

ตองค ำำน ง !! ถงรปแบบฟงกชนกอนประกำศ Prototype โดยรปแบบฟงกชนม 3 รปแบบ ดงตอไปน

ฟงก ช นท ม ร บและส งค ำกล บฟ งก ช นท ม กำรร บแต ไม ม กำรส งค ำกล บฟ งก ช นท ไม ม กำรร บและส งค ำกล บ

Page 13: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ม ร บและส งค ากล บ

DataType FunctionName (Datatype Var1, Datatype Var2){

statement(s);return(value);

}

Page 14: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ตวอย างฟ งก ช นท ม ร บและส งค ากล บint square (int width, int height){

int result=0;result=width*height;return(result);

}

Page 15: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ตวอย างฟ งก ช นท มร บและส งค ากล บ ( ฟงก ช น main)

Void main (){

int width=0, height=0, result=0;cin>>width>>height;result= square(width, height);cout<<result;

}

Page 16: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ม การร บแต ไมม การส งกล บ

void FunctionName (Datatype Var1, Datatype Var2){

statement(s);}

Page 17: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ม การร บแต ไม มการส งกล บVoid square (int width, int height){

int result=0;result=width*height;cout<<result;

}

Page 18: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ม การร บแต ไม มการส ง กลบ ( ฟงก ช น main)

Void main (){

int width=0, height=0;cin>>width>>height;square(width, height);

}

Page 19: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ไม ม การร บและส งค ากล บ

void FunctionName (){

statement(s);}

Page 20: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ไม ม การร บและส งค ากล บVoid square (){

int width=0, height=0, result=0;cin>>width>>height;result=width*height;cout<<result;

}

Page 21: Lab Chapter7 Structure And Function

การประกาศ Prototype (ตอ)

ฟงก ช นท ไม ม การร บและส งค า กลบ ( ฟงก ช น main)Void main (){

square();}

Page 22: Lab Chapter7 Structure And Function

int square (int,int); void main(){

result=square(width, height);

}

int square (int width, int height){

int result=0;result=width*height;return(result);

}

Page 23: Lab Chapter7 Structure And Function

int square (int width, int height){

int result=0;result=width*height;return(result);

}

void main(){

result=square(width, height);

}

Page 24: Lab Chapter7 Structure And Function

Question

?