Solutions TOPIC O: Pointers.
Contents :
Pointers.
Exercises :
D1: .
S1
Exercise
S2
Exercise
S3
<* M2EXTENSIONS + *><*
STORAGE + *>
MODULE S3;
FROM IO IMPORT WrStr, WrLn, RdKey, WrCard, RdLn, WrChar;
TYPE
TreeRcPt = POINTER TO TreeRc;
TreeRc = RECORD
ch:CHAR;
left, right: TreeRcPt;
END;
PROCEDURE CreateInitialTree(): TreeRcPt;
VAR
root, newRc, rightTree: TreeRcPt;
BEGIN
NEW(newRc);
newRc^.ch := 'a';
newRc^.left := NIL;
newRc^.right := NIL;
root := newRc;
NEW(newRc);
newRc^.ch := 'c';
newRc^.left := root;
newRc^.right := NIL;
root := newRc;
NEW(newRc);
newRc^.ch := 'd';
newRc^.left := NIL;
newRc^.right := NIL;
root^.right := newRc;
NEW(newRc);
newRc^.ch := 'g';
newRc^.left := root;
newRc^.right := NIL;
root := newRc;
NEW(rightTree);
rightTree^.ch := 'i';
rightTree^.left := NIL;
rightTree^.right := NIL;
NEW(newRc);
newRc^.ch := 'q';
newRc^.left := rightTree;
newRc^.right := NIL;
rightTree := newRc;
NEW(newRc);
newRc^.ch := 'z';
newRc^.left := NIL;
newRc^.right := NIL;
rightTree^.right := newRc;
root^.right := rightTree;
RETURN root;
END CreateInitialTree;
VAR
root: TreeRcPt;
BEGIN
(* initiele tree maken *)
root:= CreateInitialTree();
END S3.
Exercise
S4
<* M2EXTENSIONS + *>
<* STORAGE + *>
MODULE S4;
FROM IO IMPORT WrStr, WrLn, RdKey, WrCard, RdLn;
FROM Lib IMPORT Delay; (* Delay: let the
program wait a little time *)
(* de graphics library vind je
onder
C:/Bin/Xds/Def/Ts/Graph.def *)
FROM Graph IMPORT Init, Rectangle,
Circle;
FROM Graph IMPORT _clrBLACK, _clrBLUE,
_clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA;
FROM Graph IMPORT _clrBROWN, _clrWHITE,
_clrGRAY, _clrLIGHTBLUE, _clrLIGHTGREEN;
FROM Graph IMPORT _clrLIGHTCYAN,
_clrLIGHTRED,
_clrLIGHTMAGENTA, _clrLIGHTYELLOW,
_clrBRIGHTWHITE;
(* deze 16 kleuren zijn constantes gaande van 0 tot 15 *)
CONST SCREEN_SIZE = 400;
NBR = 10;
SIZE
= 20;
DELAY_TIME
= 100;
TYPE
FigureRcPt = POINTER TO
FigureRc;
FigureRc = RECORD
type: (RECTANGLE, CIRCLE);
x0,
y0: CARDINAL;
size:
CARDINAL;
color:CARDINAL;
next:
FigureRcPt;
END;
PROCEDURE PlotFigureList(figureList:
FigureRcPt);
VAR
figure:
FigureRcPt;
BEGIN
figure := figureList;
WHILE (figure # NIL) DO
(* Plot figure
*)
CASE
figure^.type
OF
RECTANGLE:
Rectangle(figure^.x0,
figure^.y0, figure^.x0 + figure^.size, figure^.y0 + figure^.size,
figure^.color,
FALSE);
| CIRCLE:
Circle(figure^.x0,
figure^.y0, figure^.size, figure^.color);
END;
Delay(DELAY_TIME);
(* wait *)
(* delete
figure:
make black *)
CASE
figure^.type
OF
RECTANGLE:
Rectangle(figure^.x0, figure^.y0, figure^.x0 + figure^.size, figure^.y0
+
figure^.size, _clrBLACK, TRUE);
|
CIRCLE:
Circle(figure^.x0, figure^.y0, figure^.size, _clrBLACK);
END;
figure :=
figure^.next;(*
next figure*)
END;
END PlotFigureList;
VAR
newFigure, figureList:
FigureRcPt;
i:CARDINAL;
x:CHAR;
BEGIN
(* Initialise graphics *)
IF NOT Init(1, 1 , SCREEN_SIZE,
SCREEN_SIZE)
THEN (* creates a drawing window of 900 by 700 *)
WrStr("Sorry, graphics
doesn't
work");WrLn;
RETURN;
END;
(* Add a figure to the list *)
NEW(newFigure);
newFigure^.type := CIRCLE;
newFigure^.x0 := SIZE;
newFigure^.y0 := SIZE;
newFigure^.size := SIZE;
newFigure^.color := _clrBLUE;
newFigure^.next := NIL;
figureList := newFigure;
(* Add more figures to the list *)
FOR i:= 2 TO NBR DO
NEW(newFigure);
newFigure^.type := CIRCLE;
newFigure^.x0 := SIZE * i;
newFigure^.y0 := SIZE * i;
newFigure^.size := SIZE;
newFigure^.color :=
_clrWHITE;
newFigure^.next :=
figureList;
(* add to list *)
figureList := newFigure;
END;
PlotFigureList(figureList);
(* Show graphics until user presses a
key *)
WrStr("Press any key to finish the
program");
x := RdKey(); RdLn; WrLn;
WrLn;
END S4.
Exercise
X1
Exercise
H1
Exercise
H2
Exercise
T1
Exercise