Complete Projects
Notes: digit at guards against reading past the end of the string (ALGOL 68's AND is not guaranteed to short-circuit; a68g offers ANDF for that).
42.1 ALGOL 68: RPN calculator
Notes: digit at guards against reading past the end of the string (ALGOL 68's AND is not guaranteed to short-circuit; a68g offers ANDF for that). The two pops are written as separate declarations: a collateral declaration REAL b = pop, a = pop would leave the order of the pops unspecified. char in string yields a BOOL that is simply voided here; its useful effect is setting k.
BEGIN
[1:100] REAL stack; INT sp := 0;
PROC push = (REAL x) VOID: (sp +:= 1; stack[sp] := x);
PROC pop = REAL: IF sp = 0 THEN print(("stack empty", newline)); stop ELSE sp -:= 1; stack[sp + 1] FI;
PROC evaluate = (STRING expr) REAL:
BEGIN
PROC digit at = (INT j) BOOL:
IF j > UPB expr THEN FALSE ELSE expr[j] >= "0" AND expr[j] <= "9" FI;
sp := 0;
INT i := LWB expr;
WHILE i <= UPB expr DO
CHAR c = expr[i];
IF digit at(i) THEN
REAL v := 0;
WHILE digit at(i) DO
v := v * 10 + (ABS expr[i] - ABS "0"); i +:= 1
OD;
push(v)
ELSE
IF c /= " " THEN
REAL b = pop; # separate declarations: b must be popped first #
REAL a = pop;
INT k := 0;
char in string(c, k, "+-*/"); # k = position of the operator, or 0 #
push(CASE k IN a + b, a - b, a * b, a / b
OUT print(("bad operator ", c, newline)); stop
ESAC)
FI;
i +:= 1
FI
OD;
pop
END;
print((fixed(evaluate("3 4 + 2
…42.2 ALGOL 68: word frequency counter
Reads a text file, normalizes words to lower case, stores counts in a binary search tree (Chapter 33) keyed by STRING, and prints the most frequent words as an aligned table using printf formats. Extensions: ignore a stop-word list read from a second file; output as CSV.
42.3 ALGOL 60: numerical library
A MARST program providing simpson (Jensen's device), newton sqrt, Gaussian elimination with partial pivoting on dynamic arrays, and a least-squares line fit, with a driver that tests each against known results. Write it in the style of the *Handbook for Automatic Computation*: each procedure documented with a 'comment' describing parameters.
42.4 ALGOL 68: matrix toolkit
Operate on [,] REAL values with operators +, -, *, T (transpose), INV (Gauss–Jordan), DET, a pretty-printer using formats, and a test suite comparing a * INV a with the identity matrix within a tolerance.
42.5 Cross-language comparison project
Implement the same five algorithms (GCD, sieve, quicksort, binary search, eight queens) in ALGOL 60 (MARST), ALGOL 68 (a68g), Pascal and C. Compare line counts, readability, safety (what each language checks), and run time.