Aide 'Delphi' Chiffre romain convertisseur
Dernière réponse : dans Programmation
Bonjour,
je cherche a codé un convertisseur de chiffre romain en chiffre arable.
J'ai dejà essayer de le coder mais je bloc... un avis ?
Merci d'avance.
{$APPTYPE CONSOLE}
uses
SysUtils;
var
TabRom : array [1..31] of string = ('MMMM', 'MMM', 'MM', 'M', 'CM',
'DCCC', 'DCC', 'DC', 'D', 'CD', 'CCC', 'CC', 'C', 'XC', 'LXXX',
'LXX', 'LX', 'L', 'XL', 'XXX', 'XX', 'X', 'IX', 'VIII', 'VII', 'VI',
'V', 'IV', 'III', 'II', 'I');
TabArab : array [1..31] of integer =(4000,3000,2000,1000,900,800,700,600,
500,400,300,200,100,90,80,70,60,50,40,30,20,10,9,8,7,6,5,4,3,2,1);
ChaineValue : string;
i,res : integer;
begin
{Entrer value}
writeln('Entrer un chiffre romain : ');
readln(ChaineValue);
res:=0;
{ comparer la chaine string aux elements du tableau. Faire correspondre
au chiffre romain. A chaque comparaison correcte, on inplemente.}
for i := 1 to length(ChaineValue) do
begin
If (ChaineValue=TabRom) then res:= res + TabArab;
end;
{Sortir le resultat}
write('La solution est : ', res );
readln;
end.
je cherche a codé un convertisseur de chiffre romain en chiffre arable.
J'ai dejà essayer de le coder mais je bloc... un avis ?
Merci d'avance.
Citation :
program Chiffre_romain_En_arabe;{$APPTYPE CONSOLE}
uses
SysUtils;
var
TabRom : array [1..31] of string = ('MMMM', 'MMM', 'MM', 'M', 'CM',
'DCCC', 'DCC', 'DC', 'D', 'CD', 'CCC', 'CC', 'C', 'XC', 'LXXX',
'LXX', 'LX', 'L', 'XL', 'XXX', 'XX', 'X', 'IX', 'VIII', 'VII', 'VI',
'V', 'IV', 'III', 'II', 'I');
TabArab : array [1..31] of integer =(4000,3000,2000,1000,900,800,700,600,
500,400,300,200,100,90,80,70,60,50,40,30,20,10,9,8,7,6,5,4,3,2,1);
ChaineValue : string;
i,res : integer;
begin
{Entrer value}
writeln('Entrer un chiffre romain : ');
readln(ChaineValue);
res:=0;
{ comparer la chaine string aux elements du tableau. Faire correspondre
au chiffre romain. A chaque comparaison correcte, on inplemente.}
for i := 1 to length(ChaineValue) do
begin
If (ChaineValue=TabRom) then res:= res + TabArab;
end;
{Sortir le resultat}
write('La solution est : ', res );
readln;
end.
Autres pages sur : aide delphi chiffre romain convertisseur
Lassé par la pub ? Créez un compte
Bonjour,
Je me suis intéressé à ton idée de convertisseur, mais ton code est assez vide, alors j'ai réalisé une petite class (unit) :
Donc c'est une class (objet) pour Delphi, comment l'utiliser en "roman -> arabe" :
Voila, j'ai pas vérifié le fonctionnement, à toi de t'en inspirer.
Bonne journée
Je me suis intéressé à ton idée de convertisseur, mais ton code est assez vide, alors j'ai réalisé une petite class (unit) :
Unit uRomanNumber;
Interface
Uses System, SysUtils;
const
BASIC_ROMAN : array[1..13] of String = ( 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ) ;
BASIC_VALUES : array[1..13] of Integer = ( 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 );
type
TRomanNumber = Class(TObject)
// This Class represents a Roman numeral, it includes a conversion algorithm for Arabic characters
private
stValue : Integer;
stRoman : String;
procedure intToRoman;
procedure romanToInt;
public
procedure setRoman(Val : String);
function getRoman : String;
procedure setValue(Val : Integer);
function getValue : Integer;
published
constructor Create;
constructor Create(Val : Integer);
constructor Create(Val : String);
property ROMAN : String read getRoman write setRoman;
property VALUE : Integer read getValue write setValue;
end;
Implementation
Uses System, SysUtils;
constructor TRomanNumber.Create;
begin
end;
constructor TRomanNumber.Create(Val : Integer);
begin
stValue := Val;
end;
constructor TRomanNumber.Create(Val : String);
begin
stRoman := Val;
end;
procedure TRomanNumber.intToRoman;
var
remainder, i : Integer;
begin
If stRoman = nil Then
begin
stRoman := '';
If stValue > 0 and stValue < 4000 Then
begin
remainder := setValue;
For i := 1 to 13 do
begin
while remainder >= BASIC_VALUES[i] do
begin
stRoman = stRoman + BASIC_ROMAN[i];
remainder = remainder - BASIC_VALUES[i];
end;
end;
end;
end;
end;
procedure TRomanNumber.romanToInt;
var
r : String;
index, i : Integer;
begin
r := AnsiUpperCase(stRoman);
stValue := 0;
index := 1;
For i := 1 to 13 do
begin
While AnsiPos(BASIC_ROMAN[i], r) = index do
begin
stValue := stValue + BASIC_VALUES[i];
index := index + Length(BASIC_ROMAN[i]);
end;
end;
stRoman := r;
end;
procedure TRomanNumber.setRoman(Val : String);
begin
stRoman := Val;
end;
function TRomanNumber.getRoman : String;
begin
intToRoman;
Result := stRoman;
end;
procedure TRomanNumber.setValue(Val : Integer);
begin
stValue := Val;
end;
function TRomanNumber.getValue : Integer;
begin
romanToInt;
Result := stValue;
end;
end.
Donc c'est une class (objet) pour Delphi, comment l'utiliser en "roman -> arabe" :
Program pRomanTest;
{$APPTYPE CONSOLE}
Uses SysUtils, uRomanNumber;
var
roman : String;
value : Integer;
conve : TRomanNumber;
begin
writeln('Entrer un chiffre romain : ');
readln(roman);
conve := TRomanNumber.Create(roman)
res := conve.VALUE;
write('La solution est : ', res );
readln;
end.
Voila, j'ai pas vérifié le fonctionnement, à toi de t'en inspirer.
Bonne journée
Je me disais aussi ... Ce serait trop beau qu'il fonctionne de suite.
Si t'as besoin de comprendre les méthodes utilisées etc. vas sur DelphiBasics (< clic).
Si t'as besoin de comprendre les méthodes utilisées etc. vas sur DelphiBasics (< clic).
Voila la solution
{$APPTYPE CONSOLE}
uses
SysUtils;
var
insert : string; //nombre Romain
output : integer; //nombre arabe
car_run : integer; //caractère traité
cpt,cptfor : integer; //compteur
{tab_rom : array[0..12] of string =('M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I');}
{tab_val : array[0..12] of integer =(1000,900,500,400,100,90,50,40,10,9,5,4,1); }
nbr_rom : array [0..2,0..9] of string = (('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'),
('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'),
('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'));
exp : array [0..2] of integer = (1,10,100);
begin
try
Write('Quelle valeur transformer ? ');
Readln(insert);
insert:=uppercase(insert);
output:=0;
car_run:=1;
cpt:=9;
//1000
while insert[car_run]='M' do
begin
output:=output+1000;
car_run:=car_run+1;
end;
for cptfor := 2 downto 0 do
begin
writeln(cptfor);
while (cpt>=0) and (Copy(insert,car_run,Length(nbr_rom[cptfor,cpt])) <> nbr_rom[cptfor,cpt]) do
begin
dec(cpt);
end;
output:=output+(cpt*exp[cptfor]);
car_run:=car_run+length(nbr_rom[cptfor,cpt]);
cpt:=9;
end;
if (car_run>length(insert)) then
writeln(output)
else
writeln('Ce n est pas un chiffre romain');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
Citation :
program chiffre_romain_revert;{$APPTYPE CONSOLE}
uses
SysUtils;
var
insert : string; //nombre Romain
output : integer; //nombre arabe
car_run : integer; //caractère traité
cpt,cptfor : integer; //compteur
{tab_rom : array[0..12] of string =('M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I');}
{tab_val : array[0..12] of integer =(1000,900,500,400,100,90,50,40,10,9,5,4,1); }
nbr_rom : array [0..2,0..9] of string = (('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'),
('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'),
('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'));
exp : array [0..2] of integer = (1,10,100);
begin
try
Write('Quelle valeur transformer ? ');
Readln(insert);
insert:=uppercase(insert);
output:=0;
car_run:=1;
cpt:=9;
//1000
while insert[car_run]='M' do
begin
output:=output+1000;
car_run:=car_run+1;
end;
for cptfor := 2 downto 0 do
begin
writeln(cptfor);
while (cpt>=0) and (Copy(insert,car_run,Length(nbr_rom[cptfor,cpt])) <> nbr_rom[cptfor,cpt]) do
begin
dec(cpt);
end;
output:=output+(cpt*exp[cptfor]);
car_run:=car_run+length(nbr_rom[cptfor,cpt]);
cpt:=9;
end;
if (car_run>length(insert)) then
writeln(output)
else
writeln('Ce n est pas un chiffre romain');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
Lassé par la pub ? Créez un compte