martes, 20 de marzo de 2018

Convertir un numero entero positivo a Texto

Ruby es un lenguaje de programación orientado a objetos, y de código abierto, su sitio de internet https://www.ruby-lang.org/es/ que he empezado a aprender el cual es fácil y sencillo aunque poderoso


El siguiente programa convierte un numero a texto, y esta escrito en Ruby.

su uso con un ejemplo al teclear 1234567, el programa exscribe de salida :

un millón doscientos treinta y cuatro mil quinientos sesenta y siete

El código:


=begin   Convertir un numero a texto
  Autor: Ruben E.
Codificacion en Ruby
Escritura de los numeros en Español
1 = uno
  100 = cien
            1 000 = mil
    1 000 000 = un millon
    1 000 000 000 = mil millones ( en muchos paises de habla inglesa = un billon)
1 000 000 000 000 = un billon    ( en muchos paises de habla inglesa = un trillon)

=end

$Str1= ["", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve","diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete","dieciocho", "diecinueve","veinte"]

$Str3= ["treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa"]

$Str5 = ["ciento", "doscientos", "trescientos","cuatrocientos","quinientos","seicientos","setecientos","ochocientos","novecientos"]


def ceroAveinte(x)
return $Str1[x]
end
def veintes(x)
s = "veinti"
x -= 20;
return s += ceroAveinte(x)
end
def treintaCien(x)
y = (x/10)-3
x%= 10
s= $Str3[y]
if x != 0
s += " y "
end
return s += ceroAveinte(x)
end
def unoAcien(x)
s= ""
if(x <=20)
s= ceroAveinte(x)
else
if x < 30
s= veintes(x)
else
if x <100
s= treintaCien(x)
else
if x == 100
s= "cien"
end
end
end
end
return s
end

def menosDeMil(x)
s= ""
if x <= 100
s = unoAcien(x)
else
y = (x/100)-1
x%= 100
s= $Str5[y]
if x  != 0
s += " "
s += unoAcien(x)
end
end
return s
end
def menosDeMillon(x)
y= 0
s = ""
if x < 1000
s = menosDeMil(x)
else
y = x/1000
x = x%1000
if y == 1
s = "mil"
else
if y == 301
s = "trecientos un mil"
else
s = (menosDeMil(y) + " mil")
end
end
if x != 0
s += (" " + menosDeMil(x))
end
end
return s
end
def menosDeBillon(x)
y = 0
s = ""
if x < 1000000
s= menosDeMillon(x)
else
f = 1
y = x/1000000
x = x%1000000
if y == 1
s = "un millon"
else
if y == 301
s= "trescientos un millones"
elsif y == 301301
s = " trescientos un mil trecientos un millones"
else
s = (menosDeMillon(y) + " millones")
end
end
if (x)
s += " "
end
s += menosDeMillon(x)
end
return s
end
def menosDeTrillon(x)
y = 0
s = ""
if x < 1000000000000
s= menosDeBillon(x)
else
f = 1
y = x/1000000000000
x = x%1000000000000
if y == 1
s = "un billon"
else
s = (menosDeBillon(y) + " billones" )
end
if (x)
s += " "
end
s += menosDeBillon(x)
end
return s
end

def convertir(x)
if x == 0
return s = " cero"
else
menosDeTrillon(x)
end
end

conteo = 0
while conteo < 3
puts " escribe un numero entero "
numero = gets.to_i
#for numero in 1201..1400
#$n = numero
print numero
print " "
puts convertir(numero)
puts ""
conteo += 1
#end
end