53
Mr.Warawut Khangkhan Mr.Warawut Khangkhan Twitter: http://twitter.com/awarawut Facebook: http://www.facebook.com/AjWarawut E-mail: [email protected] Mobile: 083-0698-410

พื้นฐานการออกแบบโปรแกรม

  • Upload
    warawut

  • View
    878

  • Download
    0

Embed Size (px)

DESCRIPTION

พื้นฐานการออกแบบโปรแกรม (Fundamental of Program Design)

Citation preview

Page 1: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut KhangkhanMr.Warawut KhangkhanTwitter: http://twitter.com/awarawut

Facebook: http://www.facebook.com/AjWarawutE-mail: [email protected]

Mobile: 083-0698-410

Page 2: พื้นฐานการออกแบบโปรแกรม

ObjectiveObjective1. ��������������� �������

2. �������������������������������� ������������ !"

3. ���#������ !$�� !����%� ������� Procedural ��$Object-Oriented ���Object-Oriented ���

4. '����()�$*!#+��!�'#��#������� �����,�! #!*�!

5. ���#����%��!������'-���$.� � #�����

6. ����������/������������ 6 �$���!#��������+ ���0��1��*� ����%�.� � #������% !(����������

Mr.Warawut 2Chapter 1 Review

Page 3: พื้นฐานการออกแบบโปรแกรม

ContentsContents� Steps in Program Development

� Program Design Methodology

� ����%� ������� Procedural ��$ Object-Oriented

Objective of Structured Program Design Techniques� Objective of Structured Program Design Techniques

� Introduction to Algorithm and Pseudo Code

� Six Basic Computer Operations

� The Three Basic Control Structures

Mr.Warawut Chapter 1 Review 3

Page 4: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 4

Page 5: พื้นฐานการออกแบบโปรแกรม

Steps in Program DevelopmentSteps in Program Development1. �1����23� (Define the Problem)

2. !%�$���%����'!�������23� (Outline the Solution)

3. ����������'-� (Develop and Algorithm)

4. ���*��#��(�����!��!������'-� (Test the Algorithm for 4. ���*��#��(�����!��!������'-� (Test the Algorithm for

Correctness)

5. ���%� ���� (Programming)

6. '�*�� ���� (Testing)

7. ���'1���*��$�1)!��4 ���� (Document and Maintain

the Program)

Mr.Warawut Chapter 1 Review 5

Page 6: พื้นฐานการออกแบบโปรแกรม

Define the Program

� Inputs

� Outputs

� Processing

Mr.Warawut Chapter 1 Review 6

Page 7: พื้นฐานการออกแบบโปรแกรม

Outline the Solution� ���!������5�,���!�% �%" �����5�������5���� (���!���1����23�)

� � !%�$���%����'!�������23�� !" �$������%� ���������$���8�* ������"

� * ��������!!�'�0���������% �% (Subtask)� * ��������!!�'�0���������% �% (Subtask)

� * ��#��*������+���8���,�!�

� #!*�!'�0�,�#��#)� �, � ���.�1 �����1���'!�����

� �������$ #!*�!��!�#�+�

� ��$ ���� (Logic)

Mr.Warawut Chapter 1 Review 7

Page 8: พื้นฐานการออกแบบโปรแกรม

Develop and Algorithm� �������'�0�,�����%�1����'1!� ��$������/����������������!������'-�'�0

����

� .� � #�� (Pseudo Code) ��5�����'�������'-����0��,�������23�'!#��������+#��������+

Mr.Warawut Chapter 1 Review 8

Page 9: พื้นฐานการออกแบบโปรแกรม

Test the Algorithm for Correctness� ��5��������'�0*1#�3'�0*)�

� ���*��'��! ��$��!������'-� ��������� ��$��1������'�*��������$���8����� �$�������

Mr.Warawut Chapter 1 Review 9

Page 10: พื้นฐานการออกแบบโปรแกรม

Programming� �1������'-�'�0�������������% !*���9+��������%����%� ����

(,)�#1*�0!)

� ������,�:4$���*�! (High Language) ���0����%� ���� �, � C, PASCAL ��5����PASCAL ��5����

Mr.Warawut Chapter 1 Review 10

Page 11: พื้นฐานการออกแบบโปรแกรม

Testing� �1�������;����������0�'�*�����#�0�!��� ����'�0������%��-��� (�����!�����

� ����*��� �����,)�#1*�0! (Syntax Errors)

� ���� (Logic Errors)� ���� (Logic Errors)

� ������'�*�����!��#������ !������������ �$*(��9+��$*:��������

Mr.Warawut Chapter 1 Review 11

Page 12: พื้นฐานการออกแบบโปรแกรม

Document and Maintain the

Program� ����'1���*�$��� �����$���!���'1���!�� ���������1����23���(-!

�-�����*)�'�% #�� �'�*��8�����+

� ���*�$��� �����$�$������%� ���*:%��� (External Document) �, � 8�! #!*�! ������'-�'�0�,�� ���*:%��� (External Document) �, � 8�! #!*�! ������'-�'�0�,�

������23� ��$8���!�'�*��������

� ���*:%�� (Internal Document) #�� ,)�#1*�0!�� ����

� ��1)!��4 �����$���0%����!���!�������$����)! ����

Mr.Warawut Chapter 1 Review 12

Page 13: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 13

Page 14: พื้นฐานการออกแบบโปรแกรม

Program Design MethodologyProgram Design Methodology� ������� ������� Procedure-Driven

� ���#��#�� - ���$���� (Processes) ���=2!�+,�� (Functions) � ������� ������� Event-Driven

� ���#��#�� - ���)�9+���� �������:%�����5�*1#�3 '�0* !8�� � ������� ���#��#�� - ���)�9+���� �������:%�����5�*1#�3 '�0* !8�� � �����������!�����0%����!���� �$*(�$

� ������� ������� Data-Driven� ���#��#�� - �������� �������� �$���� �%��0����������#$�+

��������$#��*������+$�� !������ ����1��� #!*�!�����������!��� ��$���0� #!*�!���������(���1����-�� #�����!���8�����+��!�������>�$(�����9���1���(����� ���$������'�0�$'1����!�������1������0���*� 8�����+������+�)�'�0���!�

Mr.Warawut Chapter 1 Review 14

Page 15: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 15

Page 16: พื้นฐานการออกแบบโปรแกรม

�������������� �������������� ProceduralProcedural ������ObjectObject--OrientedOriented� ����%� ����������!� ! (Top-Down Development)

� ������� ���������49$ ���� (Modular Design)

� � �����,�!���() (Object-Oriented Programming)

Mr.Warawut Chapter 1 Review 16

Page 17: พื้นฐานการออกแบบโปรแกรม

�������� ��� ��������� ��������������� ��� ��������� ������������ ��������� ����

Control Modulestart

call module 1call module 2call module 3

stop

Mr.Warawut Chapter 1 Review 17

stop

Module 1begindo 1do 2do 3

return

Module 2begindo xdo ydo z

return

Module 3begin

if x then yelse zdo abcreturn

Page 18: พื้นฐานการออกแบบโปรแกรม

�������� ������������������� � !��"�������������� ������������������� � !��"������������#$����%& ������#$����%&

Mr.Warawut Chapter 1 Review 18

'�0�: http://www.chapterpiece.com/software-development-

process/2010/04/13/how-to-build-software-3/

Page 19: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 19

Page 20: พื้นฐานการออกแบบโปรแกรม

Objective of Structured Program Objective of Structured Program

Design TechniquesDesign Techniques1. ���!���� ������#)9:���$#���%�?�����'1!���! �������

� �$����8�����+�$��-�����!�0�����9�� !"

2. ����*�('�0�$����� ����)! ���������!���! %����#�

3. '1������������!����%� ������5����% !��$�� ��#��*$�����$! %�-��3. '1������������!����%� ������5����% !��$�� ��#��*$�����$! %�-��

4. ������������ ����

Mr.Warawut Chapter 1 Review 20

Page 21: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 21

Page 22: พื้นฐานการออกแบบโปรแกรม

Introduction to Algorithm and Introduction to Algorithm and

Pseudo CodePseudo CodeAlgorithm

� #�� ��) ���!�����������@��9A+'�0�$�1���*� �����23����

� #�� ����������� .-0!�$�,�����%� !�" ����'1�% !� �%�$�$������%,)��1�����5�������5����'�0,����� ��$���$���� ���0�����/�����(�����!�����������#� �>��5�������5����'�0,����� ��$���$���� ���0�����/�����(�����!�����������#� �>�$���8�����+'�0(�����!�����!�

Mr.Warawut Chapter 1 Review 22

Page 23: พื้นฐานการออกแบบโปรแกรม

Example Algorithm������������� �����������������

1. ������%��$���0�-0!*1�>���

2. �����1*$����������

3. �1�$���0�-0!*1�>����* �!��,�3. �1�$���0�-0!*1�>����* �!��,�

4. B��.�!�'�#�0�!�)!�!��,�

5. �1��1'�0���������'�!��,�

6. �C�D

7. ��$�9 3 �'�

Mr.Warawut Chapter 1 Review 23

Page 24: พื้นฐานการออกแบบโปรแกรม

������������ �������������������������������� ���!1. ������'-����!�� �1��� � �����#������! %

2. ������'-����!��#��(�����!��8�����+'�0�,�������23����9�� !"

3. �$��������������'�0$�)�����������'-����!��#����%�! % ���%!��� ���1����!����0��$���8���#��������+����1����!����0��$���8���#��������+���

4. ������'-����!���)�*���*)�

Mr.Warawut Chapter 1 Review 24

Page 25: พื้นฐานการออกแบบโปรแกรม

Example Algorithm���������������������������� �!��!��

turn on calculatorclear calculatorrepeat the following instructionsrepeat the following instructions

key in baht amountkey in decimal point (.)key in satang amountpress addition(+) key

until all prices have been enteredwrite down total priceturn off calculator

Mr.Warawut Chapter 1 Review 25

Page 26: พื้นฐานการออกแบบโปรแกรม

Introduction to Algorithm and Introduction to Algorithm and

Pseudo CodePseudo Code (Cont.)(Cont.)

Pseudo Code

� �1��,��'�������'-�

� �������+*�(�1������'-�'�0�1�*���������.� � #�������%���5�,)�#1*�0!:4 �������'��'�:4 �������'��'�

Mr.Warawut Chapter 1 Review 26

Page 27: พื้นฐานการออกแบบโปรแกรม

� ���"���� �#�$%&�&���1. (��%#1����$ %##1*�0! (Statement) ������%��%� ���������!

:4��!�?4�% !! %

2. ����-0!�'�� ������%��$ %##1*�0!���%!#1*�0!���%�

3. #��,�% ���������5��$ %,�+���0��%�#1�B�$ (Keywords) ����% !,����� 3. #��,�% ���������5��$ %,�+���0��%�#1�B�$ (Keywords) ����% !,����� ��(-!��� #!*�! �#��#)������5�*��* �� .�0!��$'1��!�� ��$'1���� �! %

4. �� �$�$ %##1*�0!������%��1��������!� ! �%�����%!'!���'!���%���$��'!���'!���%��' ����

5. ��) ���!�$ %##1*�0!� !" ���������) �������%������������! ���� �� ���!�1���,�0� ������� �������% ���0����*�(��%��,�!� �����������

Mr.Warawut Chapter 1 Review 27

Page 28: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 28

Page 29: พื้นฐานการออกแบบโปรแกรม

Six Basic Computer OperationsSix Basic Computer Operations1. #��������+*�(�����������

2. #��������+*�(�*�!8�����+���

3. #��������+*�(#1��9���

4. #��������+*�(�1���# ��������4. #��������+*�(�1���# ��������

5. #��������+*�(���%��'�%���$������'1!����

6. #��������+*�('1!�.�1" ���

Mr.Warawut Chapter 1 Review 29

Page 30: พื้นฐานการออกแบบโปรแกรม

����� ���!�����'������% (��� �)��9+�������� (����)�) �, � �'�+����� #�%+��+� ��������������� �

�=�+��������*�0������>���������� �, � �� �����������*�+ �� ����������'� ������������8 �'!��;�#�%+��+� ��5����

� ���� ��������$�,�#1��% read ��$ get ���0��,�������%�.� � #��� ���� ��������$�,�#1��% read ��$ get ���0��,�������%�.� � #��� read �,����0���������� ��#�+����=�+������

� get �,�*1���������������;�#�%+��+�

Mr.Warawut Chapter 1 Review 30

read studentNameget systemDateread number1, number2get taxCode

Page 31: พื้นฐานการออกแบบโปรแกรม

����� ���!�����'����) ��"!(��� ��*�!8�����+�$�,�#1��% print, write, put, output ��� display

� print �,�*1����* !8�����+���'!�#�0�!����+

� write �,�*1����* !���+�)����0���>����'-��!���=�+

� put, output ��� display �$�,�*1����* !���+�)�������*�!8�'!� put, output ��� display �$�,�*1����* !���+�)�������*�!8�'!��:�

Mr.Warawut Chapter 1 Review 31

print “Program Completed”write customer record to master fileput name, address and postcodeoutput totalTaxdisplay “End of data”

Page 32: พื้นฐานการออกแบบโปรแกรม

����� ���!�����'����) ��"!(�� (Cont.)� #1*�0! prompt '�0�,�*1����*�!���#��� ��'�0�$�,�#1*�0! get, ���0��$���*�(

*�0�* ��������%�*�.�+����% !*���9+%�0!�-��

promt for studentMarkget studentMark

Mr.Warawut Chapter 1 Review 32

get studentMark

Page 33: พื้นฐานการออกแบบโปรแกรม

����� ���!�����'�*����(��� *�3���49+'�0�,����#1��9

� *�3���49+ + �,��'����� (add)

� *�3���49+ – �,��'���� (subtract)

� *�3���49+ * �,��'��#�9 (multiply)� *�3���49+ * �,��'��#�9 (multiply)

� *�3���49+ / �,��'��� (divide)

� *�3���49+ ( ) �,��'��#�0�!��%�!��>���C�/�C�

� #1��%'�0�,����#1��9�$�,�#1� compute ��$ calculate

Mr.Warawut Chapter 1 Review 33

divide totalMarks by studentCountsalesTax costPrice * 0.10compute C = (F – 32) * 5 / 9

Page 34: พื้นฐานการออกแบบโปรแกรม

����� ���!�����'�*�����+�������(��*�('1��� 3 ����� ��!���

1. �,�#1��% initialize ���#1� set ���0��1���# ��0���������������

2. �,�*�3���49+�#�0�!��% = ����#�0�!��% � ���0��1���# �����������

3. �,�#1��% store ��������>������������������3. �,�#1��% store ��������>������������������

Mr.Warawut Chapter 1 Review 34

initialize totalPrice to zeroset studentCount to 0totalPrice = costPrice + salesTaxtotalPrice ���� costPrice + salesTaxstore customerId in lastCustomerID

Page 35: พื้นฐานการออกแบบโปรแกรม

����� ���!�����' ���#� ��#�� , -������*����(��� �,�#1 if…then…else ��$�����% end if �*��

if employeeStatus is partTime thenadd 1 to partTimeCount

else

Mr.Warawut Chapter 1 Review 35

elseadd 1 to fullTimeCount

end if

Page 36: พื้นฐานการออกแบบโปรแกรม

����� ���!�����'�*����$.*�/ (��� �,�#1 dowhile…enddo ��� repeat…until

dowhile file_flag <> “eof”read student recordprint studentName, address to report

Mr.Warawut Chapter 1 Review 36

print studentName, address to reportadd 1 to studentTotal

enddo

eof – End Of File

Page 37: พื้นฐานการออกแบบโปรแกรม

Mr.Warawut Chapter 1 Review 37

Page 38: พื้นฐานการออกแบบโปรแกรม

The Three Basic Control StructuresThe Three Basic Control Structures� Sequence

� Selection

� Repetition

Mr.Warawut Chapter 1 Review 38

Page 39: พื้นฐานการออกแบบโปรแกรม

Sequence Formatstatement a

statement b

statement c

::

:

Mr.Warawut Chapter 1 Review 39

Page 40: พื้นฐานการออกแบบโปรแกรม

Example: Sequenceadd 1 to pageCount

print heading line1

print heading line2

set lineCount to zeroset lineCount to zero

read customer record

Mr.Warawut Chapter 1 Review 40

Page 41: พื้นฐานการออกแบบโปรแกรม

Selection Formatif condition p is true then

statement(s) in true case

else

statement(s) in false casestatement(s) in false case

end if

Mr.Warawut Chapter 1 Review 41

Page 42: พื้นฐานการออกแบบโปรแกรม

Example: Simple IF Statementif accountBalance < 300000 then

serviceCharge = 5.00

else

serviceCharge = 2.00serviceCharge = 2.00

end if

Mr.Warawut Chapter 1 Review 42

Page 43: พื้นฐานการออกแบบโปรแกรม

Example: Null False Branchif employeeStatus = partTime then

add 1 to partTimeCount

end if

Mr.Warawut Chapter 1 Review 43

Page 44: พื้นฐานการออกแบบโปรแกรม

Example: Combined IF Statement 1if employeeStatus = partTime

and employeeGender = female then

add 1 to partTimeCount

end ifend if

Mr.Warawut Chapter 1 Review 44

Page 45: พื้นฐานการออกแบบโปรแกรม

Example: Combined IF Statement 2if (recordCode = “23” or updateCode = delete)

and accountBalance = zero then

delete customer record

end ifend if

Mr.Warawut Chapter 1 Review 45

Page 46: พื้นฐานการออกแบบโปรแกรม

Example: Combined IF Statement 3if not (recordCode = “23”

and updateCode = delete) then

update customer record

end ifend if

Mr.Warawut Chapter 1 Review 46

Page 47: พื้นฐานการออกแบบโปรแกรม

Example: Nested IF Statement 1if recordCode = “A” then

increment counter_Aelse

if recordCode = “B” thenincrement counter_Bincrement counter_B

elseif recordCode = “C” then

increment counter_Celse

increment errorCounterend if

end ifend if

Mr.Warawut Chapter 1 Review 47

Page 48: พื้นฐานการออกแบบโปรแกรม

Example: Nested IF Statement 2if employeeStatus = partTime then

if employeeGender = female thenif employeeAge > 30 then

add 1 to matureFemaleEmployeeelseelse

add 1 to youngFemaleEmployeeend if

elseadd 1 to maleEmployee

end ifelse

add 1 to fullTimeEmployeeend if

Mr.Warawut Chapter 1 Review 48

Page 49: พื้นฐานการออกแบบโปรแกรม

Repetition Formatdowhile

dowhile condition p is true

statement blockstatement block

enddo

Mr.Warawut Chapter 1 Review 49

Page 50: พื้นฐานการออกแบบโปรแกรม

Repetition Format (Cont.)repeat…until

repeat

statementstatement

statement

:

:

until condition is true

Mr.Warawut Chapter 1 Review 50

Page 51: พื้นฐานการออกแบบโปรแกรม

Example: dowhile loop 1set studentTotal to zero

dowhile studentTatal < 50

read student record

print studentName, address to reportprint studentName, address to report

add 1 to studentTotal

enddo

Mr.Warawut Chapter 1 Review 51

Page 52: พื้นฐานการออกแบบโปรแกรม

Example: dowhile loop 2Process Student_record

set studentCount to zero

read student record

dowhile studentNumber not equal 999dowhile studentNumber not equal 999

write student record

increment studentCount

read student record

enddo

print studentCount

end

Mr.Warawut Chapter 1 Review 52

Page 53: พื้นฐานการออกแบบโปรแกรม

Example: repeat…until loopProcess Student_record

set studentCount to zerorepeat

read student recordif studentNumber not equal 999if studentNumber not equal 999

write student recordincrement studentCount

end ifuntil studentNumber = 99print studentCount

end

Mr.Warawut Chapter 1 Review 53