Created
May 20, 2021 22:03
-
-
Save paulofellix/6f6573aafc3fd19d9a989176413c0fb8 to your computer and use it in GitHub Desktop.
Converter Romano Para Arabico
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int traduzirNumeralRomano(String texto) { | |
int n = 0; | |
int numeralDaDireita = 0; | |
for (int i = texto.length() - 1; i >= 0; i--) { | |
int valor = (int) traduzirNumeralRomano(texto.charAt(i)); | |
n += valor * Math.signum(valor + 0.5 - numeralDaDireita); | |
numeralDaDireita = valor; | |
} | |
return n; | |
} | |
private double traduzirNumeralRomano(char caractere) { | |
return Math.floor(Math.pow(10, "IXCM".indexOf(caractere))) + 5 * Math.floor(Math.pow(10, "VLD".indexOf(caractere))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function converter_romano_para_arabico(texto varchar) | |
returns integer | |
LANGUAGE plpgsql | |
as $$ | |
declare | |
n integer; | |
numeralDaDireita integer; | |
valor integer; | |
i integer; | |
caractere varchar; | |
arrRom1 varchar[] = array['I','X','C','M']; | |
arrRom2 varchar[] = array['V', 'L', 'D']; | |
pos1 integer; | |
pos2 integer; | |
begin | |
n := 0; | |
numeralDaDireita := 0; | |
for i in reverse (length(texto))..1 | |
loop | |
caractere := (string_to_array(texto, null))[i]; | |
-- raise notice '%', caractere; | |
pos1 = coalesce(array_position(arrRom1, caractere) -1, -1); | |
-- raise notice '%', pos1; | |
pos2 = coalesce(array_position(arrRom2, caractere) -1, -1); | |
-- raise notice '%', pos2; | |
-- raise notice '%', '--------'; | |
valor = floor(pow(10, pos1) + 5 * floor(pow(10,pos2))); | |
-- raise notice '%', valor; | |
n = n + (valor * sign(valor + 0.5 - numeralDaDireita)); | |
numeralDaDireita = valor; | |
end loop; | |
-- raise notice '%', n; | |
return n; | |
end $$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment