45
ครั ้งที่ 2 การออกแบบโปรแกรมด้วยฟังก์ชันและคลาส

ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

ครงท 2

การออกแบบโปรแกรมดวยฟงกชนและคลาส

Page 2: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Top down design • เทคนคทมการแบงปญหาออกเปนปญหายอยๆใหแยกออกจากกน

ลกษณะบนลงลาง • โดยโปรแกรมจะประกอบดวยโปรแกรมยอยๆตางๆทมการปฏสมพนธ

ตอกนเปนล าดบชน • ผลดของการออกแบบเทคนคนคอ ปญหาไดถกแบงเปนปญหายอยๆ

และโปรแกรมยอย 1 โปรแกรมจะแกปญหายอยไดเพยง 1 ปญหา ท าใหงายตอความเขาใจและการแกไข

Page 3: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function Library Function

C++ ไดเตรยมคลาสทก าหนดไว(predefined class) และฟงกชนในไลบรารมาตรฐาน อทเชน ไลบรารมาตรฐาน cmath

User defined Function

Page 4: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

ไลบรารมาตรฐาน cmath • การท างานจะมการสงผานอากวเมนต x

ไปยงฟงกชน sqrt เมอถกเรยกใช ( function call)

• ถา x มคาเทากบ 16.0 การท างานเปนล าดบขนตอนดงน

• ดงนนฟงกชน sqrt จะท าการค านวณหาคารากทสองของ 16.0 ซงคอคา 4.0

• ซงผลลพธของฟงกชนมคาเทากบ 4.0 ซงจะมผลให y มคาเทากบ 4.0

Page 5: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

การท างานของฟงกชน • เรยกวา“black box”

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

Page 6: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

/ / File: squareRoot.cpp

/ / Performs three square root computations

#include <cmath> / / sqrt function

#include <iostream> / / i/o functions

using namespace std;

int main ( )

{

float first; / / input : one of two data values

float second; / / input : second of two data values

float answer: / / output : a square root value

/ / Get first number and display its square root.

cout << “Enter the first number: “ ;

cin >> first ;

answer = sqrt ( first );

cout << “The square root of the first number is “ << answer << endl ;

/ / Get second number and display its square root.

cout << “Enter the second number : “;

cin >> second;

answer = sqrt (second);

cout << “The square root of the second number is “ << answer << endl;

/ / Display the square root of the sum of first and second.

answer = sqrt ( first + second );

cout << “The square root of the sum of both numbers is “ << andwer << endl;

return 0;

}

Page 7: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน
Page 8: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Top-Down Design และ Structure Charts ปญหา ตองการวาดภาพบาน และ คน

Page 9: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

การวเคราะห รปบานนน เปนการพมพ -รปสามเหลยมทไมมฐาน -และพมพ รปของ สเหลยม รปคนนน -พมพ รปวงกลม -พมพ สามเหลยมทไมมฐาน -พมพรปเสนตรง -และพมพรปสามเหลยมทไมมฐาน อกครงหนง ตามล าดบ

Page 10: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

การวเคราะห • a circle เปนภาพวงกลม • a base line เปนเสนตรง • parallel lines เปนเสน

ขนาน

• intersecting lines เปนสามเหลยมทไมมฐาน

Page 11: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

การออกแบบ INITIAL ALGORITHM • วาดวงกลม • วาดสามเหลยม • วาดสามเหลยมไมมฐาน ALGORITHM REFINEMENTS

เนองจากรปสามเหลยมไมม เราสามารถก าหนดรปนไดจากองคประกอบอนๆ Step 2 Refinement

– วาดสามเหลยมไมมฐาน – วาดเสนตรง

Page 12: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

structure chart

Page 13: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function • Function without

Arguments • Functions with

Input Arguments

• Function with Multiple Arguments

• Functions with Input Arguments and Single Result

• Function Pass by Value and Reference Parameters

Page 14: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function without Arguments

Page 15: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function Prototypes

Page 16: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function prototypes and main function for drawing a stick figure / / Draws a stick figure (main function only) #include <iostream> using namespace std; / / Functions used … void drawCircle ( ) ; / / Draws a circle void drawTriangle ( ); / / Draws a triangle void drawIntersect ( ); / / Draws intersecting lines void drawBase ( ); / / Draws a horizontal line int main ( ) { / / Draw a circle. drawCircle ( ); / / Draw a triangle. drawTriangle ( ); / / Draw intersecting lines. drawIntersect ( ); return 0; }

Page 17: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function Definitions

Page 18: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

//Draws a circle

void drawCircle( )

{ cout << “ * “ << endl ;

cout << “* *” << endl ;

cout << ” * * “ << endl ;

}

// Draws a triangle

void drawTriangle ( )

{

drawIntersect ( ) ;

drawBase ( ) ;

}

Page 19: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Program to draw a stick figure / / File: stickFigure.cpp / / Drawa a stick figure #include <iostream> using namespace std; / / Functions used … void drawCircle ( ) ; void drawTriangle ( ) ; void drawIntersect ( ) ; void drawBase ( ) ; int main ( ) { / / Draw a circle. drawCircle ( ) ; / / Draw a triangle. drawTriangle ( ) ; / / Draw intersecting lines. drawIntersect ( ) ; return 0 ; }

/ / Draws a circle void drawCircle ( ) { cout << “ * “ << endl; cout << “ * * “ << endl; cout << “ * * “ << endl; } / / end drawCircle / / Draws a triangle void drawTriangle ( ) { drawIntersect ( ); drawBase( ); } / / end drawTriangle / / Draws intersecting lines void drawIntersect ( ) { cout << “ / \ \ “ << endl; cout << “ / \ \ “ << endl; cout << “ / \ \ “ << endl; } / / end drawIntersect / / Draws a horizontal line void drawBase ( ) { cout << “ _ _ _ _ _ _ “ << endl; } / / end drawBase

Page 20: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

แสดงการเรยกใชระหวางฟงกชนหลกและโปรแกรมยอย

Page 21: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Functions with Input Arguments

Page 22: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Call Functions with Input Arguments

Page 23: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Prototype

void drawCircleChar (char) ;

Page 24: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Functions with Input Arguments and Single Result

Page 25: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Functions with Input Arguments and Single Result

Page 26: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Functions with Input Arguments and Single Result

Page 27: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Functions findCircum and findArea float findCircum (float r)

{

return (2.0 * PI * r );

}

float findArea (float r )

{

return ( PI * r * r );

}

Page 28: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Functions findCircum

การเรยกใชเปนดงน radius = 10.0 ;

circum = findCircum(radius) ;

Page 29: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Function with Multiple Arguments

/ / specified by its second argument.

/ / Pre : x and n are defined and library

// cmath is included.

float scale ( float x, int n )

{

float scaleFactor ; / / local variable

scaleFactor = pow(10 , n) ;

return ( x * scaleFactor ) ;

}

Page 30: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Scope of Names void one (int anArg, double second) ; / / prototype 1 int funTwo (int one, char anArg); / / prototype 2 const int MAX = 950; const int LIMIT = 200; int main ( ) { int localVar ; . . . } / / end main void one (int anArg, double second) / / header 1 { int oneLocal ; / / local 1 . . . } / / end one int funTwo (int one, char anArg) / / header 2 { int localVar ; / / local 2 . . . } / / end funTwo

Page 31: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

Value and Reference Parameters

• ในการแกปญหาบางอยางนน เราตองการใหการท างานของฟงกชนนนสามารถสงผานขอมลไดมากกวา 1 คา

• เราใชเครองหมาย &(ampersand) เปนตวก าหนด โดยพารามเตอรตวใดมการระบ & จะแสดงใหทราบวามการสงผานแบบอางอง โดยจะไมก าหนดหรอจองเนอทใหมในการปฏบตงานในฟงกชน แตจะใชเนอทเดยวกบพารามเตอรทสงผานมาจากจดเรยกใช

Page 32: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

โปรแกรมหาคาผลรวมและคาเฉลย ก าหนดชอฟงกชน และการเรยกใชไดดงน

computeSumAve(x ,y , sum ,mean) ;

การเรยกใชฟงกชนนมการสงผานพารามเตอร 4 ตวดวยกนโดย x, y ,

sum ,mean เปน Actual Argument ซง x , y เปนคาของขอมลทเปนเลขจ านวนจรงใดๆซงท าหนาทเปนขอมลเขา

ใหแกฟงกชน โดยกอนการเรยกใชคา x ,y ตองมคาเกบอยกอนแลว แต sum และ mean ยงไมมคาใดๆ แตเมอมการเรยกใชฟงกชนนผลลพธ

จากการท างานจะใหคาแก sum และ mean

Page 33: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

#include <iostream> using namespace std; // Function prototype void computeSumAve(float , float ,

float& , float&); int main () { float x , // input – first number y , // input – second

number sum, // output – their sum mean ;// output – their

average cout << “Enter 2 numbers: “ ; cin >> x >> y ; computeSumAve(x , y , sum , mean);

// Display results cout << “Sum is “ << sum <<

endl ; cout << “Average is “ << mean

<< endl ; return 0 ; }

void computeSumAve (float num1 ,

float num2 , float& sum , float& average)

{ sum = num1 + num2 ; average = sum / 2.0 ; }

Page 34: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

computeSumAve(x ,y , sum ,mean) ;

void computeSumAve (float num1 , float num2

,float& sum ,float& average)

{ sum = num1 + num2 ; average = sum / 2.0 ; }

Page 35: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

computeSumAve(x ,y , sum ,mean) ;

void computeSumAve (float num1 , float num2

,float& sum ,float& average)

{ sum = num1 + num2 ; average = sum / 2.0 ; }

18.0

9.0

Page 36: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

ตวอยางท 1 จงเขยนโปรแกรม รบเลขจ านวนเตม 5 จ านวน จงหา 1. ผลรวมของตวเลขเหลาน 2. หาคาเฉลยของตวเลขเหลาน โดยสรางเปนฟงกชน

int sum(int.int.int.int,int);

double average(int);

Page 37: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

ตวอยางท 2

จงเขยนโปรแกรมหาจ านวนชวโมง , นาท และวนาท โดยสรางเปนฟงกชนดงน

void Find_time(int,int&,int&,int&);

Page 38: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

ตวอยางท 3 จงเขยนฟงกชนในการสลบคาขอมล 2 ตว

ตวแปร X Y กอนท างาน 6 3 หลงท างาน 3 6 เขยนอยางไร

Page 39: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

#include <iostream> using namespace std; void Test1(int& ,int); int main() { int a,b,c ; a = 3 ; b = 9; c = 5 ; Test1(b , c) ; return 0; } void Test1(int& a ,int b) { int c=4 ; a = b ; b= c ; c = a ; }

หลงจากท างาน ตวแปร a , b , c มคาเทาใด

แบบฝกหด

Page 40: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

#include <iostream> using namespace std; void Test2(int ,int&); int main() { int a,b,c ; a = 3 ; b = 9; c = 5 ; Test2(c , a) ; return 0; } void Test2(int a ,int& b ) { int c=4 ; a = b ; b= c ; c = a ; }

หลงจากท างาน ตวแปร a , b , c มคาเทาใด

แบบฝกหด

Page 41: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

#include <iostream> using namespace std; void Test3(int& ,int&); int main() { int a,b,c ; a = 3 ; b = 9; c = 5 ; Test3(a , b) ; return 0; } void Test3(int& b,int& a) { int c=4 ; a = b ; b= c ; c = a ; }

หลงจากท างาน ตวแปร a , b , c มคาเทาใด

แบบฝกหด

Page 42: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

#include <iostream> using namespace std; void Test4(int ,int); int main() { int a,b,c ; a = 3 ; b = 9; c = 5 ; Test4(b , c) ; return 0; } void Test4(int a ,int b) { int c=4 ; a = b ; b= c ; c = a ; }

หลงจากท างาน ตวแปร a , b , c มคาเทาใด

แบบฝกหด

Page 43: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

#include <iostream> using namespace std; int Test5(int ,int) ; int main() { int a,b,c ; a = 3 ; b = 9; c = 5 ; a = Test5(c , b) ; return 0; } int Test5(int a ,int b) { int c = 4 ; C = C+B; return c ; }

หลงจากท างาน ตวแปร a , b , c มคาเทาใด

แบบฝกหด

Page 44: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

การบานขอท 1

จงเขยนโปรแกรมหาพนทของสามเหลยม ,พนทของสเหลยม โดยผใชปอน ความยาวฐาน และสวนสงใดๆทางแปนพมพ

จงเสรางฟงกชนในการหาพนทดงน

float Triangle(float , float);

void Rectangle(float ,float ,float&);

Page 45: ครั้งที่ 2 - Ramkhamhaeng Universityinstructor.ru.ac.th/urai/cos2101/COS2101_2.pdf · 2014-07-09 · และฟังก์ชนัในไลบรารีมาตรฐาน

การบานขอท 2

จงเขยนโปรแกรมในการคดดอกเบย โดยสรางฟงกชนในการคดดอกเบยประเภท 1 ป 6 เดอน และ 3 เดอน

โปรแกรมนใหผใชปอนเงนตน และ อตราดอกเบยตอป การท างานจะพมพวาถาฝากประเภท 1 ปไดเงนเทาใด

ฝากประเภท 6 เดอนไดเงนเทาใดใน 1 ป ฝากประเภท 3 เดอนไดเงนเทาใดใน 1 ป