modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Chapter 15 - Example programs


The intent of this chapter is to give several example programs that use nearly every capability of Modula-2 as illustrations of the use of Modules and larger usable programs. These modules and programs are primarily intended to illustrate the method of building up a program from the various constructs studied in the earlier chapters.

Areas

This program may have little or no utilitarian value, but it may be valuable as an illustration of a method for implementing a menu for selection purposes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
MODULE Areas;

(* Coronado Enterprises - 1987 *)

FROM Terminal2 IMPORT WriteString, WriteChar, WriteLn, ReadChar,
                      WriteReal, ReadReal;

VAR InChar, CapInChar : CHAR;

(* ************************************************ Area Of Square *)
PROCEDURE AreaOfSquare;
VAR Length, Area : REAL;
BEGIN
   WriteString("Square    Enter length of a side : ");
   ReadReal(Length);
   Area := Length * Length;
   WriteLn;
   WriteString("The area is ");
   WriteReal(Area,15);
   WriteLn;
END AreaOfSquare;

(* ********************************************* Area Of Rectangle *)
PROCEDURE AreaOfRectangle;
VAR Width, Height, Area : REAL;
BEGIN
   WriteString("Rectangle    Enter Width : ");
   ReadReal(Width);
   WriteLn;
   WriteString("Enter Height ");
   ReadReal(Height);
   Area := Width * Height;
   WriteString("      The area is ");
   WriteReal(Area,15);
   WriteLn;
END AreaOfRectangle;

(* ********************************************** Area Of Triangle *)
PROCEDURE AreaOfTriangle;
VAR Base, Height, Area : REAL;
BEGIN
   WriteString("Triangle    Enter base : ");
   ReadReal(Base);
   WriteLn;
   WriteString("Enter height : ");
   ReadReal(Height);
   Area := 0.5 * Base * Height;
   WriteString("      The area is ");
   WriteReal(Area,15);
   WriteLn;
END AreaOfTriangle;

(* ************************************************ Area Of Circle *)
PROCEDURE AreaOfCircle;
VAR Radius, Area : REAL;
BEGIN
   WriteString("Circle      Enter Radius : ");
   ReadReal(Radius);
   WriteLn;
   Area := 3.141592 * Radius * Radius;
   WriteString("The area is ");
   WriteReal(Area,15);
   WriteLn;
END AreaOfCircle;

(* ************************************************** Main Program *)
BEGIN
   REPEAT
      WriteLn;
      WriteString("Input the first letter of the selection");
      WriteString("and press Enter.");
      WriteLn;
      WriteString("Select shape: Square Rectangle Triangle ");
      WriteString("Circle Quit");
      WriteLn;
      WriteString("Requested shape is ");
      ReadChar(InChar);
      CapInChar := CAP(InChar);           (* Get capital of letter *)
      CASE CapInChar OF
        'S' : AreaOfSquare;    |
        'R' : AreaOfRectangle; |
        'T' : AreaOfTriangle;  |
        'C' : AreaOfCircle;    |
        'Q' : WriteString("Quit program");
              WriteLn;
      ELSE
         WriteChar(InChar);
         WriteString(" Invalid Character ");
         WriteLn;
      END;
   UNTIL CapInChar = 'Q';
END Areas.