Solutions TOPIC G: Records

Contents :

  RECORD.

Exercises :


D1: Sum of Complex numbers.

Exercise
MODULE D1;
<* NOOPTIMIZE + *>
    FROM IO IMPORT RdChar, WrChar, WrStr, RdStr, WrLn, RdCard, WrCard, RdInt, WrInt, RdReal, WrReal, WrFixReal, RdBool, WrBool;  (*
import general procedures *)
 

    VAR
      c1, c2, c3: RECORD            (* variable-declarations *)
                    re, img: REAL;
                  END;
BEGIN
    WrLn;
    c1.re := 3.5;
    c1.img := -4.76;
    c2.re := 4.0;
    c2.img := 14.6;

    (* sum *)
    c3.re := c1.re + c2.re;
    c3.img := c1.img + c2.img;

    (* print *)
    WrStr("The complex sum of ");WrFixReal(c1.re, 1, 0);WrChar('+'); WrFixReal(c1.img, 1, 0);WrChar('i');
    WrStr(" and ");WrFixReal(c2.re, 1, 0);WrChar('+'); WrFixReal(c2.img, 1, 0);WrChar('i');
    WrStr(" is: ");WrFixReal(c3.re, 2, 0);WrChar('+'); WrFixReal(c3.img, 2, 0);WrChar('i');WrLn;
END D1.


S1

Exercise


S2

Exercise

S3

Exercise

S4

Exercise

MODULE S4;
<* WOFF + *> <* NOOPTIMIZE + *>
    FROM RealMath IMPORT sqrt, exp, sin, cos, tan, arcsin, arccos, arctan, power, round;
    FROM Lib IMPORT Delay; (* Wait n milliseconds *)
     FROM Wimdows IMPORT Text, SetFont, GetKey;
    FROM Wimdows IMPORT Window, KillWindow, ClearScreen, Line, Rectangle, Circle, Ellipse, Disc, _clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA;
    FROM Wimdows IMPORT _clrBROWN, _clrWHITE, _clrGRAY, _clrLIGHTBLUE, _clrLIGHTGREEN, _clrLIGHTCYAN, _clrLIGHTRED, _clrLIGHTMAGENTA, _clrLIGHTYELLOW, _clrBRIGHTWHITE;
         (* zie C:/Bin/Xds/Def/Ts/Graph.def  *)

   (* A. Constants *)
    CONST SCREEN_SIZE = 600;
        MIN_FIG_SIZE = 10;
    MAX_FIG_SIZE = 50;

   (* B. Types *)
    TYPE
        ColourSb = [0..15];  (* subrange *)
        SizeSb = [MIN_FIG_SIZE..MAX_FIG_SIZE];

   (* C. Procedures *)

    (* GEGEVEN procedures:
       de cirkel & ellipse komen uit de graph-library en zijn als volgt gedefinieerd:
        PROCEDURE Circle(x0, y0, r, c :LONGCARD);
          centre x0,y0; radius r
        PROCEDURE Ellipse ( x0,y0,a,b,c :LONGCARD; fill :BOOLEAN );
          center x0,y0; semi-axes a,b;  color c ; whether filled
      neem voor a size/2 en voor b size/3

  de andere procedures zijn als volgt gedefinieerd:
         PROCEDURE Square(x0,y0,size,colour:CARDINAL);
         PROCEDURE Asterisk(x0,y0,size,colour:CARDINAL); tekent een 'ster'
         PROCEDURE Triangle(x0,y0,size,colour:CARDINAL);
    x0, y0 is steeds het middelpunt

      deze procedure geeft een random getal terug tussen min en max:
         PROCEDURE RandomCard(min, max: CARDINAL): CARDINAL;
   *)
    PROCEDURE Square(x0,y0,size,colour:CARDINAL);
    BEGIN
      Rectangle (x0 - size/2,y0 - size/2,x0 + size/2,y0 + size/2, colour,TRUE);
    END Square;

    PROCEDURE Asterisk(x0,y0,size,colour:CARDINAL);
      VAR a:CARDINAL;
    BEGIN
      a:= VAL(CARDINAL,((VAL(REAL,size)/(2.0))*0.7071) ); (* cos(45)*)
      Line(x0-size/2, y0, x0+size/2,y0, colour);
      Line(x0, y0+size/2, x0, y0-size/2, colour);
      Line(x0-a, y0+a, x0+a, y0-a, colour);
      Line(x0+a, y0+a, x0-a, y0-a, colour);
    END Asterisk;

    PROCEDURE Triangle(x0,y0,size,colour:CARDINAL);
      VAR a:CARDINAL;
    BEGIN
      a:= VAL(CARDINAL,((VAL(REAL,size)/(2.0))*0.7071) ); (* cos(45)*)
      Line(x0, y0-a, x0-size/2, y0+a, colour);
      Line(x0-size/2, y0+a, x0+size/2, y0+a, colour);
      Line(x0, y0-a, x0+size/2, y0+a, colour);
    END Triangle;

    VAR  gSeed: CARDINAL; (* Random waarde generator *)
    PROCEDURE RandomCard(min, max: CARDINAL): CARDINAL;
       VAR
         factor: REAL;
    BEGIN
        factor := (VAL(REAL, max - min) + 0.99999) / FLOAT(1013 - 1);
        gSeed := (gSeed * 351 + 138) MOD 1013;
        RETURN min + VAL(CARDINAL, VAL(REAL, gSeed) * factor);
    END RandomCard;
    (************* TOT HIER DE GEGEVEN PROCEDURES ******************)

   (* D. Variables*)
    VAR x: CARDINAL;
BEGIN

  Window("Exercise S4", SCREEN_SIZE, SCREEN_SIZE);

  SetFont(1, _clrWHITE);
  Text("Welke figuur wenst u? (c=cirkel, e=ellips, v=vierkant, s=ster, d=driehoek)" , 10, 10);
  x := GetKey();
  ClearScreen();


  Rectangle(1, 1, 60, 60, _clrWHITE, FALSE);



  (* tell user to press a key to finish the program... *)
  SetFont(1, _clrRED);
  Text("press any key to finish the program", SCREEN_SIZE/2-120, 10);
  (* ...and program will wait here until user presses a key *)
  x := GetKey();
  (* finally close the window *)
  KillWindow();

END S4.


S5

Exercise

MODULE S5;
<* WOFF + *> <* NOOPTIMIZE + *>
FROM IO IMPORT WrStr, WrLn, RdCard, WrCard, RdInt, WrInt, RdKey, RdLn, WrFixReal;
FROM Windows IMPORT VK_ESCAPE;
FROM Wimdows IMPORT SetFont, Text, GetKeyPressed;
FROM Wimdows IMPORT Window, KillWindow, Line, Ellipse, Disc, Circle, Rectangle, _clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, _clrMAGENTA;
FROM RealMath IMPORT cos, sin, pi;
FROM Lib IMPORT Delay; (* Wait n milliseconds *)

CONST SCREEN_SIZE = 600;

TYPE LedemaatRc = RECORD
                    x,y: CARDINAL;
                    lengte, lengteVoet: CARDINAL;
                    hoek: INTEGER; (* in graden *)
                    kleur: CARDINAL;
                  END;

PROCEDURE TekenLedemaat(ledemaat: LedemaatRc);
  VAR hoek_rad, x2, y2, x3, y3: REAL;
BEGIN
  hoek_rad := VAL(REAL, ledemaat.hoek) * pi / 180.0;
  x2 := VAL(REAL, ledemaat.x) + VAL(REAL, ledemaat.lengte) * sin(hoek_rad);
  y2 := VAL(REAL, ledemaat.y) + VAL(REAL, ledemaat.lengte) * cos(hoek_rad);
  Line(ledemaat.x, ledemaat.y, VAL(CARDINAL, x2), VAL(CARDINAL, y2), ledemaat.kleur);
  IF ledemaat.lengteVoet > 0 THEN
    x3 := x2 + VAL(REAL, ledemaat.lengteVoet) * cos(hoek_rad);
    y3 := y2 - VAL(REAL, ledemaat.lengteVoet) * sin(hoek_rad);
    Line(VAL(CARDINAL, x2), VAL(CARDINAL, y2), VAL(CARDINAL, x3), VAL(CARDINAL, y3), ledemaat.kleur);
  END;
END TekenLedemaat;

(*********************************************************************************)
  VAR k: CARDINAL;
      ledemaat: LedemaatRc;
BEGIN
  WrLn;
  Window("Exercise S5",SCREEN_SIZE,SCREEN_SIZE);

  ledemaat.x := 100;
  ledemaat.y := 100;
  ledemaat.lengte := 100;
  ledemaat.lengteVoet := 20;
  ledemaat.hoek := -60;
  ledemaat.kleur := _clrRED;
  TekenLedemaat(ledemaat);


  (* tell user to press escape to finish the program... *)
  SetFont(1, _clrRED);
  Text("press ESC to quit", SCREEN_SIZE/2-50, 10);
  (* ...and program will wait here until user presses escape *)
  REPEAT
    Delay(25);
  UNTIL (GetKeyPressed(k) AND (k = VK_ESCAPE));
  (* finally close the window *)
  KillWindow();

END S5.


X1

 PROCEDURE Arrow(x0, y0, x1, y1, size, colour: CARDINAL);
   CONST ANGLE = 0.3;
         PI = 3.14;
   VAR alfa: REAL;
       xa, ya: ARRAY[1..3] OF CARDINAL;

 BEGIN
   Line(x0, y0, x1, y1, colour);
   IF x1 # x0 THEN
     alfa := arctan((FLOAT(y1)-FLOAT(y0))/(FLOAT(x1)-FLOAT(x0)));
   ELSE
     IF y1 > y0 THEN
       alfa := PI/2.0;
     ELSE
       alfa := -PI/2.0;
     END;
   END;
   xa[1] := x1;
   ya[1] := y1;
   xa[2] := VAL(INTEGER, x1) - VAL(INTEGER, FLOAT(size) * cos(alfa + ANGLE));
   ya[2] := VAL(INTEGER, y1) - VAL(INTEGER, FLOAT(size) * sin(alfa + ANGLE));
   xa[3] := VAL(INTEGER, x1) - VAL(INTEGER, FLOAT(size) * cos(alfa - ANGLE));
   ya[3] := VAL(INTEGER, y1) - VAL(INTEGER, FLOAT(size) * sin(alfa - ANGLE));
   Polygon(3, xa, ya, colour, TRUE);
 END Arrow;

Exercise


H1

Exercise

H2

Exercise

T1

Exercise