program RomanExpressions; Function OutOfRome(S:String):LONGINT; var C,I:integer; Begin C := 0; I := 1; While I < length(S) do Begin Case S[I] of 'M': C := C+1000; 'D': C := C+500; 'C': Begin Case S[I+1] of 'M':Begin C:= C+900; I:=I+1; End; 'D':Begin C:= C+400; I:=I+1; End; Else C := C+100; End; End; 'L': C:= C+50; 'X': Begin Case S[I+1] of 'C':Begin C:= C+90; I:=I+1; End; 'L':Begin C:= C+40; I:=I+1; End; Else C := C+10; End; End; 'V': C:= C+5; 'I': Begin Case S[I+1] of 'X':Begin C:= C+9; I:=I+1; End; 'V':Begin C:= C+4; I:=I+1; End; Else C := C+1; End; End; End; I:= I+1; End; If I = Length(S) then Case S[I] of 'M':C:= C+1000; 'D':C:= C+500; 'C':C:= C+100; 'L':C:= C+50; 'X':C:= C+10; 'V':C:= C+5; 'I':C:= C+1; 'O':C:= C+0; End; OutOfRome := C; End; Function ToRome(C:LONGINT):string; var S:String; I,J:integer; Begin S := ''; if C <> 0 then begin I := C div 1000; If I > 0 then For J := 1 to I do S := S+'M'; C := C mod 1000; I := C div 900; If I = 1 then Begin S := S+'CM'; C := C - 900; End; I := C div 500; If I > 0 then For J := 1 to I do S := S+'D'; C := C mod 500; I := C div 400; If I = 1 then Begin S := S+'CD'; C := C - 400; End; I := C div 100; If I > 0 then For J := 1 to I do S := S+'C'; C := C mod 100; I := C div 90; If I = 1 then Begin S := S+'XC'; C := C - 90; End; I := C div 50; If I > 0 then For J := 1 to I do S := S+'L'; C := C mod 50; I := C div 40; If I = 1 then Begin S := S+'XL'; C := C - 40; End; I := C div 10; If I > 0 then For J := 1 to I do S := S+'X'; C := C mod 10; I := C div 9; If I = 1 then Begin S := S+'IX'; C := C - 9; End; I := C div 5; If I > 0 then For J := 1 to I do S := S+'V'; C := C mod 5; I := C div 4; If I = 1 then Begin S := S+'IV'; C := C - 4; End; For I := 1 to C do S := S+'I'; ToRome := S; Exit; End;{If c<> 0} if c=0 then ToRome:='O'; End; var S:string; P: array[1..10] of integer; cislo:longint; vysek: String; Znam:char; I,J,Z,pom : integer; ERROR :boolean; Begin Repeat Readln(S); Case S[1] of 'R': Begin Writeln('Ready'); For I := 1 to 10 do P[I] := 0; End; 'Q': BEgin Writeln('Bye'); halt; end; Else Begin If S[2] = '=' then Begin J := ord(S[1])-ord('0'); Z := 3; End Else Begin J := 10; Z := 4; End; cislo:=0; vysek :=''; Znam := '+'; error := false; While Z <= Length(S) do BEGIN Case S[Z] of '+':begin if pom = 0 then begin if znam = '+' then cislo:=cislo + outofrome(vysek) else cislo:=cislo - outofrome(vysek) ; vysek :=''; end else begin if znam = '+' then cislo := cislo + P[pom] else cislo := cislo - P[pom]; end; pom := 0; if cislo >10000 then begin writeln ('Error'); error :=true; break; end; znam := s[z]; end; '-':begin if pom = 0 then begin if znam = '+' then cislo:=cislo + outofrome(vysek) else cislo:=cislo - outofrome(vysek) ; vysek :=''; end else begin if znam = '+' then cislo := cislo + P[pom] else cislo := cislo - P[pom]; end; pom := 0; if cislo >10000 then begin writeln ('Error'); error := true; break; end; znam := s[z]; end; '1'..'9':Begin if (Z < length (S)) and (S[Z]='1') and (s[z+1] ='0') then begin pom:=10; Z:=Z+1; end else pom:=ord(S[z])-ord('0'); IF p[pom] =0 then begin writeln('Error'); ERROR := TRUE; break; end; End; 'A'..'Z':begin vysek:=vysek+S[z]; pom := 0; end; end; z:= z+1; End; IF (ERROR = FALSE) THEN BEGIN IF (pom = 0) then begin if znam = '+' then cislo:=cislo + outofrome(vysek) else cislo:=cislo - outofrome(vysek) ; vysek :=''; end else begin if znam = '+' then cislo := cislo + P[pom] else cislo := cislo - P[pom]; end; pom := 0; if cislo >10000 then BEGIN writeln ('Error'); ERROR := TRUE; END; p[J] := cislo; IF NOT ERROR THEN writeln (J,'=',torome(cislo)); END; End; End; Until (S = 'QUIT'); End.