current program - Programmers Heaven

#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# current program

Posts: 10Member
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 : ');
CLRSCR;
write('First Roman number? ');
write('Operator? ');
write('Second Roman number? ');
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 ');
CLRSCR;
WRITELN('***********************************');
WRITELN(' THE CALCULATION HAS DONE ');
WRITELN('***********************************');
WRITELN('CLICK ENTER TO EXIT ');
CLRSCR;
end.

• Posts: 6,349Member
: 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 : ');
: CLRSCR;
: write('First Roman number? ');
: write('Operator? ');
: write('Second Roman number? ');
: 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 ');
: CLRSCR;
: WRITELN('***********************************');
: WRITELN(' THE CALCULATION HAS DONE ');
: WRITELN('***********************************');
: WRITELN('CLICK ENTER TO EXIT ');
: 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.