Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program check;
- var
- input : string;
- a : array[1..12] of char;
- i : word;
- c : char;
- successful : boolean;
- function is_valid_char(c : char) : boolean;
- var
- i : word;
- successful2 : boolean;
- begin
- successful2 := false;
- for i := 1 to 12 do
- begin
- if c = a[i] then //Does the character match the valid character at index i?
- begin
- successful2 := true; //Yes it does, set successful to true
- break; //Stop loop, character is valid
- end;
- end;
- exit(successful2); //If the character didn't match any of the values in the array then successful will still be false
- end;
- begin
- write('Enter a string: ');
- readln(input);
- a[1] := 'i';
- a[2] := 'I';
- a[3] := 'V';
- a[4] := 'v';
- a[5] := 'C';
- a[6] := 'c';
- a[7] := 'D';
- a[8] := 'd';
- a[9] := 'L';
- a[10] := 'l';
- a[11] := 'M';
- a[12] := 'n';
- successful := true; //Set this to true so that if there are no invalid characters you can tell it succeeded
- for i := 1 to length(input) do
- begin
- c := input[i]; //Get the array value of 'input' at index 'i'
- if is_valid_char(c) <> true then //Is the character valid?
- begin
- successful := false; //We found an invalid character, set successful to false
- break; //Break out of the loop, no point checking the rest
- end;
- end;
- if successful = true then //If there was an invalid character then successful would have been set to false
- Begin
- rn:='';
- ReadLn(input);
- Temp:=input;
- Repeat
- If (Temp = M)Then Begin
- D:=Temp Div 1000;
- For i:=1 To D Do Begin
- rn:=rn+'M';
- End;
- Temp:= Temp Mod 1000;
- End
- Else Temp:= Temp Mod 1000;
- If (Temp Div 100 <> 0)Then Begin
- D:=Temp Div 100;
- Case (D) Of
- 1:rn:=rn+'C';
- 2:rn:=rn+'CC';
- 3:rn:=rn+'CCC';
- 4:rn:=rn+'CD';
- 5:rn:=rn+'D';
- 6:rn:=rn+'DC';
- 7:rn:=rn+'DCC';
- 8:rn:=rn+'DCCC';
- 9:rn:=rn+'CM';
- End;
- Temp:=Temp Mod 100;
- End;
- If (Temp Div 10 <> 0) Then Begin
- D:=Temp Div 10;
- Case (D) Of
- 1:rn:=rn+'X';
- 2:rn:=rn+'XX';
- 3:rn:=rn+'XXX';
- 4:rn:=rn+'XL';
- 5:rn:=rn+'L';
- 6:rn:=rn+'LX';
- 7:rn:=rn+'LXX';
- 8:rn:=rn+'LXXX';
- 9:rn:=rn+'XC';
- End;
- Temp:=Temp Mod 10;
- End;
- Case (Temp) Of
- 1:rn:=rn+'I';
- 2:rn:=rn+'II';
- 3:rn:=rn+'III';
- 4:rn:=rn+'IV';
- 5:rn:=rn+'V';
- 6:rn:=rn+'VI';
- 7:rn:=rn+'VII';
- 8:rn:=rn+'VIII';
- 9:rn:=rn+'IX';
- End;
- Until Temp <= 10;
- Write(User,'=',rn);
- readln();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement