Introduction To Diagrammatic Stepwise Refinement
Download
Report
Transcript Introduction To Diagrammatic Stepwise Refinement
Introduction
to
Diagrammatic
Stepwise Refinement
DIAGRAMMATIC STEPWISE REFINEMENT (DSR).
an approach to program design based on
The Program Structure Diagrams used in the
Jackson Method (JSP)
Wirth's Stepwise Refinement approach to program
design.
Steps in the DSR approach
1.
Define the problem completely.
2.
Work out what needs to be done to solve the problem.
3.
Using Stepwise Refinement, design a Program
Structure Diagram (PSD) to represent your solution to
the problem.
4.
Write out the Executable Operations needed to
produce the output from the input.
5.
Assign the Executable Operations to the appropriate
places in the PSD.
6.
Insert the Iteration and Selection conditions.
7.
Test your solution using test data.
8.
Translate the populated PSD into code.
Stepwise Refinement
Is an approach to program design which involves
breaking a task into sub-tasks.
Each sub-task is in turn broken into further sub-tasks.
This process continues until the sub-tasks are so
simple that their implementation is straight forward.
Program Structure Diagrams
Program Structure Diagrams (PSDs) are used to
represent the hierarchy of tasks and sub-tasks
created by Stepwise Refinement.
They are drawn using the three classic constructs
of Structured Programming.
Sequence
Selection
Iteration
Sequence PSD
PRINT
REPORT
PRINT
REPORT
HEADINGS
PRINT
REPORT
BODY
PRINT
REPORT
FOOTING
Selection PSD
COUNT
STUDENT
o
ADD TO
MALES
ADD TO
FEMALES
Selection with Null part
COUNT
STUDENT
o
ADD TO
LM51 COUNT
ADD TO
LM60 COUNT
Iteration PSD
PROCESS
ACCOUNTS
*
PROCESS
ACCOUNT
Iteration PSD - Explicit Repeat Loop
PROCESS
ACCOUNTS
PROCESS
FIRST
ACCOUNT
PROCESS
REMAINING
ACCOUNTS
*
PROCESS
ACCOUNT
Iteration PSD
PRINT
COUNTRY
BODY
*
INITIALIZE
COUNTY
TOTALS
PROCESS
COUNTY
RECORD
PRINT
COUNTY
TOTALS
Print-Country-Body is an iteration of three components.
This is equivalent to ;
an iteration of one component which is in turn a sequence
of three.
Iteration PSD
PRINT
COUNTRY
BODY
*
PRINT
COUNTY
REPORT
INITIALIZE
COUNTY
TOTALS
PROCESS
COUNTY
RECORD
PRINT
COUNTY
TOTALS
Payroll Proof Totals Program Specification
Write a program to produce proof totals for a weekly
payroll. The payroll file consists of records with the
following format:COL 1
COLS 2-6
COLS 7-11
Record-Type
1 = Regular
2 = Overtime
3 = Bonus
Employee-Number
Earnings
PIC 9.
PIC 9(5).
PIC 9(3)V99.
(in pounds and pence)
The print layout should be as follows:
PAYROLL PROOF TOTALS
Regular Earnings : 25
$2,111.23
Overtime Earnings : 2
$21.50
Bonus Earnings :
6
$123.45
Applying DSR to the Payroll Proof Totals Program
1. Define the problem completely
2. Work out what needs to be done to solve the
problem.
3. Using Stepwise Refinement design a Program
Structure Diagram (PSD) to represent your
solution to the problem.
Print
Proof Totals
Report
Print
Proof Totals
Report
Process
Payroll
Records
Print
Proof
Totals
Print
Proof Totals
Report
Process
Payroll
Records
*
Process A
Payroll
Record
Print
Proof
Totals
Print
Proof Totals
Report
Process
Payroll
Records
Print
Proof
Totals
*
Process A
Payroll
Record
o
Add To
Regular
Add To
Overtime
Add To
Bonus
Print
Proof Totals
Report
Process
Payroll
Records
Print
Proof
Totals
*
Process A
Payroll
Record
Print
Report
Headings
o
Add To
Regular
Add To
Overtime
Add To
Bonus
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
4. Write out the Executable Operations needed to
produce the output from the input.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
OPEN OUTPUT Print-File.
CLOSE Print-File.
WRITE Print-Line FROM Heading-Line AFTER ADVANCING PAGE.
WRITE Print-Line FROM Regular-Line AFTER ADVANCING 1 LINE.
WRITE Print-Line FROM Overtime-Line AFTER ADVANCING 1 LINE.
WRITE Print-Line FROM Bonus-Line AFTER ADVANCING 1 LINE.
MOVE Regular-Total TO Print-Reg-Total.
MOVE Overtime-Total TO Print-Overtime-Total.
MOVE Bonus-Total TO Print-Bonus-Total.
MOVE Regular-Count TO Print-Reg-Count.
MOVE Overtime-Count TO Print-Over-Count.
MOVE Bonus-Count TO Print-Bonus-Count.
ADD Earnings TO Regular-Total.
ADD Earnings TO Overtime-Total.
ADD Earnings TO Bonus-Total.
ADD 1 TO Regular-Count.
ADD 1 TO Overtime-Count.
ADD 1 TO Bonus-Count.
READ Payroll-File
AT END SET End-of-File TO TRUE
END-READ.
20. OPEN INPUT Payroll-File.
21. CLOSE Payroll-File.
5. Assign the Executable Operations to the appropriate
places in the PSD.
Algorithm
For all Executable Operations.
Get an Executable Operation.
Ask "To what component/components must I
attach this operation to get the program to
work?".
If there are already operations attached to a
target component then a further question must
be asked ; "Where, in relation to the existing
operations, does this operation go?".
Print
Proof Totals
Report
Process
Payroll
Records
Print
Proof
Totals
*
Process A
Payroll
Record
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
o
Add To
Regular
Add To
Overtime
Add To
Bonus
1. OPEN OUTPUT Print-File.
2. CLOSE Print-File.
Print
Bonus
Totals
Print
Proof Totals
Report
1
Process
Payroll
Records
Print
Proof
Totals
2
Print
Regular
Totals
Print
Overtime
Totals
*
Process A
Payroll
Record
Print
Report
Headings
Print
Bonus
Totals
o
Add To
Regular
Add To
Overtime
Add To
Bonus
1. OPEN OUTPUT Print-File.
2. CLOSE Print-File.
3. WRITE Print-Line FROM Heading-Line ...
4. WRITE Print-Line FROM Regular-Line ...
5. WRITE Print-Line FROM Overtime-Line ...
6. WRITE Print-Line FROM Bonus-Line ...
Print
Proof Totals
Report
1
Process
Payroll
Records
Print
Proof
Totals
2
*
Process A
Payroll
Record
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
4
5
6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
3.
4.
5.
6.
7.
8.
9.
WRITE Print-Line FROM Heading-Line ...
WRITE Print-Line FROM Regular-Line ...
WRITE Print-Line FROM Overtime-Line ...
WRITE Print-Line FROM Bonus-Line ...
MOVE Regular-Total TO Print-Reg-Total.
MOVE Overtime-Total TO Print-Overtime-Total.
MOVE Bonus-Total TO Print-Bonus-Total.
Print
Proof Totals
Report
1
Process
Payroll
Records
Print
Proof
Totals
2
*
Process A
Payroll
Record
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,4
8,5
9,6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
7.
8.
9.
10.
11.
12.
MOVE Regular-Total TO Print-Reg-Total.
MOVE Overtime-Total TO Print-Overtime-Total.
MOVE Bonus-Total TO Print-Bonus-Total.
MOVE Regular-Count TO Print-Reg-Count.
MOVE Overtime-Count TO Print-Over-Count.
MOVE Bonus-Count TO Print-Bonus-Count.
Print
Proof Totals
Report
1
Process
Payroll
Records
Print
Proof
Totals
2
*
Process A
Payroll
Record
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
10.
11.
12.
13.
14.
15.
16.
17.
18.
MOVE Regular-Count TO Print-Reg-Count.
MOVE Overtime-Count TO Print-Over-Count.
MOVE Bonus-Count TO Print-Bonus-Count.
ADD Earnings TO Regular-Total.
ADD Earnings TO Overtime-Total.
ADD Earnings TO Bonus-Total.
ADD 1 TO Regular-Count.
ADD 1 TO Overtime-Count.
ADD 1 TO Bonus-Count.
Print
Proof Totals
Report
1
Process
Payroll
Records
Print
Proof
Totals
2
*
Process A
Payroll
Record
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o
Add To
Regular
13,16
Add To
Overtime
Add To
Bonus
14,17
15,18
13.
14.
15.
16.
17.
18.
19.
ADD Earnings TO Regular-Total.
ADD Earnings TO Overtime-Total.
ADD Earnings TO Bonus-Total.
ADD 1 TO Regular-Count.
ADD 1 TO Overtime-Count.
ADD 1 TO Bonus-Count.
READ Payroll-File
AT END SET End-of-File TO TRUE
END-READ.
Print
Proof Totals
Report
1,19
Process
Payroll
Records
Print
Proof
Totals
2
*
Process A
Payroll
Record
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
19. READ Payroll-File
AT END SET End-of-File TO TRUE
END-READ.
Print
Proof Totals
Report
1,19
Process
Payroll
Records
Print
Proof
Totals
2
*
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
19. READ Payroll-File
AT END SET End-of-File TO TRUE
END-READ.
20. OPEN INPUT Payroll-File.
21. CLOSE Payroll-File.
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
Print
Proof
Totals
2,21
*
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
20. OPEN INPUT Payroll-File.
21. CLOSE Payroll-File.
6. Insert the Iteration and Selection conditions.
Go to each iteration component and ask;
"What iteration statement must I use here to get the
program to work correctly?".
Write the Iteration statement, give it a number and
insert its number into the diagram.
Go to each selection component and ask;
" What selection statement must I use here to get
the program to work correctly?".
Write the Selection statement, give it a number and
insert its number into the diagram.
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
1. PERFORM Process-Payroll-Records
UNTIL End-of-File.
Using IF...ELSE
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
1. PERFORM Process-Payroll-Records
UNTIL End-of-File.
2. IF Regular-Rec PERFORM Add-to-Regular
ELSE
IF Overtime-Rec PERFORM Add-to-Overtime
ELSE PERFORM Add-to-Bonus
END-IF
END-IF.
Using the Evaluate
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
1. PERFORM Process-Payroll-Records
UNTIL End-of-File.
2. EVALUATE TRUE
WHEN Regular-Rec PERFORM Add-to-Regular
WHEN Overtime-Rec PERFORM Add-to-Overtime
WHEN Bonus-Rec
PERFORM Add-to-Bonus
END-EVALUATE.
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
Employee-Rec
Type Emp-No Earnings
13,16
14,17
15,18
1
1234
050.25
000.00
000
000.00
000
000.00
000
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
Employee-Rec
Type Emp-No Earnings
13,16
14,17
15,18
1
1234
050.25
050.25
001
000.00
000
000.00
000
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
13,16
Add To
Overtime
14,17
Add To
Bonus
15,18
050.25
001
Employee-Rec
Type Emp-No Earnings
000.00
000
2
000.00
000
1234
025.50
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
13,16
Add To
Overtime
14,17
Add To
Bonus
15,18
050.25
001
Employee-Rec
Type Emp-No Earnings
025.50
001
2
000.00
000
1234
025.50
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
Employee-Rec
Type Emp-No Earnings
13,16
14,17
15,18
1
2345
120.25
050.25
001
025.50
001
000.00
000
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
Employee-Rec
Type Emp-No Earnings
13,16
14,17
15,18
1
2345
120.25
170.50
002
025.50
001
000.00
000
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
Employee-Rec
Type Emp-No Earnings
13,16
14,17
15,18
3
1234
010.50
170.50
002
025.50
001
000.00
000
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
Print
Proof
Totals
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
Employee-Rec
Type Emp-No Earnings
13,16
14,17
15,18
3
1234
010.50
170.50
002
025.50
001
010.50
001
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
13,16
Add To
Overtime
14,17
Add To
Bonus
15,18
170.50
002
Employee-Rec
Type Emp-No Earnings
025.50
001
HIGH-VALUES
010.50
001
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
13,16
Add To
Overtime
14,17
Add To
Bonus
15,18
170.50
002
Employee-Rec
Type Emp-No Earnings
025.50
001
HIGH-VALUES
010.50
001
7. Test the program using test data.
Type
1
2
1
3
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
Emp-No
1234
1234
2345
1234
Earnings
050.25
025.50
120.25
010.50
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
13,16
Add To
Overtime
14,17
Add To
Bonus
15,18
170.50
002
Employee-Rec
Type Emp-No Earnings
025.50
001
HIGH-VALUES
010.50
001
8. Translate the populated PSD into COBOL code
Start at the top of the diagram.
At each parent component write the sequence of
executable operations, child components and
iteration and selection conditions as statements in a
paragraph.
Replace child components with PERFORMs of the
component name.
At the top component, use the name as the name of
the first paragraph and preceed it with the
PROCEDURE DIVISION header.
End the sequence with the STOP RUN statement.
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
PROCEDURE DIVISION.
Print-Proof-Totals-Report.
OPEN INPUT Payroll-File.
OPEN Output Print-File.
Read Payroll-File ....
PERFORM Process-Payroll-Records
UNTIL End-Of-File.
PERFORM Print-Proof-Totals.
CLOSE Print-File.
CLOSE Payroll-File.
STOP RUN.
Print
Proof Totals
Report
1,20,19
Process
Payroll
Records
*
Print
Proof
Totals
2,21
1
Process A
Payroll
Record
19
Print
Report
Headings
Print
Regular
Totals
Print
Overtime
Totals
Print
Bonus
Totals
3
7,10,4
11,8,5
12,9,6
o 2
Add To
Regular
Add To
Overtime
Add To
Bonus
13,16
14,17
15,18
Process-Payroll-Records.
EVALUATE TRUE
WHEN Regular-Rec PERFORM Add-to-Regular
WHEN Overtime-Rec PERFORM Add-to-Overtime
WHEN Bonus-Rec
PERFORM Add-to-Bonus
END-EVALUATE.
READ Payroll-File
AT END SET End-Of-File TO TRUE
END-READ.