current program

program RomanCalculator;


uses wincrt;


const
cRomanChars='IVXLCDM';
cRomanValues:array[1..7] of integer = (1, 5, 10, 50, 100, 500, 1000);

var
operator:char;
roman1,roman2:string;
result1,result2,result3:integer;
word,result4:string;

function RomanToDecimal(s:string):integer;
var
i:integer;
Result:integer;
begin
Result:= 0;
if Length(s)= 0
then Exit; { No number given, exit the function }
for i:= 1 to Length(s) do
if Pos(s[i], cRomanChars) = 0
then { Invalid character found }
begin
Result:= -1; { Give error result }
Exit; { Exit the function }
end
else
Result:= Result + cRomanValues[Pos(s[i], cRomanChars)];
RomanToDecimal:=Result;
if Result>=4000
then
writeln('Error');
end;



function DecimalToRoman(Number:integer):string;
const
cRomanChars:array[1..7] of string = ('I','V' ,'X' ,'L' ,'C' ,'D' ,'M' );

var
i,j:integer;
s:string;
Result:string;
begin
i:=7;
Result:='';
while (Number>0) and (i>0) do
begin
while Number-cRomanValues[i]>=0 do
begin
Result:=Result+cRomanChars[i]; { Add roman digit to result }
Number:=Number-cRomanValues[i]; { Remove the value }
end;
dec(i);
end;
i:=6;
repeat
s:='';
for j:=1 to 4 do { create a string with 4 the same roman digits }
s:=s+cRomanChars[i];
j:=Pos(s, Result);
while j>0 do
begin
Delete(Result,j,4); { change 4 roman digits into small-large combination}
Insert(cRomanChars[i]+cRomanChars[i+1],Result,j); {ie: IIII to IV }
j:=Pos(s,Result);
end;
dec(i);
until i=0;
DecimalToRoman:=Result;
end;



begin
WRITELN('---------------------------------------------');
WRITELN(' WELLCOME TO ROMAN CALCULATOR ');
WRITELN('PROGRAMMER : HONG KONG TANG KING PO COLLEGE');
WRITELN(' FORM 4A STUDENT CALVIN LAW');
WRITELN(' CLASS NUMBER IS 17');
WRITELN('---------------------------------------------');
WRITE('PLEASE CLICK ENTER TO USE : ');
READLN;
CLRSCR;
write('First Roman number? ');
readln(roman1);
write('Operator? ');
readln(operator);
write('Second Roman number? ');
readln(roman2);
RomanToDecimal(roman1);
result1:=RomanToDecimal(roman1);
RomanToDecimal(roman2);
result2:=RomanToDecimal(roman2);
result3:=0;
case operator of
'+':begin
result3:=result1+result2;
word:='plus';
end;
'-':begin
result3:=result1-result2;
word:='subtract';
end;
'*':begin
result3:=result1*result2;
word:='multiply';
end;
'/':begin
result3:=result1 div result2;
word:='divide';
end;
end;
if result3>=4000
then writeln('Error')
else
DecimalToRoman(result3);
result4:=DecimalToRoman(result3);
writeln('This is ',result1,word,result2,' and the answer is ',result4,' (',result3,').');
WRITELN('PLEASE CLICK ENTER ');
READLN;
CLRSCR;
WRITELN('***********************************');
WRITELN(' THE CALCULATION HAS DONE ');
WRITELN('***********************************');
WRITELN('CLICK ENTER TO EXIT ');
READLN;
CLRSCR;
end.

Comments

  • : program RomanCalculator;
    :
    :
    : uses wincrt;
    :
    :
    : const
    : cRomanChars='IVXLCDM';
    : cRomanValues:array[1..7] of integer = (1, 5, 10, 50, 100, 500, 1000);
    :
    : var
    : operator:char;
    : roman1,roman2:string;
    : result1,result2,result3:integer;
    : word,result4:string;
    :
    : function RomanToDecimal(s:string):integer;
    : var
    : i:integer;
    : Result:integer;
    : begin
    : Result:= 0;
    : if Length(s)= 0
    : then Exit; { No number given, exit the function }
    : for i:= 1 to Length(s) do
    : if Pos(s[i], cRomanChars) = 0
    : then { Invalid character found }
    : begin
    : Result:= -1; { Give error result }
    : Exit; { Exit the function }
    : end
    : else
    : Result:= Result + cRomanValues[Pos(s[i], cRomanChars)];
    : RomanToDecimal:=Result;
    : if Result>=4000
    : then
    : writeln('Error');
    : end;
    :
    :
    :
    : function DecimalToRoman(Number:integer):string;
    : const
    : cRomanChars:array[1..7] of string = ('I','V' ,'X' ,'L' ,'C' ,'D' ,'M' );
    :
    : var
    : i,j:integer;
    : s:string;
    : Result:string;
    : begin
    : i:=7;
    : Result:='';
    : while (Number>0) and (i>0) do
    : begin
    : while Number-cRomanValues[i]>=0 do
    : begin
    : Result:=Result+cRomanChars[i]; { Add roman digit to result }
    : Number:=Number-cRomanValues[i]; { Remove the value }
    : end;
    : dec(i);
    : end;
    : i:=6;
    : repeat
    : s:='';
    : for j:=1 to 4 do { create a string with 4 the same roman digits }
    : s:=s+cRomanChars[i];
    : j:=Pos(s, Result);
    : while j>0 do
    : begin
    : Delete(Result,j,4); { change 4 roman digits into small-large combination}
    : Insert(cRomanChars[i]+cRomanChars[i+1],Result,j); {ie: IIII to IV }
    : j:=Pos(s,Result);
    : end;
    : dec(i);
    : until i=0;
    : DecimalToRoman:=Result;
    : end;
    :
    :
    :
    : begin
    : WRITELN('---------------------------------------------');
    : WRITELN(' WELLCOME TO ROMAN CALCULATOR ');
    : WRITELN('PROGRAMMER : HONG KONG TANG KING PO COLLEGE');
    : WRITELN(' FORM 4A STUDENT CALVIN LAW');
    : WRITELN(' CLASS NUMBER IS 17');
    : WRITELN('---------------------------------------------');
    : WRITE('PLEASE CLICK ENTER TO USE : ');
    : READLN;
    : CLRSCR;
    : write('First Roman number? ');
    : readln(roman1);
    : write('Operator? ');
    : readln(operator);
    : write('Second Roman number? ');
    : readln(roman2);
    : RomanToDecimal(roman1);
    : result1:=RomanToDecimal(roman1);
    : RomanToDecimal(roman2);
    : result2:=RomanToDecimal(roman2);
    : result3:=0;
    : case operator of
    : '+':begin
    : result3:=result1+result2;
    : word:='plus';
    : end;
    : '-':begin
    : result3:=result1-result2;
    : word:='subtract';
    : end;
    : '*':begin
    : result3:=result1*result2;
    : word:='multiply';
    : end;
    : '/':begin
    : result3:=result1 div result2;
    : word:='divide';
    : end;
    : end;
    : if result3>=4000
    : then writeln('Error')
    : else
    : DecimalToRoman(result3);
    : result4:=DecimalToRoman(result3);
    : writeln('This is ',result1,word,result2,' and the answer is ',result4,' (',result3,').');
    : WRITELN('PLEASE CLICK ENTER ');
    : READLN;
    : CLRSCR;
    : WRITELN('***********************************');
    : WRITELN(' THE CALCULATION HAS DONE ');
    : WRITELN('***********************************');
    : WRITELN('CLICK ENTER TO EXIT ');
    : READLN;
    : CLRSCR;
    : end.
    :
    Here is the full RomanToDecimal() function:
    [code]
    function RomanToDecimal(s: string): integer;
    var
    i: integer;
    begin
    Result := 0;
    i := 1;
    while i <= Length(s) do
    begin
    if Pos(s[i], cRomanChars) = 0 then
    begin
    Result := -1;
    Exit;
    end else begin
    if (i < Length(s)) and
    (Pos(s[i+1], cRomanChars) > Pos(s[i], cRomanChars)) then
    begin
    Result := Result + cRomanValues[Pos(s[i+1], cRomanChars)] -
    cRomanValues[Pos(s[i], cRomanChars)];
    inc(i);
    end else
    Result := Result + cRomanValues[Pos(s[i], cRomanChars)];
    end;
    inc(i);
    end;
    end;
    [/code]
    Be sure to understand the changes from the last one.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion