Upload
warawut
View
878
Download
0
Embed Size (px)
DESCRIPTION
พื้นฐานการออกแบบโปรแกรม (Fundamental of Program Design)
Citation preview
Mr.Warawut KhangkhanMr.Warawut KhangkhanTwitter: http://twitter.com/awarawut
Facebook: http://www.facebook.com/AjWarawutE-mail: [email protected]
Mobile: 083-0698-410
ObjectiveObjective1. ��������������� �������
2. �������������������������������� ������������ !"
3. ���#������ !$�� !����%� ������� Procedural ��$Object-Oriented ���Object-Oriented ���
4. '����()�$*!#+��!�'#��#������� �����,�! #!*�!
5. ���#����%��!������'-���$.� � #�����
6. ����������/������������ 6 �$���!#��������+ ���0��1��*� ����%�.� � #������% !(����������
Mr.Warawut 2Chapter 1 Review
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
Mr.Warawut Chapter 1 Review 4
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
Define the Program
� Inputs
� Outputs
� Processing
Mr.Warawut Chapter 1 Review 6
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
Develop and Algorithm� �������'�0�,�����%�1����'1!� ��$������/����������������!������'-�'�0
����
� .� � #�� (Pseudo Code) ��5�����'�������'-����0��,�������23�'!#��������+#��������+
Mr.Warawut Chapter 1 Review 8
Test the Algorithm for Correctness� ��5��������'�0*1#�3'�0*)�
� ���*��'��! ��$��!������'-� ��������� ��$��1������'�*��������$���8����� �$�������
Mr.Warawut Chapter 1 Review 9
Programming� �1������'-�'�0�������������% !*���9+��������%����%� ����
(,)�#1*�0!)
� ������,�:4$���*�! (High Language) ���0����%� ���� �, � C, PASCAL ��5����PASCAL ��5����
Mr.Warawut Chapter 1 Review 10
Testing� �1�������;����������0�'�*�����#�0�!��� ����'�0������%��-��� (�����!�����
� ����*��� �����,)�#1*�0! (Syntax Errors)
� ���� (Logic Errors)� ���� (Logic Errors)
� ������'�*�����!��#������ !������������ �$*(��9+��$*:��������
Mr.Warawut Chapter 1 Review 11
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
Mr.Warawut Chapter 1 Review 13
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
Mr.Warawut Chapter 1 Review 15
�������������� �������������� ProceduralProcedural ������ObjectObject--OrientedOriented� ����%� ����������!� ! (Top-Down Development)
� ������� ���������49$ ���� (Modular Design)
� � �����,�!���() (Object-Oriented Programming)
Mr.Warawut Chapter 1 Review 16
�������� ��� ��������� ��������������� ��� ��������� ������������ ��������� ����
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
�������� ������������������� � !��"�������������� ������������������� � !��"������������#$����%& ������#$����%&
Mr.Warawut Chapter 1 Review 18
'�0�: http://www.chapterpiece.com/software-development-
process/2010/04/13/how-to-build-software-3/
Mr.Warawut Chapter 1 Review 19
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
Mr.Warawut Chapter 1 Review 21
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
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
������������ �������������������������������� ���!1. ������'-����!�� �1��� � �����#������! %
2. ������'-����!��#��(�����!��8�����+'�0�,�������23����9�� !"
3. �$��������������'�0$�)�����������'-����!��#����%�! % ���%!��� ���1����!����0��$���8���#��������+����1����!����0��$���8���#��������+���
4. ������'-����!���)�*���*)�
Mr.Warawut Chapter 1 Review 24
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
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
� ���"���� �#�$%&�&���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
Mr.Warawut Chapter 1 Review 28
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
����� ���!�����'������% (��� �)��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
����� ���!�����'����) ��"!(��� ��*�!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”
����� ���!�����'����) ��"!(�� (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
����� ���!�����'�*����(��� *�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
����� ���!�����'�*�����+�������(��*�('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
����� ���!�����' ���#� ��#�� , -������*����(��� �,�#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
����� ���!�����'�*����$.*�/ (��� �,�#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
Mr.Warawut Chapter 1 Review 37
The Three Basic Control StructuresThe Three Basic Control Structures� Sequence
� Selection
� Repetition
Mr.Warawut Chapter 1 Review 38
Sequence Formatstatement a
statement b
statement c
::
:
Mr.Warawut Chapter 1 Review 39
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
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
Example: Simple IF Statementif accountBalance < 300000 then
serviceCharge = 5.00
else
serviceCharge = 2.00serviceCharge = 2.00
end if
Mr.Warawut Chapter 1 Review 42
Example: Null False Branchif employeeStatus = partTime then
add 1 to partTimeCount
end if
Mr.Warawut Chapter 1 Review 43
Example: Combined IF Statement 1if employeeStatus = partTime
and employeeGender = female then
add 1 to partTimeCount
end ifend if
Mr.Warawut Chapter 1 Review 44
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
Example: Combined IF Statement 3if not (recordCode = “23”
and updateCode = delete) then
update customer record
end ifend if
Mr.Warawut Chapter 1 Review 46
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
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
Repetition Formatdowhile
dowhile condition p is true
statement blockstatement block
enddo
Mr.Warawut Chapter 1 Review 49
Repetition Format (Cont.)repeat…until
repeat
statementstatement
statement
:
:
until condition is true
Mr.Warawut Chapter 1 Review 50
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
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
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