| 1 |
(* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. *) |
| 2 |
(* This file is part of GNU Modula-2. |
| 3 |
|
| 4 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License as published by the Free |
| 6 |
Software Foundation; either version 2, or (at your option) any later |
| 7 |
version. |
| 8 |
|
| 9 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 |
for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 16 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 17 |
|
| 18 |
MODULE prog21; |
| 19 |
|
| 20 |
(* Referral exercise from 1995 *) |
| 21 |
|
| 22 |
(* Author - Stuart F Lewis March 1997 *) |
| 23 |
|
| 24 |
(* Reads student sports race data from a file *) |
| 25 |
(* stores in an array*) |
| 26 |
(* displays the results *) |
| 27 |
|
| 28 |
(* MODEL SOLUTION FOR prog21 for course.swd *) |
| 29 |
|
| 30 |
FROM StrIO IMPORT WriteLn,WriteString,ReadString; |
| 31 |
FROM StrLib IMPORT StrEqual, StrLess, StrConCat; |
| 32 |
FROM StdIO IMPORT Read, Write; |
| 33 |
FROM NumberIO IMPORT WriteInt, ReadCard,WriteCard; |
| 34 |
FROM ASCII IMPORT eof; |
| 35 |
|
| 36 |
CONST |
| 37 |
MaxPeople = 20; |
| 38 |
EndOfRaceMark = 'qqqqqqqqqq'; |
| 39 |
|
| 40 |
TYPE |
| 41 |
Rec = RECORD |
| 42 |
strdata : ARRAY[0..9] OF CHAR; |
| 43 |
END; |
| 44 |
|
| 45 |
name_type = ARRAY[0..9] OF CHAR; |
| 46 |
OnePerson = RECORD |
| 47 |
name : name_type; |
| 48 |
score : CARDINAL; |
| 49 |
END; |
| 50 |
|
| 51 |
All = ARRAY[1..MaxPeople] OF OnePerson; |
| 52 |
|
| 53 |
VAR |
| 54 |
draw,position,Npeople,count : CARDINAL; |
| 55 |
Results : All; |
| 56 |
|
| 57 |
PROCEDURE continue; |
| 58 |
VAR |
| 59 |
ch : CHAR; |
| 60 |
BEGIN |
| 61 |
WriteLn; |
| 62 |
WriteString("press ENTER to continue > "); |
| 63 |
Read(ch); |
| 64 |
END continue; |
| 65 |
|
| 66 |
(* main program procedures start here *) |
| 67 |
|
| 68 |
PROCEDURE Initialise; |
| 69 |
BEGIN |
| 70 |
Npeople := 0; |
| 71 |
FOR count :=1 TO MaxPeople DO |
| 72 |
WITH Results[count] DO |
| 73 |
name := ' '; |
| 74 |
score := 0; |
| 75 |
END;(*with*) |
| 76 |
END;(*for*) |
| 77 |
END Initialise; |
| 78 |
|
| 79 |
PROCEDURE SearchFor(temp : name_type;in : All ; |
| 80 |
VAR Index : CARDINAL) : BOOLEAN ; |
| 81 |
BEGIN |
| 82 |
FOR count :=1 TO Npeople DO |
| 83 |
WITH in[count] DO |
| 84 |
IF StrEqual(name,temp) |
| 85 |
THEN |
| 86 |
Index := count; |
| 87 |
RETURN TRUE; |
| 88 |
END;(*if*) |
| 89 |
END;(*with*) |
| 90 |
END;(*for*) |
| 91 |
RETURN FALSE; |
| 92 |
END SearchFor; |
| 93 |
|
| 94 |
PROCEDURE Add(temp : name_type;VAR in : All ; VAR Index : CARDINAL) ; |
| 95 |
BEGIN |
| 96 |
Npeople := Npeople +1; |
| 97 |
WITH in[Npeople] DO |
| 98 |
name := temp; |
| 99 |
END;(*with*) |
| 100 |
Index := Npeople; |
| 101 |
END Add; |
| 102 |
|
| 103 |
PROCEDURE ReadStr10(VAR temp : name_type) ; |
| 104 |
BEGIN |
| 105 |
temp := ' '; |
| 106 |
ReadString(temp); |
| 107 |
StrConCat(temp,' ',temp); |
| 108 |
WriteString(temp); |
| 109 |
WriteString('****'); |
| 110 |
WriteLn; |
| 111 |
END ReadStr10; |
| 112 |
|
| 113 |
|
| 114 |
PROCEDURE InputData; |
| 115 |
VAR |
| 116 |
race,temp : name_type; |
| 117 |
Index,points : CARDINAL; |
| 118 |
BEGIN |
| 119 |
ReadStr10(race); |
| 120 |
WHILE NOT StrEqual(race,EndOfRaceMark) DO |
| 121 |
ReadStr10(temp); (*blank line*) |
| 122 |
ReadStr10(temp); (*first person in a race*) |
| 123 |
points := 3; (*first place*) |
| 124 |
WHILE StrEqual(temp,EndOfRaceMark) DO |
| 125 |
IF NOT SearchFor(temp,Results,Index) THEN |
| 126 |
Add(temp,Results,Index); |
| 127 |
END;(*if*) |
| 128 |
IF points > 0 THEN |
| 129 |
Results[Index].score := Results[Index].score + points; |
| 130 |
points := points - 1; |
| 131 |
END;(*if*) |
| 132 |
ReadStr10(temp); |
| 133 |
END;(*while same race*) |
| 134 |
(* ReadStr10(temp); blank line*) |
| 135 |
ReadStr10(race); |
| 136 |
END;(*while races*) |
| 137 |
WriteString(race); |
| 138 |
END InputData; |
| 139 |
|
| 140 |
PROCEDURE OutputResults; |
| 141 |
BEGIN |
| 142 |
position := 1; |
| 143 |
draw := 0; |
| 144 |
FOR count :=1 TO Npeople DO |
| 145 |
WITH Results[count] DO |
| 146 |
WriteString(name); |
| 147 |
WriteCard(score,6); |
| 148 |
WriteCard(position,4); |
| 149 |
IF score > Results[count + 1].score |
| 150 |
THEN |
| 151 |
position := position + 1 + draw; |
| 152 |
IF draw > 0 |
| 153 |
THEN |
| 154 |
draw := 0; |
| 155 |
WriteString("="); |
| 156 |
END;(*if*) |
| 157 |
END;(*if*) |
| 158 |
IF (score = Results[count + 1].score) |
| 159 |
THEN |
| 160 |
draw := draw + 1; |
| 161 |
WriteString("="); |
| 162 |
END;(*if*) |
| 163 |
WriteLn; |
| 164 |
END;(*with*) |
| 165 |
END;(*for*) |
| 166 |
END OutputResults; |
| 167 |
|
| 168 |
PROCEDURE Less(first,second : CARDINAL) : BOOLEAN; |
| 169 |
BEGIN |
| 170 |
(*compares score first then name to maintain order *) |
| 171 |
(* comparison is back to front because we want descending order*) |
| 172 |
IF Results[first].score > Results[second].score |
| 173 |
THEN RETURN TRUE |
| 174 |
ELSE |
| 175 |
IF (Results[first].score = Results[second].score) |
| 176 |
AND (StrLess(Results[first].name,Results[second].name)) |
| 177 |
THEN RETURN TRUE |
| 178 |
ELSE RETURN FALSE |
| 179 |
END;(*IF*) |
| 180 |
END;(*IF*) |
| 181 |
END Less; |
| 182 |
|
| 183 |
PROCEDURE Swap(first,second : CARDINAL); |
| 184 |
VAR |
| 185 |
temp : OnePerson; |
| 186 |
BEGIN |
| 187 |
temp := Results[first]; |
| 188 |
Results[first] := Results[second]; |
| 189 |
Results[second] := temp; |
| 190 |
END Swap; |
| 191 |
|
| 192 |
|
| 193 |
BEGIN (*main program*) |
| 194 |
Initialise; |
| 195 |
InputData; |
| 196 |
WriteLn; |
| 197 |
(*HSort(Npeople,Less,Swap);*) |
| 198 |
OutputResults; |
| 199 |
END prog21. |