Program Realisation 2 Course Organization http://www.win.tue.nl/˜hemerik/2IP20/ • 9 Lectures Lecture 1 • 6 Lab Sessions with compulsory weekly assignments (groups of 2) Kees Hemerik Tom Verhoe ff • 3 Lab Sessions with a final assignment (individual) Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica • No written exam Software Engineering Technology • Lecture notes, tutorials, handouts, assignments, programs, . . . Feedback to T.Verhoeff@TUE.NL � 2006, T. Verhoe ff @ TUE.NL c 1 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 2 Program Realization 2: Lecture 1 Today’s Topics An Easy Programming Problem: Windows 2001 Problem G, Prelims, Dutch Programming Championship NKP 1998 • Link with Program Realization 1 and Algorithm Design 1 Input: 1 200 3 3 • Refresher on Pascal 200 0 400 100 150 100 50 200 150 2 0 100 200 200 • Motivation for Abstract Data Types 2 100 1 ■ ❅ 50 50 ❅ ❅ 200 100 50 • Simple Graphical User Interfaces in Delphi Output: ■ ❅ ❅ ❅ Desktop 1 0 0 50 100 150 200 250 300 350 400 � 2006, T. Verhoe ff @ TUE.NL c 3 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 4 Program Realization 2: Lecture 1
A Hacked Solution (with a mistake) A Monolithic Solution (using an array) 1 program Windows2001m1; 2 { Monolithic version using an array to store the windows } 1 program Windows2001h; { Hacked version } 3 2 var r,n,m,x,y,j,k:integer; 4 var w: array [1..10000] of record xl,xh,yl,yh:integer end ; 3 5 r: Integer; { number of runs (input) } 4 begin i: Integer; { to count off the runs } 6 5 readln(r); 7 n: Integer; { number of windows (input) } 6 for r:=1 to r do begin readln(n); 8 w: array [1..10000] of record 7 for n:=1 to n do with w[n] do readln(xl,xh,yl,yh); xl, yl, xh, yh: Integer; { coordinates of a window } 9 readln(m); 8 10 end ; { w[1..n] = list of windows (input) } 9 for m:=1 to m do begin readln(x,y);k:=1;j:=n+1; j: Integer; { to count off the windows } 11 10 while k<>j do with w[k] do 12 m: Integer; { number of mouse clicks (input) } if (xl<=x) and (x<=xh) and (yl<=y) and (y<=yh) then j:=k 11 13 c: Integer; { to count off the mouse clicks } 12 else k:=k+1; x, y: Integer; { coordinates of a mouse click (input) } 14 if k<=n then writeln(k) else writeln(’Desktop’) 13 15 k: Integer; { window number (output) } 14 end end end . 16 17 begin 18 ReadLn(r) { number of runs } � 2006, T. Verhoe ff @ TUE.NL c 5 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 6 Program Realization 2: Lecture 1 A Monolithic Solution (using an array) A Monolithic Solution (using an array) 19 ; for i := 1 to r do begin 20 21 { Read and store the windows } 22 ReadLn(n) { number of windows } 37 else k := k + 1 ; for j := 1 to n do with w[j] do 23 38 { k = j, hence if k = n+1 then not found, 24 ReadLn(xl, yl, xh, yh) { coordinates of the window } otherwise w[k] is first window containing (x,y) } 39 25 40 26 { Read and process the mouse clicks } 41 { Output the result } 27 ; ReadLn(m) { number of mouse clicks } 42 ; if k = n + 1 then WriteLn(’Desktop’) ; for c := 1 to m do begin 28 43 else WriteLn(k) 29 ReadLn(x, y) { coordinates of the mouse click } end { for c } 44 30 45 31 { Find first window that contains (x,y) 46 end { for i } 32 using a Bounded Linear Search } 47 end . ; k := 1 ; j := n + 1 33 34 ; while k <> j do with w[k] do 35 if (xl <= x) and (x <= xh) and 36 (yl <= y) and (y <= yh) then j := k � 2006, T. Verhoe ff @ TUE.NL c 6 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 6 Program Realization 2: Lecture 1
Quality Assessment Why Modular Structure? • Comments : at top, variable declarations, statement groups • Correct construction • Variables : sensible names, single purpose • Construction by a team • Layout : indentation, whitespace, empty lines; begin ... end • Verification • Constants : explicitly named • Adaptation • Protection : check input for validity • Reuse • Modular structure : explicit � 2006, T. Verhoe ff @ TUE.NL c 7 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 8 Program Realization 2: Lecture 1 A Monolithic Solution (using a linked list) A Monolithic Solution (using a linked list) 1 program Windows2001m2; 19 c: Integer; { to count off the mouse clicks } { Monolithic version using a linked list to store the windows } x, y: Integer; { coordinates of a mouse click (input) } 2 20 3 21 k: Integer; { window number (output) } 4 type (*ADDED*) 22 NodeP = ˆNode; { pointer to a node } (*ADDED*) 5 23 begin 6 Node = record { node in linked list of windows } (*ADDED*) 24 ReadLn(r) { number of runs } 7 xl, yl, xh, yh: Integer; { coordinates of window } (*ADDED*) 25 ; for i := 1 to r do begin 8 tail: NodeP; { pointer to next window, if not nil } (*ADDED*) 26 9 end ; (*ADDED*) 27 { Read and store the windows } ReadLn(n) { number of windows } 10 28 11 var 29 ; w := nil ; u := nil (*ADDED*) 12 r: Integer; { number of runs (input) } 30 { inv: uˆ is last window in list w, if u <> nil } (*ADDED*) 13 i: Integer; { to count off the runs } 31 ; for j := 1 to n do begin (*CHANGED*) 14 n: Integer; { number of windows (input) } 32 New(v) { create new node } (*ADDED*) w: NodeP; { list of windows (input) } (*CHANGED*) ; with vˆ do begin (*ADDED*) 15 33 16 u, v: NodeP; { to construct and traverse list w } (*ADDED*) 34 ReadLn(xl, yl, xh, yh) { coordinates of window } 17 j: Integer; { to count off the windows } 35 ; tail := nil (*ADDED*) 18 m: Integer; { number of mouse clicks (input) } 36 end { with vˆ } (*ADDED*) � 2006, T. Verhoe ff @ TUE.NL c 9 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 9 Program Realization 2: Lecture 1
A Monolithic Solution (using a linked list) A Monolithic Solution (using a linked list) ; if u = nil then w := v else uˆ.tail := v (*ADDED*) 37 38 ; u := v ; v := nil (*ADDED*) 39 end { for j } (*ADDED*) 55 40 56 { Output the result } 41 { Read and process the mouse clicks } 57 ; if u = nil then WriteLn(’Desktop’) (*CHANGED*) ; ReadLn(m) { number of mouse clicks } 42 58 else WriteLn(k) 43 ; for c := 1 to m do begin end { for c } 59 44 ReadLn(x, y) { coordinates of the mouse click } 60 45 61 { Deallocate all windows } (*ADDED*) 46 { Find first window that contains (x,y) 62 ; while w <> nil do begin (*ADDED*) using a Bounded Linear Search } 47 63 v := wˆ.tail ; Dispose(w) ; w := v (*ADDED*) 48 ; u := w ; v := nil ; k := 1 (*CHANGED*) end { while } (*ADDED*) 64 49 ; while u <> v do with uˆ do (*CHANGED*) 65 if (xl <= x) and (x <= xh) and 50 66 end { for i } 51 (yl <= y) and (y <= yh) then v := u (*CHANGED*) 67 end . 52 else begin u := tail ; k := k + 1 end (*CHANGED*) 53 { u = v, hence if u = nil then not found, } (*CHANGED*) 54 { otherwise uˆ is first window containing (x,y) } (*CHANGED*) � 2006, T. Verhoe ff @ TUE.NL c 9 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 9 Program Realization 2: Lecture 1 Linked List with Pointers A Solution Structured with Routines NodeP = ˆNode; { pointer to a node } (*ADDED*) 1 program Windows2001s1; Node = record { node in linked list of windows } (*ADDED*) { Mildly structured version using an array to store the windows } 2 xl, yl, xh, yh: Integer; { coordinates of window } (*ADDED*) 3 tail: NodeP; { pointer to next window, if not nil } (*ADDED*) 4 const end ; (*ADDED*) MaxWindowListLength = 10000; { maximum length of a WindowList } 5 6 7 type ✲ ✲ w 200, 0, 400, 100 w 200, 0, 400, 100 8 Window = record 9 xl, yl, xh, yh: Integer; { lower-left and upper-right corners } r r { invariant: xl <= xh /\ yl <= yh } 10 ❄ ❄ 11 end ; ✲ u u 100, 50, 300, 150 100, 50, 300, 150 ❆ 12 ❆ ❆ 13 WindowList = record r r ❆ ❆ 14 length: 0..MaxWindowListLength; { number of windows in the list } ❄ ❆ ❆ ❄ item: array [1..MaxWindowListLength] of Window; 15 ❆ ✲ ✲ ❆ ❯ v v 0, 100, 200, 200 0, 100, 200, 200 16 { item[1..length] = list of windows } 17 end ; r r 18 ❄ ❄ � 2006, T. Verhoe ff @ TUE.NL c 10 Program Realization 2: Lecture 1 � 2006, T. Verhoe ff @ TUE.NL c 11 Program Realization 2: Lecture 1
Recommend
More recommend