ob@IFI.UIB.NO (Ole Bjorn Tuftedal) (04/04/91)
MODULE TstGenSort; (* Richard S. Wiener, Department of Computer Science, University of Colorado at Colorado Springs Colorado Springs, Colorado 80907, U S A. *) (* This module demonstrates the module GenericSorting by calling the exported procedure GenericSort to sort arrays of: real numbers, integers, strings and records. The records are sorted on two different fields. *) (* Related modules: DEFINITION MODULE GenericSorting, file name GENERICS.DEF IMPLEMENTATION MODULE GenericSorting; file name GENERICS:MOD *) FROM GenericSorting IMPORT GenericSort; FROM SYSTEM IMPORT ADR, WORD; FROM InOut IMPORT WriteString, WriteLn, WriteCard, ReadInt, WriteInt, ReadString; FROM RealInOut IMPORT ReadReal, WriteReal; TYPE string60 = ARRAY[0..59] OF CHAR; TYPE person = RECORD name: string60; age: INTEGER; END; (* record *) VAR k: INTEGER; RealNumberArray: ARRAY[11..20] OF REAL; IntegerNumberArray: ARRAY[-11..-5] OF INTEGER; StringArray: ARRAY[35..38] OF string60; PersonArray: ARRAY[1..5] OF person; s: string60; (* sample object *) r: REAL; (* sample object *) i: INTEGER; (* sample object *) p: person; (* sample object *) PROCEDURE RealGreaterThan(r1, r2: ARRAY OF WORD): BOOLEAN; VAR p1, p2: POINTER TO REAL; BEGIN (* RealGreaterThan *) p1:= ADR(r1); p2:= ADR(r2); RETURN (p1 > p2); END RealGreaterThan; PROCEDURE IntegerGreaterThan(i1, i2: ARRAY OF WORD): BOOLEAN; VAR p1, p2: POINTER TO INTEGER; BEGIN (* IntegerGreaterThan *) p1:= ADR(i1); p2:= ADR(i2); RETURN (p1 > p2); END IntegerGreaterThan; PROCEDURE StringGreaterThan(s1, s2: ARRAY OF WORD): BOOLEAN; VAR p1, p2: POINTER TO string60; i: CARDINAL; BEGIN (* StringGreaterThan *) p1:= ADR(s1); p2:= ADR(s2); i:= 0; WHILE (i < 59) AND (p1[i] = p2[i]) DO INC(i); END; (* WHILE i *) RETURN (p1[i] > p2[i]); END StringGreaterThan ; PROCEDURE PersonGreaterThanByName(per1, per2: ARRAY OF WORD): BOOLEAN; VAR p1, p2: POINTER TO person; BEGIN (* PersonGreaterThanByName *) p1:= ADR(per1); p2:= ADR(per2); RETURN StringGreaterThan(p1.name, p2.name); END PersonGreaterThanByName; PROCEDURE PersonGreaterThanByAge(per1, per2: ARRAY OF WORD): BOOLEAN; VAR p1, p2: POINTER TO person; BEGIN (* PersonGreaterThanByAge *) p1:= ADR(per1); p2:= ADR(per2); RETURN IntegerGreaterThan(p1.age, p2.age); END PersonGreaterThanByAge; PROCEDURE InputReals(); VAR i: CARDINAL; BEGIN (* InputReals *) FOR i:= 11 TO 20 DO WriteString("Enter real number "); WriteCard(i,1); WriteString(' : '); ReadReal(RealNumberArray[i]); END; (* FOR i *) END InputReals; PROCEDURE InputIntegers(); VAR i: INTEGER; BEGIN (* InputIntegers *) FOR i:= -11 TO -5 DO WriteString("Enter integer "); WriteString(' : '); ReadInt(IntegerNumberArray[i]); WriteLn; END; (* FOR i *) END InputIntegers; PROCEDURE InputStrings(); VAR i: CARDINAL; BEGIN (* InputReals *) FOR i:= 35 TO 38 DO WriteString("Enter string "); WriteCard(i,1); WriteString(' : '); ReadString(StringArray[i]); WriteLn; END; (* FOR i *) END InputStrings; PROCEDURE InputPersons(); VAR i: CARDINAL; BEGIN (* InputPersons *) FOR i:= 1 TO 5 DO WriteLn; WriteString('Enter the name of a person: '); ReadString(PersonArray[i].name); WriteLn; WriteString('Enter the age of the person: '); ReadInt(PersonArray[i].age); END; (* FOR i *) END InputPersons; BEGIN (* TestGenericSort *) InputReals(); GenericSort(RealNumberArray, r, r, RealGreaterThan); WriteLn; WriteString(' The Sorted Numbers'); WriteLn; WriteString(' ------------------'); WriteLn; FOR k:= 11 TO 20 DO WriteReal(RealNumberArray[k],15); WriteLn; END; (* FOR k *) WriteLn; WriteLn; InputIntegers; GenericSort(IntegerNumberArray, i, i, IntegerGreaterThan); WriteLn; WriteString(' The Sorted Numbers'); WriteLn; WriteString(' ------------------'); WriteLn; FOR k:= -11 TO -5 DO WriteInt(IntegerNumberArray[k],15); WriteLn; END; (* FOR k *) WriteLn; WriteLn; InputStrings; GenericSort(StringArray, s, s, StringGreaterThan); WriteLn; WriteString(' The Sorted Strings'); WriteLn; WriteString(' ------------------'); WriteLn; FOR k:= 35 TO 38 DO WriteString(StringArray[k]); WriteLn; END; (* FOR k *) WriteLn; WriteLn; InputPersons; GenericSort(PersonArray, p, p, PersonGreaterThanByName); WriteLn; WriteString(' Sorted People by Name'); WriteLn; WriteString(' ---------------------'); WriteLn; FOR k:= 1 TO 5 DO WriteLn; WriteString('Name --> '); WriteString(PersonArray[k].name); WriteLn; WriteString('Age --> '); WriteInt(PersonArray[k].age,1); END; (* FOR k *) WriteLn; WriteLn; GenericSort(PersonArray, p, p, PersonGreaterThanByAge); WriteLn; WriteString(' Sorted People by Age'); WriteLn; WriteString(' --------------------'); WriteLn; FOR k:= 1 TO 5 DO WriteLn; WriteString('Name --> '); WriteString(PersonArray[k].name); WriteLn; WriteString('Age --> '); WriteInt(PersonArray[k].age,1); END; (* FOR k *) WriteLn; END TstGenSort. Ole-Bjorn Tuftedal University of Bergen, Norway Internet: tuftedal@ifi.uib.no Bitnet: sinot@nobergen