5 de febrero de 2022

Números Arábigos a Romanos

? NUM2ROMANO(1980)

FUNCTION Num2Romano(tnNum)
  DIMENSION laNum(13),laRom(13)
  LOCAL lnI, lcRom

  laNum(1) = 1
  laNum(2) = 4
  laNum(3) = 5
  laNum(4) = 9
  laNum(5) = 10
  laNum(6) = 40
  laNum(7) = 50
  laNum(8) = 90
  laNum(9) = 100
  laNum(10) = 400
  laNum(11) = 500
  laNum(12) = 900
  laNum(13) = 1000

  laRom(1) = "I"
  laRom(2) = "IV"
  laRom(3) = "V"
  laRom(4) = "IX"
  laRom(5) = "X"
  laRom(6) = "XL"
  laRom(7) = "L"
  laRom(8) = "XC"
  laRom(9) = "C"
  laRom(10) = "CD"
  laRom(11) = "D"
  laRom(12) = "CM"
  laRom(13) = "M"

  lcRom = ""

  FOR lnI = 13 TO 1 STEP -1
    DO WHILE tnNum >= laNum(lnI)
      tnNum = tnNum - laNum(lnI)
      lcRom = lcRom + laRom(lnI)
    ENDDO
  ENDFOR
  RETURN lcRom
ENDFUNC

1 comentario :

  1. Alternativa
    * Função para converter um número em um número romano
    FUNCTION converter_numero_romano(lnumero)
    LOCAL croman_number, lvalid_number, lremainder

    croman_number = ""
    lvalid_number = .T.
    lremainder = lnumero

    DO WHILE lvalid_number AND lremainder > 0
    DO CASE
    CASE lremainder >= 1000
    croman_number = croman_number + "M"
    lremainder = lremainder - 1000
    CASE lremainder >= 900
    croman_number = croman_number + "CM"
    lremainder = lremainder - 900
    CASE lremainder >= 500
    croman_number = croman_number + "D"
    lremainder = lremainder - 500
    CASE lremainder >= 400
    croman_number = croman_number + "CD"
    lremainder = lremainder - 400
    CASE lremainder >= 100
    croman_number = croman_number + "C"
    lremainder = lremainder - 100
    CASE lremainder >= 90
    croman_number = croman_number + "XC"
    lremainder = lremainder - 90
    CASE lremainder >= 50
    croman_number = croman_number + "L"
    lremainder = lremainder - 50
    CASE lremainder >= 40
    croman_number = croman_number + "XL"
    lremainder = lremainder - 40
    CASE lremainder >= 10
    croman_number = croman_number + "X"
    lremainder = lremainder - 10
    CASE lremainder >= 9
    croman_number = croman_number + "IX"
    lremainder = lremainder - 9
    CASE lremainder >= 5
    croman_number = croman_number + "V"
    lremainder = lremainder - 5
    CASE lremainder >= 4
    croman_number = croman_number + "IV"
    lremainder = lremainder - 4
    OTHERWISE
    croman_number = croman_number + "I"
    lremainder = lremainder - 1
    ENDCASE
    ENDDO

    RETURN croman_number
    ENDFUNC

    ResponderBorrar

Los comentarios son moderados, por lo que pueden demorar varias horas para su publicación.