Post on 30-May-2018
8/14/2019 SE Code Restructuring Solutions
1/47
Date: 27.10.2007 SE_CodeRestructuring_Solutions.ppt Page: 1
Reverse Engineering
Code Restructuring
Guest Lecture
SolutionsSolutions
October 2007 3rd VersionOctober 2007 3rd Version
presented bypresented by
Dipl. Ing. Werner HoffmannDipl. Ing. Werner HoffmannEMAIL:EMAIL: pwhoffmann@t-online.depwhoffmann@t-online.de
See:See: Copyright Note in Note Page. Copyright Note in Note Page.
A member of IEEE and ACM
Mainframe
Please see thenotes pages for
additionalcomments.
ETHEidgenssische Technische Hochschule ZrichSwiss Federal Institute of Technology Zrich
mailto:pwhoffmann@t-online.demailto:pwhoffmann@t-online.demailto:pwhoffmann@t-online.de8/14/2019 SE Code Restructuring Solutions
2/47
Code Restructuring - Exercises Page: 2Date: 21.06.2007
Agenda - Exercises
1. Basic Methods
2. Restructuring
3. Some more complex examples
8/14/2019 SE Code Restructuring Solutions
3/47
Code Restructuring - Exercises Page: 3Date: 21.06.2007
Agenda - Exercises
Basic Methods Restructuring
Some more complex examples
8/14/2019 SE Code Restructuring Solutions
4/47
Code Restructuring - Exercises Page: 4Date: 21.06.2007
Exercise: Basic Methods (1)
A) Name the method(s) which are used for source code restructuring inthe following example. Additional name the resulting structural controlblocks.
F2
2 p2
FT
F11 p1
F
T
3
?
3
?
REPEAT F1 U(p1)
DoW(p2) F2Method:
Substitution
=
8/14/2019 SE Code Restructuring Solutions
5/47
Code Restructuring - Exercises Page: 5Date: 21.06.2007
Exercise: Basic Methods (2-1)
B) Name the method(s) which are used for source code restructuring inthe following example. Name additional methods to get structuralcontrol blocks.
=
Method:
Interchange
Resolution
Transposition - KCombination J,K
Combination J,K
1
p1F
T
J
K
2 3
L
1
2
J
2 p1F
TK
1 p1
FT
3
J
K
4
L
1
2
8/14/2019 SE Code Restructuring Solutions
6/47
Code Restructuring - Exercises Page: 6Date: 21.06.2007
Exercise: Basic Methods (2-2)
B) Name the method(s) which are used for source code restructuring inthe following example. Name additional methods to get structuralcontrol blocks.
=
J,K
2 p1F
T
1 p1F
T
3
J,K
K
4
L
K
3 2 p1F
TJ,K
4
L
p1
Method:
Transposition
SubstitutionDOW(p1) {J,K}
8/14/2019 SE Code Restructuring Solutions
7/47Code Restructuring - Exercises Page: 7Date: 21.06.2007
Exercise: Basic Methods (3)
C) Name the method(s) which are used for source code restructuring inthe following example.
=
Method:
Transposition
1 H2 1 2
HH
H
H
8/14/2019 SE Code Restructuring Solutions
8/47
Code Restructuring - Exercises Page: 8Date: 21.06.2007
Exercise: Basic Methods (4-1)
D) Name the method(s) which are used for source code restructuring inthe following example. Name additional methods to get basic structuralcontrol blocks.
=
Method:
Combination
p1F
T
F,G,E
1 HIp1F
T
G
E
1 HI
F
8/14/2019 SE Code Restructuring Solutions
9/47
8/14/2019 SE Code Restructuring Solutions
10/47
Code Restructuring - Exercises Page: 10Date: 21.06.2007
Exercise: Basic Methods (5)
E) Name the method(s) which are used for source code restructuring inthe following example.
=
Method: Interchange
B 1
A
C 2
D 3
B
C
1
D
2 3
A
8/14/2019 SE Code Restructuring Solutions
11/47
Code Restructuring - Exercises Page: 11Date: 21.06.2007
Exercise: Basic Methods (6)
F) Name the method(s) which are used for source code restructuring inthe following example.
Method: Inverse Notation
=
A
1 p
F
T
A
1
T
F
p
=A1 q
T
F
A1
F
T
q
Means: DO W(p) A
Means: REPEAT AU(q)
8/14/2019 SE Code Restructuring Solutions
12/47
Code Restructuring - Exercises Page: 12Date: 21.06.2007
Agenda - Exercises
Basic Methods
Restructuring Some more complex examples
8/14/2019 SE Code Restructuring Solutions
13/47
Code Restructuring - Exercises Page: 13Date: 21.06.2007
Exercise: Restructuring (1)
A) Restructure following flowchart and describe used methods/rules:
A
B1 p
T
F
AB
1 p
T
FB
A,B
1 p
T
FB
Step 1: Transposition B
Step 2: Combination A,B
Step 3: Pseudo Code BDoW(p) {A,B}
8/14/2019 SE Code Restructuring Solutions
14/47
Code Restructuring - Exercises Page: 14Date: 21.06.2007
Exercise: Restructuring (2)
B) Restructure following flowchart and describe used methods/rules:
B1
q
T
Fp
T
F
A
2 B
1
q
T
F
p
T
F
A
2
B
B
1q
T
F
p
T
F
A
2
B
Step 1: Transposition B
Step 2: Interchange
Step 3: Pseudo CodeIF pTHEN IF q
THEN AELSE B
ELSE B
te: Another method can be used: Resolution
8/14/2019 SE Code Restructuring Solutions
15/47
Code Restructuring - Exercises Page: 15Date: 21.06.2007
Exercise: Restructuring (3)
C) Restructure following flowchart and describe used methods/rules:
B1
qT
F
p
T
F
A
2
B 1
qT
F
p
T
F
A
2
B
B
1 q
T
Fp
T
F
A
2
B
Step 1: Transposition B
tep 2: Interchange Step 3: Inverse Notation
Step 4: Pseudo CodeIF p
THEN AELSE B
DoW(q) B
8/14/2019 SE Code Restructuring Solutions
16/47
Code Restructuring - Exercises Page: 16Date: 21.06.2007
Exercise: Restructuring (3-1)
D) Restructure following flowchart and describe used methods/rules:
F2
p1T
F
p2
TF
F1 21
F3
F2,F1
p1
T
Fp2
TF
F1 21
F3
DoW( p1) {F2,F1}
First Solution:
Step 1: Transposition BStep 2: Interchange 1,2Step 3: Combination F2,F1
Step 4: Inverse Notation p1Step 5: Substitution inner DO_WHILESee next foil!
8/14/2019 SE Code Restructuring Solutions
17/47
Code Restructuring - Exercises Page: 17Date: 21.06.2007
Exercise: Restructuring (3-2)
D) Restructure following flowchart and describe used methods/rules:
F2
p1T
F
p2
TF
F1 21
F3
p2
TF
F1,DoW ( p1) {F2,F1} 1
F3,DoW ( p1){F2,F1}
First Solution:
Step 6: Transposition DoWStep 7: Combination F1,
Combination F3,
Continue:
Step 8: Pseudo CodeF1DoW( p1) {F2,F1}DoW(p2) {F3, DoW( p1) {F2,F1}}
8/14/2019 SE Code Restructuring Solutions
18/47
Code Restructuring - Exercises Page: 18Date: 21.06.2007
Exercise: Restructuring (3-3)
D) Restructure following flowchart and describe used methods/rules:
F2
p1T
F
p2
T
FF1 21
F3
F2,F1
p1
T
F
p2
TF
F1 21
F3
TF1 1 p1
F
p2
T
F2,F1
T
F3
O
B
B
3
4F
?O
F
Second Solution:
Step 1: Transposition F1Step 2: Interchange 1,2Step 3: Combination F2,F1
Step 4: Resolution and Cleanup
Step 5: Add control-functionB - means Back-switchO means go_out-switch?O means test for go_out
See next foil!
8/14/2019 SE Code Restructuring Solutions
19/47
Code Restructuring - Exercises Page: 19Date: 21.06.2007
Exercise: Restructuring (3-4)
D) Restructure following flowchart and describe used methods/rules:
F2
p1T
F
p2
T
FF1 21
F3
F1 1 p1T
F
p2
T
F2,F1
T
F3
O
B
B
3
4F
?O
F
Second Solution: Step 6: Pseudo Code
F1REPEATIF p1
THEN IF p2THEN {F3, B}ELSE O
ELSE {F2,F1,B}U(?O)
8/14/2019 SE Code Restructuring Solutions
20/47
Code Restructuring - Exercises Page: 20Date: 21.06.2007
Exercise: Restructuring (3-4)
D) Restructure following flowchart and describe used methods/rules:
Third Solution:
Step 6: Pseudo Code{F1,TRUE}REPEAT
POPIF p1
THEN IF p2THEN {F3, FALSE}ELSE TRUE
ELSE {F2,F1,FALSE}
U(TOP)
F1 1 p1T
F
p2
T
F2,F1
T
F3
3
4F
TOP
F
TRUE TRUE
FALSE
POP
FALSE
8/14/2019 SE Code Restructuring Solutions
21/47
Code Restructuring - Exercises Page: 21Date: 21.06.2007
Agenda - Exercises
Basic Methods
Restructuring
Some more complexexamples
8/14/2019 SE Code Restructuring Solutions
22/47
Code Restructuring - Exercises Page: 22Date: 21.06.2007
Exercise: Example 1 (1)
A) Specification:
A bank offers two mortgages. The first is for $55,000 at a monthlyrate 0f 0.6 percent. The second is for $26,000 at a monthly rate of0.9 percent.If the payment is $1,500 per month,
How long will it take to pay off both mortgages if themonthly payments reduce the first mortgages first?
How much will be the last payment be?
Use following Pseudo-Code as starting point for a code restructuretask.
After restructuring analyze and discuss the result.
8/14/2019 SE Code Restructuring Solutions
23/47
Code Restructuring - Exercises Page: 23Date: 21.06.2007
Exercise: Example 1 (2)
A) Pseudo-Code:L1 = 55000 *loan 1
I1 = 0.006 *interest 1
L2 = 26000 *loan 2
I2 = 0.009 *interest 2
Pay = 1500 *monthly payment
M = 0 *month numberNext_Month:
M = M + 1
p = Pay (L1*I1) (L2*I2)
IF p
8/14/2019 SE Code Restructuring Solutions
24/47
Code Restructuring - Exercises Page: 24Date: 21.06.2007
Exercise: Example 1 (3)
Draw Flowchart:
p1
T
FF1 1 F2 p2 p3
F3
F4
p1 2 p4
3
F6
5
4
F7
T
F5
T
FT
F FT F
8/14/2019 SE Code Restructuring Solutions
25/47
Code Restructuring - Exercises Page: 25Date: 21.06.2007
Exercise: Example 1 (4)
Restructure Flowchart:
Methods/Rules used: Resolution Combination Inverse Notation Rule 3: Indecomposable program figures
p1
T
F
F1,TRUE 1.1
F3
F4POP,TRUE
POP,F21.2
FALSE
TRUE,F7
TRUE
FALSE
p2 p3 p1
TRUE
TOP
POP
TOP p4
F6,POP,TRUE
TOP POP
F
T
T
F
T
F
F
T
T
FF
T
F
T
F5
8/14/2019 SE Code Restructuring Solutions
26/47
Code Restructuring - Exercises Page: 26Date: 21.06.2007
Exercise: Example 1 (5)
Pseudo-Code: {F1,TRUE}REPEAT
{POP,TRUE}
REPEAT
{POP,F2}
IF p1
THEN {TRUE,F7}ELSE
FALSE
IF p2
THEN TRUEELSE {IF p3
THEN F3
ELSE F4
ENDIF
IF p1
THEN TRUE
ELSE FALSEENDIF}
ENDIF
ENDIF
UNTIL(TOP)
POP
IF TOP
THEN
ELSE IF p4
THEN {F6,POP,TRUE}
ELSE F5ENDIF
ENDIF
UNTIL(TOP)
POP
8/14/2019 SE Code Restructuring Solutions
27/47
Code Restructuring - Exercises Page: 27Date: 21.06.2007
Exercise: Example 1 (6)
Pseudo-Code(Analyze/Discussion):
{L1=55000;I1=0006;L2=26000;I2=0.009;Pay=1500;M=0;TRUE}
REPEAT
{POP;TRUE}
REPEAT
{POP;M=M+1;p=Pay-(L1*I1)-(L2*I2)}
IF p
8/14/2019 SE Code Restructuring Solutions
28/47
Code Restructuring - Exercises Page: 28Date: 21.06.2007
Exercise: Example 1 (7)
A) Pseudo-Code (after re-engineering):L1 = 55000 *loan 1
I1 = 0.006 *interest 1
L2 = 26000 *loan 2
I2 = 0.009 *interest 2
Pay = 1500 *monthly payment
M = 0 *month number
P = PAY (L1*I1) (L2*I2)
If p 0
M = M+1
P = PAY (L1*I1) L2*I2)
P = RedMort(L1,P)P = RedMort(L2,P)
End_While
Print No.month= m left= p
RedMort: Proc(L,P) Returns();* Reduce Mortgage
* Calculate remaining
* Mortgages and left amount.D = L P
IF D
8/14/2019 SE Code Restructuring Solutions
29/47
Code Restructuring - Exercises Page: 29Date: 21.06.2007
Exercise: Example 2 (1)
A) An old Batch-Program: Task: Restructure thisflowchart and writeresulting Pseudo-Code.
START
Initialization_Routine
(1)
Jump to new page
Print Header Lines(2)
Read Order Record OIf EOF Y- Termination_Routine; END
(3)Read Address Record A
If key O = key A N-
If O.Count >= 0 N- Prepare Print_Line (O,A fields)If O.Price >= 0 N- Print Line
Calculate Sum (O.Count*O.Price) Clear Sum
Prepare Sum in Print_Line If Page_overflow N-
-Y-
8/14/2019 SE Code Restructuring Solutions
30/47
Code Restructuring - Exercises Page: 30Date: 21.06.2007
Exercise: Example 2 (2)
A) An old Batch-Program(Solution):
START
Initialization_Routine
(1)
Jump to new page
Print Header Lines(2)
Read Order Record OIf EOF Y- Termination_Routine; END
(3)Read Address Record A
If key O = key A N-
If O.Count >= 0 N- Prepare Print_Line (O,A fields)If O.Price >= 0 N- Print Line
Calc. Sum (O.Count*O.Price) Clear Sum
Prepare Sum in Print_Line If Page_overflow N-
-Y-
F6
F1
F2
F3
REPEAT F4 U(p1)F4p1
p2p3
F5p4
F7
8/14/2019 SE Code Restructuring Solutions
31/47
Code Restructuring - Exercises Page: 31Date: 21.06.2007
Exercise: Example 2 (3)
A) An old Batch-Program (Solution):
EOFF1 F2 F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p4
21
T
F
FT
8/14/2019 SE Code Restructuring Solutions
32/47
Code Restructuring - Exercises Page: 32Date: 21.06.2007
Exercise: Example 2 (4)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
EOFF1 F2 F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p4
21
T
F
FT
8/14/2019 SE Code Restructuring Solutions
33/47
Code Restructuring - Exercises Page: 33Date: 21.06.2007
Exercise: Example 2 (5)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
EOFF1 F2 F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p4
21
T
F
FT
Methods/Rules used: Transposition F2 Combination
8/14/2019 SE Code Restructuring Solutions
34/47
Code Restructuring - Exercises Page: 34Date: 21.06.2007
Exercise: Example 2 (6)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
EOF
F1
,F2
F2
F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p4
21
T
F
FT
8/14/2019 SE Code Restructuring Solutions
35/47
Code Restructuring - Exercises Page: 35Date: 21.06.2007
Exercise: Example 2 (7)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
EOFF1,F2,F3
F2,F3
F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p42
1
T
F
FT
Methods/Rules used: Transposition F3 Combination interchange
8/14/2019 SE Code Restructuring Solutions
36/47
Code Restructuring - Exercises Page: 36Date: 21.06.2007
Exercise: Example 2 (8)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
EOFF1,F2,F3
F2
F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p42
1
T
F
FT
Methods/Rules used: Transposition F3
8/14/2019 SE Code Restructuring Solutions
37/47
Code Restructuring - Exercises Page: 37Date: 21.06.2007
Exercise: Example 2 (9)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
EOFF1,F2,F3
F2
F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p42
1
T
F
FT
8/14/2019 SE Code Restructuring Solutions
38/47
Code Restructuring - Exercises Page: 38Date: 21.06.2007
Exercise: Example 2 (10)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
p5F1,F2,F3
F2
F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p42
1
T
F
TF
Methods/Rules used: Inverse Notation:
p5 = EOF = more data
8/14/2019 SE Code Restructuring Solutions
39/47
Code Restructuring - Exercises Page: 39Date: 21.06.2007
Exercise: Example 2 (11)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
p5F1,F2,F3
F2
F3
R F4U(p1)
F7
p2
T
F
p3
T
F
F5
4
3F6
p42
1
T
F
TF
p5 = EOF = more data
8/14/2019 SE Code Restructuring Solutions
40/47
Code Restructuring - Exercises Page: 40Date: 21.06.2007
Exercise: Example 2 (12)
A) An old Batch-Program (Solution) Spaghetti Bowl Approach:
F1
F2
F3
DoWHILE (more_data)
REPEAT
F4
UNITL(p1)
IF p2
THEN
IF p3
THEN F5
(ELSE)
ENDIF
(ELSE)
ENDIF
F6IF p4
THEN F2
(ELSE)ENDIF
F3
EndWHILE
F7
START
Initialization-Routine
Jump to new Page
Print Header Lines
Read Order Record ODoWHILE (more_data)
REPEAT
Read Address record A
UNITL(key O = key A)IF O.Count >= 0
THEN
IF O.Price >= 0
THEN
Calc. Sum (O.Count*O.Price)
Prepare Sum into Print-line
ENDIFENDIF
Prepare Print-Line (O,A fields)
Print Line
Clear Sum
IF Page Overflow
THEN
Jump to new page
Print Header Lines
ENDIF
Read Order Record O
EndWHILE
Termination Routine
END
ProcessOrderFile
ProcessoneOrderRecord
8/14/2019 SE Code Restructuring Solutions
41/47
Code Restructuring - Exercises Page: 41Date: 21.06.2007
Exercise: Example 3 (1)
College Admission Program:Task: Restructure thisflowchart and writeresulting Pseudo-Code.
8/14/2019 SE Code Restructuring Solutions
42/47
Code Restructuring - Exercises Page: 42Date: 21.06.2007
Exercise: Example 3 (2)
College Admission Program(Solution):
8/14/2019 SE Code Restructuring Solutions
43/47
Code Restructuring - Exercises Page: 43Date: 21.06.2007
Exercise: Example 4 (1)
A) A typical commercial program:
Task: Restructure thisflowchart and writeresulting Pseudo-Code.
Describe used methods/rules!
8/14/2019 SE Code Restructuring Solutions
44/47
Code Restructuring - Exercises Page: 44Date: 21.06.2007
Exercise: Example 4 (2)
A) A typical commercialprogram (solution):
w
8/14/2019 SE Code Restructuring Solutions
45/47
Date: 27.10.2007 SE_CodeRestructuring_Solutions.ppt Page: 45
Questions / CommentsQuestions / Comments
??????Questions, comments, further
information?
Please feel free to e-mail me!Dipl.Ing. Werner HoffmannDipl.Ing. Werner Hoffmann
EMAIL:EMAIL: pwhoffmann@t-online.depwhoffmann@t-online.deoror
pwhoffmann@aol.compwhoffmann@aol.com
mailto:pwhoffmann@t-online.demailto:pwhoffmann@t-online.demailto:pwhoffmann@t-online.de8/14/2019 SE Code Restructuring Solutions
46/47
Code Restructuring - Exercises Page: 46Date: 21.06.2007
Do not
8/14/2019 SE Code Restructuring Solutions
47/47
The End
Code Restructuring
Ithankyouforyourattention!