| Код | function rpos(const sub, str: string ): Integer; var l1,l2,l3:Integer; flag: Boolean; begin Result:=0; l3:=Length(sub); for l2:=Length(str) downto Length(sub) do begin flag:=True; l1:=l3; while (l1>=1) and (flag) do
begin if str[l2-(l3-l1)]<>sub[l1] then flag:=False; l1:=l1-1; end; Result:=l2-l3+1; if flag and (l1=0) then Exit; end; Result:=0; end;
procedure rom2dec(const sx:string); const all_ch:string ='IVXLCDM'; ed: array [1..9] of string =('I','II','III','IV','V','VI','VII','VIII','IX'); de: array [1..9] of string = ('X','XX','XXX','XL','L','LX','LXX','LXXX','XC'); sot: array [1..9] of string = ('C','CC','CCC','CD','D','DC','DCC','DCCC','CM'); //digi: array [1..7] of Integer =(1,5,10,50,100,500,1000); var ss: string; god,t,i: Integer; begin god:=0; t:=0; ss:=sx;
begin //edin for i:=9 downto 1 do if rpos(ed[i],ss)>0 then begin if (rpos(ed[i],ss)+Length(ed[i])-1<>Length(ss)) then begin Writeln('Error'); Exit; end; if (i=5) and (rpos(ed[4],ss)>0) then t:=-1; god:=god+(i+t); ss:=Copy(ss,1,rpos(ed[(i+t)],ss)-1); Break; end; //dec t:=0; for i:=9 downto 1 do if rpos(de[i],ss)>0 then begin if (rpos(de[i],ss)+Length(de[i])-1<>Length(ss)) then begin Writeln('Error'); Exit; end; if (i=5) and (rpos(de[4],ss)>0) then t:=-1; god:=god+(i+t)*10; ss:=Copy(ss,1,rpos(de[(i+t)],ss)-1); Break; end; //sot t:=0; for i:=9 downto 1 do if rpos(sot[i],ss)>0 then begin if (rpos(sot[i],ss)+Length(sot[i])-1<>Length(ss)) then begin Writeln('Error'); Exit; end; if (i=5) and (rpos(sot[4],ss)>0) then t:=-1; god:=god+(i+t)*100; ss:=Copy(ss,1,rpos(sot[(i+t)],ss)-1); Break; end; //tus while ss<>'' do if rpos('M',ss)>0 then begin god:=god+1000; if Length(ss)=1 then ss:='' else ss:=Copy(ss,1,rpos('M',ss)-1); end else begin
Break; end; end; if (ss='') then Writeln(god) else Writeln('Error'); end;
var sx: string;
begin
{ TODO -oUser -cConsole Main : Insert code here } Writeln('Please enter digital in roman (enter for exit)'); Readln(sx);
while sx<>'' do begin sx:=UpperCase(sx); rom2dec(sx); Writeln('Please enter digital in roman (enter for exit)'); Readln(sx); end; end.
|
|