Universität Ulm,
Fakultät für Mathematik und Wirtschaftswissenschaften,
SAI
Lösung zu Blatt 8 --- Allgemeine Informatik I (WS 1998)
8. Verschlüsselung
MODULE crypt;
FROM InOut IMPORT Read, Write, WriteString, WriteLn, Done;
FROM SysExit IMPORT Exit;
VAR
ch, nch, nch2: CHAR;
next: BOOLEAN;
BEGIN
next := FALSE;
Read(ch);
WHILE Done DO (*solange also Read erfolgreich*)
CASE ch OF
| 'A' .. 'Z': nch := CHR(65 + ABS(90 - ORD(ch)))
| 'a' .. 'z': nch := CHR(97 + ABS(122 - ORD(ch)))
ELSE nch := ch;
END;
IF next THEN
IF nch # 12C THEN
Write(nch);
Write(nch2);
ELSE
Write(nch2);
Write(nch);
END;
next := FALSE;
ELSE
nch2 := nch;
IF nch = 12C THEN WriteLn; ELSE next := TRUE; END;
END;
Read(ch);
END;
END crypt.
9. Folgen
MODULE order;
FROM InOut IMPORT ReadInt, WriteInt, WriteLn, WriteString, Done;
FROM SysExit IMPORT Exit;
TYPE Zustand = (start, eins, zwei, viele);
(* Die Zustaende des Automaten zur Aufgabe *)
VAR
zahl, last: INTEGER;
zustand: Zustand;
next: BOOLEAN;
BEGIN
zustand := start;
ReadInt(zahl);
WHILE Done DO (*solange also Read erfolgreich*)
CASE zustand OF
| start: zustand := eins; WriteInt(zahl, 1);
| eins: (* Falls direkter Nachfolger: gehe in Zustand zwei *)
IF zahl = last +1 THEN
zustand := zwei;
ELSE
WriteString(", ");
WriteInt(zahl, 1);
END;
| zwei:
IF zahl = last +1 THEN (* Eine Folge :-) *)
zustand := viele;
WriteString("...");
ELSE
WriteString(", ");
WriteInt(last, 1);
WriteString(", ");
WriteInt(zahl, 1);
zustand := eins;
END;
| viele: (* Nur die letzte Zahl einer Folge ausgeben *)
IF zahl # (last +1) THEN
WriteInt(last, 1);
WriteString(", ");
WriteInt(zahl, 1);
zustand := eins;
END;
ELSE Exit(1);
END;
last := zahl;
ReadInt(zahl);
END;
IF zustand = zwei THEN
WriteString(", "); WriteInt(last, 1);
ELSIF zustand = viele THEN
WriteInt(last, 1);
END;
WriteString(".");
WriteLn;
END order.
Ingo Melzer, 15 Dezember 1998