(* Programmieraufgabe P-32 *) (* Leonhard Fellermayr *) (* Mat.Nr. 22128130XXXX *) let laenge = String.length;; let anfang = fun s -> String.get s 0;; let rest = fun s -> String.sub s 1 ((laenge s)-1);; let ziffer_wert = function z -> if z='1' then 1 else if z='2' then 2 else if z='3' then 3 else if z='4' then 4 else if z='5' then 5 else if z='6' then 6 else if z='7' then 7 else if z='8' then 8 else if z='9' then 9 else if z='0' then 0 else -4711;; (* Fehler-ID *) let rec wert = function s -> function b -> function v -> if laenge s = 0 then v else let c = anfang s in if ziffer_wert c >= b then -1 else wert (rest s) b (v * b + (ziffer_wert c));; (* Definition der exception *) exception Fehlermeldung of string;; (* Hilfsfunktionen *) let string_of_char = String.make 1;; let doErr msg = raise (Fehlermeldung (msg));; (* String-Check-Funktion *) let rec check_the_string s fs b = if (laenge s) == 0 then fs else if ziffer_wert (anfang s) == -4711 then doErr ("Zeichen " ^ string_of_char (anfang s) ^ " ist keine Ziffer") else if ziffer_wert (anfang s) >= b then doErr ("Ziffer " ^ string_of_char (anfang s) ^ " ist groesser gleich Basis " ^ string_of_int b) else check_the_string (rest s) fs b;; (* rek. fŸr den Stringrest *) (* Die um Fehlerbehandlung erweiterte Funktion zahlenwert *) let zahlenwert = function (s,b) -> match (s,b) with |(_,b) when (b < 2) -> doErr ("Basis " ^ string_of_int (b) ^ " ist zu klein") |(_,b) when (b > 10) -> doErr ("Basis " ^ string_of_int (b) ^ " ist zu gross") |(_,_) -> string_of_int (wert (check_the_string s s b) b 0);; (* EOF *)