Skip to content

Instantly share code, notes, and snippets.

@sandiks
Created September 12, 2019 11:22
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 sandiks/db921ecf8f8fe9e14b5572bc7e0d8faf to your computer and use it in GitHub Desktop.
Save sandiks/db921ecf8f8fe9e14b5572bc7e0d8faf to your computer and use it in GitHub Desktop.
def main
if ARGV.length < 2
STDERR << "Usage: #{$0} <a> <b> <c>\n"
exit 1
end
a = ARGV[0].to_f
b = ARGV[1].to_f
c = ARGV[2].to_f
is_quadratic = (ARGV.length>2)
if is_quadratic
solve_quadratic(a,b,c)
else
solve_linear(a,b)
end
end
def solve_quadratic(a,b,c)
if a == 0
STDERR << "<a> should not be zero\n"
exit 1
end
disc = b**2 - 4*a*c
d_str = (disc >= 0) ? (Math.sqrt(disc) % 1 == 0) ? Math.sqrt(disc).to_i : "√#{disc}" : "√#{disc}"
s = "#{-b} ± #{d_str}"
d = "#{2*a}"
puts "\e[4m#{s}\e[0m"
#puts "─"*(s.length)
puts d.center s.length
end
def solve_linear(a,b)
puts "#{-b/a}"
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment