Skip to content

Instantly share code, notes, and snippets.

@farribeiro
Last active July 12, 2023 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farribeiro/99cae2ea64e75b6f9e4c98f404c0b8d7 to your computer and use it in GitHub Desktop.
Save farribeiro/99cae2ea64e75b6f9e4c98f404c0b8d7 to your computer and use it in GitHub Desktop.
function lerNumero(mensagem)
io.write(mensagem)
return io.read("*number")
end
function km4l(km, L)
return km / L
end
function escrevakm4l(titulo)
io.write(("*** %s ***\n"):format(titulo))
local comb = km4l(
lerNumero("\nEntre com a quilometragem rodada: "),
lerNumero("Quantidade de litros gastos: ")
)
io.write(("Km/L: %.2f\n"):format(comb))
return comb
end
function lerpreco(tipo, g)
return lerNumero(("Digite o preço d%s %s: "):format(g, tipo))
end
function porcento(vi, vf, tipo, g, gg)
io.write(("%s %s é %.2f%% mais vantajos%s"):format(gg, tipo, ((vf - vi) / vi * 100), g))
end
function escrevacusto(valor, tipo)
io.write(("Custo %s/KM: %.2f\n"):format(tipo, valor))
end
Opcao = lerNumero("[1] Vantagem álcool ou gasolina \n[2] Km por litro \n> ")
if Opcao == 1 then
CG = escrevakm4l("Gasolina")
CA = escrevakm4l("Álcool")
PG = lerpreco("gasolina", "a")
PA = lerpreco("álcool", "o")
-- Cálculo do custo por quilômetro
CKG = PG / CG -- Custo com gasolina por quilômetro
CKA = PA / CA -- Custo com álcool por quilômetro
escrevacusto(CKG, "Gasolina")
escrevacusto(CKA, "Álcool")
-- Verificação da vantagem
if CKG < CKA then
porcento(CKG, CKA, "gasolina", "a", "A")
elseif CKG > CKA then
porcento(CKA, CKG, "álcool", "o", "O")
else
io.write("Os combustíveis têm o mesmo custo por quilômetro.\n")
end
elseif Opcao == 2 then
escrevakm4l()
end
io.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment