Приветствую Вас Гость | RSS

Delphi заготовки

Вторник, 26.11.2024, 23:30
Главная » 2012 » Март » 21 » Перевод из арабских в римские
07:29
Перевод из арабских в римские

Функция перевода чисел из римского в арабское


Function myodd(s1:integer):integer;
begin
if s1 mod 2 =0 then result:=0
else result:=1;
end;

function RomanToArabic( const romanNumber : string) : integer ;
const
romanChars = 'IVXLCDMvxlcdm?!#' ;
decades : array [0..8] of integer = (
0, 1, 10, 100, 1000, 10000, 100000,
1000000, 10000000) ;
OneFive : array [0..1] of byte = (1, 5) ;
var
newValue, oldValue : integer ;
cIdx, P : byte ;
begin
result := 0;
oldValue := 0 ;
for cIdx := Length(romanNumber) downto 1 do
begin
P := Succ(Pos(romanNumber[cIdx], romanChars)) ;
newValue := OneFive[myodd(p)] * decades[P div 2] ;
if newValue = 0 then
begin
result := -1;
Exit;
end ;
if newValue < oldValue then newValue := - newValue ;
Inc(result, newValue) ;
oldValue := newValue
end ;
end;


Функция перевода чисел из арабских в римские


function Arab2Roman(arab:integer):string;
var
  i:integer;
  d:integer;
  arab_str:string;
  arab_len:integer;
begin
  Result := '';
  arab_str := IntToStr(arab);
  arab_len := Length(arab_str);
  for i := 0 to arab_len-1 do begin
    d := StrToInt(arab_str[arab_len-i]);
    if (d+1) mod 5 = 0 then
      Result := Copy('IXCM', 1+i, 1) + Copy('VXLCDM', i*2 + (d+1) div 5, 1) + Result
    else
      Result := Copy('VLD', 1+i, d div 5) + Copy('IIIXXXCCCMMM', 1+i*3, d mod 5) + Result;
  end;
end;

Просмотров: 3763 | Добавил: NetSoftWare | Рейтинг: 0.0/0
Всего комментариев: 1
1 NetSoftWare  
0
первый 100 чисел арабские / римские

1 = I
2 = II
3 = III
4 = IV
5 = V
6 = VI
7 = VII
8 = VIII
9 = IX
10 = X
11 = XI
12 = XII
13 = XIII
14 = XIV
15 = XV
16 = XVI
17 = XVII
18 = XVIII
19 = XIX
20 = XX
21 = XXI
22 = XXII
23 = XXIII
24 = XXIV
25 = XXV
26 = XXVI
27 = XXVII
28 = XXVIII
29 = XXIX
30 = XXX
31 = XXXI
32 = XXXII
33 = XXXIII
34 = XXXIV
35 = XXXV
36 = XXXVI
37 = XXXVII
38 = XXXVIII
39 = XXXIX
40 = XL
41 = XLI
42 = XLII
43 = XLIII
44 = XLIV
45 = XLV
46 = XLVI
47 = XLVII
48 = XLVIII
49 = XLIX
50 = L
51 = LI
52 = LII
53 = LIII
54 = LIV
55 = LV
56 = LVI
57 = LVII
58 = LVIII
59 = LIX
60 = LX
61 = LXI
62 = LXII
63 = LXIII
64 = LXIV
65 = LXV
66 = LXVI
67 = LXVII
68 = LXVIII
69 = LXIX
70 = LXX
71 = LXXI
72 = LXXII
73 = LXXIII
74 = LXXIV
75 = LXXV
76 = LXXVI
77 = LXXVII
78 = LXXVIII
79 = LXXIX
80 = LXXX
81 = LXXXI
82 = LXXXII
83 = LXXXIII
84 = LXXXIV
85 = LXXXV
86 = LXXXVI
87 = LXXXVII
88 = LXXXVIII
89 = LXXXIX
90 = XC
91 = XCI
92 = XCII
93 = XCIII
94 = XCIV
95 = XCV
96 = XCVI
97 = XCVII
98 = XCVIII
99 = XCIX
100 = C

Имя *:
Email *:
Код *: